-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathbackport.mjs
125 lines (103 loc) · 3.03 KB
/
backport.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import 'dotenv/config';
import { Octokit } from '@octokit/rest';
import childProcess from 'child_process';
import { defineCommand, runMain } from 'citty';
if (!process.env.GITHUB_ACCESS_TOKEN) {
throw new Error(`GITHUB_ACCESS_TOKEN environment variable must be set.`);
}
const repo = 'javascript';
const owner = 'clerk';
const octokit = new Octokit({
auth: `token ${process.env.GITHUB_ACCESS_TOKEN}`,
});
/**
* Get the details of the PR, create a new branch, cherry-pick the commit, push the branch, and create a PR.
*/
async function github({ pull_number, branch }) {
const prDetails = await octokit.pulls.get({
owner,
repo,
pull_number,
});
if (!prDetails.data.merged_at) {
throw new Error(`PR ${pull_number} is not merged yet.`);
}
const commitSha = prDetails.data.merge_commit_sha;
if (!commitSha) {
throw new Error(`PR ${pull_number} does not have a merge commit sha.`);
}
const commitMeta = await octokit.git.getCommit({
owner,
repo,
commit_sha: commitSha,
});
// Get the first line of the commit
const commitMessage = commitMeta.data.message.split('\n')[0];
console.log(`Commit message: ${commitMessage}`);
const targetBranchName = branch;
const backportBranchName = `backport-${branch}-${pull_number}`;
childProcess.execSync(`git fetch origin ${targetBranchName}`, {
stdio: `inherit`,
});
try {
childProcess.execSync(`git branch -D "${backportBranchName}"`, {
stdio: `inherit`,
});
// eslint-disable-next-line no-empty
} catch {}
childProcess.execSync(`git checkout -b "${backportBranchName}" "origin/${targetBranchName}" --no-track`, {
stdio: `inherit`,
});
childProcess.execSync(`git cherry-pick -x ${commitSha}`, {
stdio: `inherit`,
});
childProcess.execSync(`git push origin +${backportBranchName}`, {
stdio: `inherit`,
});
const pr = await octokit.pulls.create({
owner,
repo,
title: commitMessage,
head: backportBranchName,
base: targetBranchName,
body: `Backporting #${pull_number} to the ${targetBranchName} 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: [`cherry`],
});
}
/**
* @example node backport.mjs <branch> <pr>
* @example node backport.mjs v4 1234
*/
const main = defineCommand({
meta: {
name: 'backport',
version: '1.0.0',
description: 'Backport merged PR into a branch & create a cherry-pick PR',
},
args: {
branch: {
type: 'positional',
description: 'The branch on which to create the cherry-pick PR',
required: true,
},
pr: {
type: 'positional',
description: 'The PR number to backport',
required: true,
},
},
setup() {
console.log('Starting backport script. If this script fails, finish the rest manually.');
},
async run({ args }) {
const { branch, pr } = args;
await github({ branch, pull_number: Number(pr) });
},
});
void runMain(main);