Skip to content

Commit cb2df75

Browse files
authored
Add initial edition of cherry-pick script (#31705)
1 parent 487bd78 commit cb2df75

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

scripts/open-cherry-pick-pr.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/// <reference lib="esnext.asynciterable" />
2+
// Must reference esnext.asynciterable lib, since octokit uses AsyncIterable internally
3+
/// <reference types="node" />
4+
5+
import Octokit = require("@octokit/rest");
6+
import {runSequence} from "./run-sequence";
7+
import fs = require("fs");
8+
import path = require("path");
9+
10+
const userName = process.env.GH_USERNAME;
11+
const reviewers = process.env.REQUESTING_USER ? [process.env.REQUESTING_USER] : ["weswigham", "RyanCavanaugh"];
12+
const branchName = `pick/${process.env.SOURCE_ISSUE}/${process.env.TARGET_BRANCH}`;
13+
const remoteUrl = `https://${process.argv[2]}@github.com/${userName}/TypeScript.git`;
14+
15+
async function main() {
16+
const currentSha = runSequence([
17+
["git", ["rev-parse", "HEAD"]]
18+
]);
19+
const currentAuthor = runSequence([
20+
["git", ["log", "-1", `--pretty="%aN <%aE>"`]]
21+
])
22+
let logText = runSequence([
23+
["git", ["log", `master..${currentSha}`, `--pretty="%h %s%n%b"`]]
24+
]);
25+
logText = `Cherry-pick PR #${process.env.SOURCE_ISSUE} into ${process.env.TARGET_BRANCH}
26+
27+
Component commits:
28+
${logText}`
29+
const logpath = path.join(__dirname, "../", "logmessage.txt");
30+
runSequence([
31+
["git", ["checkout", "-b", "temp-branch"]],
32+
["git", ["reset", "master", "--soft"]]
33+
]);
34+
fs.writeFileSync(logpath, logText);
35+
runSequence([
36+
["git", ["commit", "-F", logpath, `--author="${currentAuthor}"`]]
37+
]);
38+
fs.unlinkSync(logpath);
39+
const squashSha = runSequence([
40+
["git", ["rev-parse", "HEAD"]]
41+
]);
42+
runSequence([
43+
["git", ["checkout", process.env.TARGET_BRANCH]], // checkout the target branch
44+
["git", ["checkout", "-b", branchName]], // create a new branch
45+
["git", ["cherry-pick", squashSha]], //
46+
["git", ["remote", "add", "fork", remoteUrl]], // Add the remote fork
47+
["git", ["push", "--set-upstream", "fork", branchName, "-f"]] // push the branch
48+
]);
49+
50+
const gh = new Octokit();
51+
gh.authenticate({
52+
type: "token",
53+
token: process.argv[2]
54+
});
55+
const r = await gh.pulls.create({
56+
owner: "Microsoft",
57+
repo: "TypeScript",
58+
maintainer_can_modify: true,
59+
title: `🤖 Cherry-pick PR #${process.env.SOURCE_ISSUE} into ${process.env.TARGET_BRANCH}`,
60+
head: `${userName}:${branchName}`,
61+
base: process.env.TARGET_BRANCH,
62+
body:
63+
`This cherry-pick was triggerd by a request on https://github.com/Microsoft/TypeScript/pull/${process.env.SOURCE_ISSUE}
64+
Please review the diff and merge if no changes are unexpected.
65+
You can view the cherry-pick log [here](https://typescript.visualstudio.com/TypeScript/_build/index?buildId=${process.env.BUILD_BUILDID}&_a=summary).
66+
67+
cc ${reviewers.map(r => "@" + r).join(" ")}`,
68+
});
69+
const num = r.data.number;
70+
console.log(`Pull request ${num} created.`);
71+
72+
await gh.issues.createComment({
73+
number: +process.env.SOURCE_ISSUE,
74+
owner: "Microsoft",
75+
repo: "TypeScript",
76+
body: `Hey @${process.env.REQUESTING_USER}, I've opened #${num} for you.`
77+
});
78+
}
79+
80+
main().catch(async e => {
81+
console.error(e);
82+
process.exitCode = 1;
83+
const gh = new Octokit();
84+
gh.authenticate({
85+
type: "token",
86+
token: process.argv[2]
87+
});
88+
await gh.issues.createComment({
89+
number: +process.env.SOURCE_ISSUE,
90+
owner: "Microsoft",
91+
repo: "TypeScript",
92+
body: `Hey @${process.env.REQUESTING_USER}, I couldn't open a PR with the cherry-pick. ([You can check the log here](https://typescript.visualstudio.com/TypeScript/_build/index?buildId=${process.env.BUILD_BUILDID}&_a=summary)). You may need to squash and pick this PR into ${process.env.TARGET_BRANCH} manually.`
93+
});
94+
});

0 commit comments

Comments
 (0)