-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathcherry_pick.js
More file actions
304 lines (265 loc) · 8.75 KB
/
cherry_pick.js
File metadata and controls
304 lines (265 loc) · 8.75 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import os from 'node:os';
import path from 'node:path';
import { getMetadata } from '../components/metadata.js';
import {
runAsync, runSync, forceRunAsync
} from './run.js';
import { writeFile } from './file.js';
import {
shortSha, getEditor
} from './utils.js';
import { getNcuDir } from './config.js';
const LINT_RESULTS = {
SKIPPED: 'skipped',
FAILED: 'failed',
SUCCESS: 'success'
};
export default class CheckPick {
constructor(prid, dir, cli, {
owner,
repo,
lint,
includeCVE
} = {}) {
this.prid = prid;
this.cli = cli;
this.dir = dir;
this.options = { owner, repo, lint, includeCVE };
}
get includeCVE() {
return this.options.includeCVE ?? false;
}
get owner() {
return this.options.owner || 'nodejs';
}
get repo() {
return this.options.repo || 'node';
}
get lint() {
return this.options.lint;
}
getUpstreamHead() {
const { upstream, branch } = this;
return runSync('git', ['rev-parse', `${upstream}/${branch}`]).trim();
}
getCurrentRev() {
return runSync('git', ['rev-parse', 'HEAD']).trim();
}
getStrayCommits(verbose) {
const { upstream, branch } = this;
const ref = `${upstream}/${branch}...HEAD`;
const gitCmd = verbose
? ['log', '--oneline', '--reverse', ref]
: ['rev-list', '--reverse', ref];
const revs = runSync('git', gitCmd).trim();
return revs ? revs.split('\n') : [];
}
get ncuDir() {
return getNcuDir(this.dir);
}
get pullDir() {
return path.resolve(this.ncuDir, `${this.prid}`);
}
getMessagePath(rev) {
return path.resolve(this.pullDir, `${shortSha(rev)}.COMMIT_EDITMSG`);
}
saveMessage(rev, message) {
const file = this.getMessagePath(rev);
writeFile(file, message);
return file;
}
async start() {
const { cli } = this;
const metadata = await getMetadata({
prid: this.prid,
owner: this.owner,
repo: this.repo
}, false, cli);
const expectedCommitShas =
metadata.data.commits.map(({ commit }) => commit.oid);
const amend = await cli.prompt(
'Would you like to amend this PR to the proposal?',
{ default: true }
);
if (!amend) {
return true;
}
try {
const commitInfo = await this.downloadAndPatch(expectedCommitShas);
const cleanLint = await this.validateLint();
if (cleanLint === LINT_RESULTS.FAILED) {
cli.error('Patch still contains lint errors. ' +
'Please fix manually before proceeding');
return false;
} else if (cleanLint === LINT_RESULTS.SUCCESS) {
cli.ok('Lint passed cleanly');
}
return this.amend(metadata.metadata, commitInfo);
} catch (e) {
cli.error(e.message);
return false;
}
}
async downloadAndPatch(expectedCommitShas) {
const { cli, repo, owner, prid } = this;
cli.startSpinner(`Downloading patch for ${prid}`);
// fetch via ssh to handle private repo
await runAsync('git', [
'fetch', `git@github.com:${owner}/${repo}.git`,
`refs/pull/${prid}/merge`]);
// We fetched the commit that would result if we used `git merge`.
// ^1 and ^2 refer to the PR base and the PR head, respectively.
const [base, head] = await runAsync('git',
['rev-parse', 'FETCH_HEAD^1', 'FETCH_HEAD^2'],
{ captureStdout: 'lines' });
const commitShas = await runAsync('git',
['rev-list', `${base}..${head}`],
{ captureStdout: 'lines' });
cli.stopSpinner(`Fetched commits as ${shortSha(base)}..${shortSha(head)}`);
cli.separator();
const mismatchedCommits = [
...commitShas.filter((sha) => !expectedCommitShas.includes(sha))
.map((sha) => `Unexpected commit ${sha}`),
...expectedCommitShas.filter((sha) => !commitShas.includes(sha))
.map((sha) => `Missing commit ${sha}`)
].join('\n');
if (mismatchedCommits.length > 0) {
throw new Error(`Mismatched commits:\n${mismatchedCommits}`);
}
const commitInfo = { base, head, shas: commitShas };
try {
await forceRunAsync('git', ['cherry-pick', `${base}..${head}`], {
ignoreFailure: false
});
} catch (ex) {
await forceRunAsync('git', ['cherry-pick', '--abort']);
throw new Error('Failed to apply patches');
}
cli.ok('Patches applied');
return commitInfo;
}
async validateLint() {
// The linter is currently only run on non-Windows platforms.
if (os.platform() === 'win32') {
return LINT_RESULTS.SKIPPED;
}
if (!this.lint) {
return LINT_RESULTS.SKIPPED;
}
try {
await runAsync('make', ['lint']);
return LINT_RESULTS.SUCCESS;
} catch {
return LINT_RESULTS.FAILED;
}
}
async amend(metadata, commitInfo) {
const { cli } = this;
const subjects = await runAsync('git',
['log', '--pretty=format:%s', `${commitInfo.base}..${commitInfo.head}`],
{ captureStdout: 'lines' });
if (commitInfo.shas.length !== 1) {
const fixupAll = await cli.prompt(
`${subjects.length} commits from the original PR are going to be` +
'squashed into a single commit. OK to proceed?', {
defaultAnswer: true
});
if (!fixupAll) {
// TODO: add this support?
throw new Error(`There are ${subjects.length} commits in the PR ` +
'and the ammend were not able to succeed');
}
await runAsync('git', ['reset', '--soft', `HEAD~${subjects.length - 1}`]);
await runAsync('git', ['commit', '--amend', '--no-edit']);
}
return this._amend(metadata);
}
async _amend(metadataStr) {
const { cli } = this;
const rev = this.getCurrentRev();
const original = runSync('git', [
'show', 'HEAD', '-s', '--format=%B'
]).trim();
// git has very specific rules about what is a trailer and what is not.
// Instead of trying to implement those ourselves, let git parse the
// original commit message and see if it outputs any trailers.
const originalHasTrailers = runSync('git', [
'interpret-trailers', '--parse', '--no-divider'
], {
input: `${original}\n`
}).trim().length !== 0;
const metadata = metadataStr.trim().split('\n');
const amended = original.split('\n');
// If the original commit message already contains trailers (such as
// "Co-authored-by"), we simply add our own metadata after those. Otherwise,
// we have to add an empty line so that git recognizes our own metadata as
// trailers in the amended commit message.
if (!originalHasTrailers) {
amended.push('');
}
const BACKPORT_RE = /BACKPORT-PR-URL\s*:\s*(\S+)/i;
const PR_RE = /PR-URL\s*:\s*(\S+)/i;
const REVIEW_RE = /Reviewed-By\s*:\s*(\S+)/i;
const CVE_RE = /CVE-ID\s*:\s*(\S+)/i;
let containCVETrailer = false;
for (const line of metadata) {
if (line.length !== 0 && original.includes(line)) {
if (line.match(CVE_RE)) {
containCVETrailer = true;
}
if (originalHasTrailers) {
cli.warn(`Found ${line}, skipping..`);
} else {
throw new Error(
'Git found no trailers in the original commit message, ' +
`but '${line}' is present and should be a trailer.`);
}
} else {
if (line.match(BACKPORT_RE)) {
let prIndex = amended.findIndex(datum => datum.match(PR_RE));
if (prIndex === -1) {
prIndex = amended.findIndex(datum => datum.match(REVIEW_RE)) - 1;
}
amended.splice(prIndex + 1, 0, line);
} else {
amended.push(line);
}
}
}
if (!containCVETrailer && this.includeCVE) {
const cveID = await cli.prompt(
'Git found no CVE-ID trailer in the original commit message. ' +
'Please, provide the CVE-ID',
{ questionType: 'input', defaultAnswer: 'CVE-2023-XXXXX' }
);
amended.push('CVE-ID: ' + cveID);
}
const message = amended.join('\n');
const messageFile = this.saveMessage(rev, message);
cli.separator('New Message');
cli.log(message.trim());
const takeMessage = await cli.prompt('Use this message?');
if (takeMessage) {
await runAsync('git', ['commit', '--amend', '-F', messageFile]);
return true;
}
const editor = await getEditor({ git: true });
if (editor) {
try {
await forceRunAsync(
editor,
[`"${messageFile}"`],
{ ignoreFailure: false, spawnArgs: { shell: true } }
);
await runAsync('git', ['commit', '--amend', '-F', messageFile]);
return true;
} catch {
cli.warn(`Please manually edit ${messageFile}, then run\n` +
`\`git commit --amend -F ${messageFile}\` ` +
'to finish amending the message');
throw new Error(
'Failed to edit the message using the configured editor');
}
}
}
}