Skip to content

Commit

Permalink
chore: Add backport script (#30732)
Browse files Browse the repository at this point in the history
* add script

Co-authored-by: pieh <pieh@users.noreply.github.com>

* re-add try catch

* Update backport.js

* Update scripts/backport.js

Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>

* Update backport.js

Co-authored-by: pieh <pieh@users.noreply.github.com>
Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
  • Loading branch information
3 people committed Apr 8, 2021
1 parent 63cc8fa commit 81ec270
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -51,6 +51,7 @@ __diff_output__/
# misc
.serverless/
.eslintcache
scripts/.env

# lock files
yarn.lock
Expand Down
117 changes: 117 additions & 0 deletions scripts/backport.js
@@ -0,0 +1,117 @@
require(`dotenv`).config()

const yargs = require(`yargs`)
const { Octokit } = require(`@octokit/rest`)
const childProcess = require(`child_process`)

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

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

const argv = yargs
.command(
`* <release> [pr]`,
`Backports merged PR into release branch and open Cherry PR`,
commandBuilder =>
commandBuilder
.positional(`release`, { type: `string` })
.positional(`pr`, { type: `number` })
)
.check(argv => {
if (!/^3\.\d+$/.test(argv.release)) {
throw new Error(`"${argv.release}" is not a release version`)
}

if (!argv.pr) {
throw new Error(`PR number is required`)
}

return true
}).argv

const repo = `gatsby`
const owner = `gatsbyjs`

// no try/catches - if it fails at any point - let it fail and just do stuff manually ;)
// this is just for happy path - if there is extra work needed - this script won't be able to do it
async function run() {
const result = await octokit.pulls.get({
owner,
repo,
pull_number: argv.pr,
})

if (!result.data.merged_at) {
throw new Error(`That pull request was not merged`)
}

const commitsha = result.data.merge_commit_sha

if (!commitsha) {
throw new Error(`Can't get merge commit sha`)
}

const commitMeta = await octokit.git.getCommit({
owner,
repo,
commit_sha: commitsha,
})

// get first line
const commitMessage = commitMeta.data.message.split(`\n`)[0]

const releaseBranchName = `release/${argv.release}`
const backportReleaseBranchName = `backport-${argv.release}-${argv.pr}`

childProcess.execSync(`git fetch origin release/${argv.release}`, {
stdio: `inherit`,
})

try {
childProcess.execSync(`git branch -D "${backportReleaseBranchName}"`, {
stdio: `inherit`,
})
// eslint-disable-next-line no-empty
} catch {}

childProcess.execSync(
`git checkout -b "${backportReleaseBranchName}" "origin/${releaseBranchName}" --no-track`,
{
stdio: `inherit`,
}
)

childProcess.execSync(`git cherry-pick -x ${commitsha}`, {
stdio: `inherit`,
})

childProcess.execSync(`git push origin +${backportReleaseBranchName}`, {
stdio: `inherit`,
})

const pr = await octokit.pulls.create({
owner,
repo,
title: commitMessage,
head: backportReleaseBranchName,
base: releaseBranchName,
body: `Backporting #${argv.pr} to the ${argv.release} release branch\n\n(cherry picked from commit ${commitsha})`,
})

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

await octokit.issues.addLabels({
owner,
repo,
issue_number: pr.data.number,
labels: [`type: cherry`],
})

return result
}

run()

0 comments on commit 81ec270

Please sign in to comment.