Skip to content

Commit

Permalink
simplify a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
vladar committed Aug 25, 2021
1 parent 46b15b7 commit 712d457
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions scripts/gatsby-changelog-generator/update-and-open-pr.js
@@ -1,13 +1,18 @@
const execa = require(`execa`)
const { Octokit } = require(`@octokit/rest`)
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`])
// TODO: save current branch/commit/hash (and restore on complete)
const base = `vladar/generate-changelogs`
const branch = `changelog-update-${Date.now()}`

const args = [`checkout`, `-b`, branch, `origin/${base}`, `--no-track`]
await execa(`git`, args)

const updatedPackages = []
for (const pkg of getAllPackageNames()) {
Expand All @@ -26,24 +31,31 @@ async function run() {
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])
}
const updatedChangelogs = updatedPackages.map(
pkg => `packages/${pkg}/CHANGELOG.md`
)
await execa(`git`, [`add`, ...updatedChangelogs])
await execa(`git`, [`commit`, `-m`, commitMessage])
await execa(`git`, [`push`, `-u`, `origin`, branch])

const octokit = new Octokit({
auth: `token ${process.env.GITHUB_ACCESS_TOKEN}`,
})

try {
const owner = `gatsbyjs`
const repo = `gatsby`

// Note: PR may already exist for this branch.
// Then it will throw but we don't care too much
const pr = await octokit.pulls.create({
owner: `gatsby`,
repo: `gatsbyjs`,
owner,
repo,
title: commitMessage,
head: branchName,
base: `master`,
body: `Update changelogs of the following packages:\n\n${updatedPackages
head: branch,
base,
body: `Updated changelogs of the following packages:\n\n${updatedPackages
.map(p => `- ${p}`)
.join(`\n`)}`,
})
Expand All @@ -54,9 +66,11 @@ async function run() {
owner,
repo,
issue_number: pr.data.number,
labels: [`bot: merge on green`],
labels: [`type: maintenance`],
})
} catch (e) {
console.log(e)
console.error(e)
}
}

run()

0 comments on commit 712d457

Please sign in to comment.