Skip to content

Commit

Permalink
draft: open PR script
Browse files Browse the repository at this point in the history
  • Loading branch information
vladar committed Aug 25, 2021
1 parent 9c3efb3 commit 46b15b7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 11 deletions.
4 changes: 2 additions & 2 deletions scripts/gatsby-changelog-generator/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const argv = yargs
try {
await regenerateChangelog(pkg)
} catch (e) {
console.error(`${pkg} error: ${e.message}`)
console.error(`${pkg}: ${e.stack}`)
}
}
}
Expand All @@ -55,7 +55,7 @@ const argv = yargs
try {
await updateChangelog(pkg)
} catch (e) {
console.error(`${pkg} error: ${e.message}`)
console.error(`${pkg}: ${e.stack}`)
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions scripts/gatsby-changelog-generator/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ function addChangelogEntries(packageName, entries, contents) {
entries.trimRight(),
contents.substr(header.length),
]
fs.writeFileSync(changelogPath(packageName), updatedChangelogParts.join(`\n\n`))
fs.writeFileSync(
changelogPath(packageName),
updatedChangelogParts.join(`\n\n`)
)
}

/**
Expand All @@ -269,14 +272,12 @@ async function updateChangelog(packageName) {
}
const changeLog = await generateChangelog(packageName, latestVersion)
if (!changeLog) {
console.log(
`Skipping ${packageName}: no new versions after ${latestVersion}`
)
return
return false
}

addChangelogEntries(packageName, changeLog, contents)
console.log(`Updated ${path}`)
return true
}

/**
Expand All @@ -285,7 +286,7 @@ async function updateChangelog(packageName) {
* Should be used inside lerna "version" hook (version is changed in package.json but not committed/tagged yet).
* See https://github.com/lerna/lerna/tree/main/commands/version#lifecycle-scripts
*/
async function onVersion() {
async function onNewVersion() {
// get list of changed files (package.json of published packages are expected to be dirty)
const { stdout } = await execa("git", ["ls-files", "-m"])
const packages = String(stdout).split(`\n`).map(toPackageName).filter(Boolean)
Expand Down Expand Up @@ -349,4 +350,4 @@ async function onVersion() {
exports.getAllPackageNames = getAllPackageNames
exports.regenerateChangelog = regenerateChangelog
exports.updateChangelog = updateChangelog
exports.onVersion = onVersion
exports.onNewVersion = onNewVersion
4 changes: 2 additions & 2 deletions scripts/gatsby-changelog-generator/lerna-version-lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const { onVersion } = require(`./generate`)
onVersion().catch(console.error)
const { onNewVersion } = require(`./generate`)
onNewVersion().catch(console.error)
62 changes: 62 additions & 0 deletions scripts/gatsby-changelog-generator/update-and-open-pr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const execa = require(`execa`)
const { getAllPackageNames, updateChangelog } = require(`./generate`)

if (!process.env.GITHUB_ACCESS_TOKEN) {
throw new Error(`GITHUB_ACCESS_TOKEN env var not set`)
}

async function run() {
await execa(`git`, [`checkout`, `master`])
await execa(`git`, [`pull`, `--tags`])

const updatedPackages = []
for (const pkg of getAllPackageNames()) {
try {
const updated = await updateChangelog(pkg)
if (updated) {
updatedPackages.push(pkg)
}
} catch (e) {
console.error(`${pkg}: ${e.stack}`)
}
}

if (!updatedPackages.length) {
console.log(`Nothing to do`)
return
}

// Commit to the same branch
const branchName = `bot-changelog-update`
const commitMessage = `DO NOT MERGE: testing`
try {
await execa(`git`, [`checkout`, `-b`, branchName, `origin/${branchName}`])
} catch {
await execa(`git`, [`checkout`, branchName])
}
await execa(`git`, [`commit`, `-m`, commitMessage])

try {
const pr = await octokit.pulls.create({
owner: `gatsby`,
repo: `gatsbyjs`,
title: commitMessage,
head: branchName,
base: `master`,
body: `Update changelogs of the following packages:\n\n${updatedPackages
.map(p => `- ${p}`)
.join(`\n`)}`,
})

console.log(`\n---\n\nPR opened - ${pr.data.html_url}`)

await octokit.issues.addLabels({
owner,
repo,
issue_number: pr.data.number,
labels: [`bot: merge on green`],
})
} catch (e) {
console.log(e)
}
}

0 comments on commit 46b15b7

Please sign in to comment.