forked from nodejs/node-core-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
landing_session.js
461 lines (404 loc) · 13.9 KB
/
landing_session.js
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
'use strict';
const path = require('path');
const os = require('os');
const {
getUnmarkedDeprecations,
updateDeprecations
} = require('./deprecations');
const {
runAsync, runSync, forceRunAsync
} = require('./run');
const Session = require('./session');
const { shortSha } = require('./utils');
const isWindows = process.platform === 'win32';
const LINT_RESULTS = {
SKIPPED: 'skipped',
FAILED: 'failed',
SUCCESS: 'success'
};
class LandingSession extends Session {
constructor(cli, req, dir, {
prid, backport, lint, autorebase, fixupAll,
checkCI, oneCommitMax
} = {}) {
super(cli, dir, prid);
this.req = req;
this.backport = backport;
this.lint = lint;
this.autorebase = autorebase;
this.fixupAll = fixupAll;
this.oneCommitMax = oneCommitMax;
this.expectedCommitShas = [];
this.checkCI = !!checkCI;
}
get argv() {
const args = super.argv;
args.backport = this.backport;
args.lint = this.lint;
args.autorebase = this.autorebase;
args.fixupAll = this.fixupAll;
args.oneCommitMax = this.oneCommitMax;
return args;
}
async start(metadata) {
const { cli } = this;
this.startLanding();
this.expectedCommitShas =
metadata.data.commits.map(({ commit }) => commit.oid);
const status = metadata.status ? 'should be ready' : 'is not ready';
// NOTE(mmarchini): default answer is yes. If --yes is given, we need to be
// more careful though, and we change the default to the result of our
// metadata check.
const defaultAnswer = !cli.assumeYes ? true : metadata.status;
const shouldContinue = await cli.prompt(
`This PR ${status} to land, do you want to continue?`, { defaultAnswer });
if (!shouldContinue) {
cli.setExitCode(1);
return this.abort(false);
}
this.saveMetadata(metadata);
this.startApplying();
return this.apply();
}
async abort(tryResetBranch = true) {
try {
const { cli } = this;
this.cleanFiles();
if (tryResetBranch) {
await this.tryResetBranch();
}
cli.ok(`Aborted \`git node land\` session in ${this.ncuDir}`);
} catch (ex) {
const { cli } = this;
cli.setExitCode(1);
cli.error(`Couldn't abort \`git node land\` session in ${this.ncuDir}`);
throw ex;
}
}
async downloadAndPatch() {
const { cli, repo, owner, prid, expectedCommitShas } = this;
cli.startSpinner(`Downloading patch for ${prid}`);
await runAsync('git', [
'fetch', `https://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) {
cli.error(`Mismatched commits:\n${mismatchedCommits}`);
process.exit(1);
}
const commitInfo = { base, head, shas: commitShas };
this.saveCommitInfo(commitInfo);
try {
await forceRunAsync('git', ['cherry-pick', `${base}..${head}`], {
ignoreFailure: false
});
} catch (ex) {
await forceRunAsync('git', ['cherry-pick', '--abort']);
cli.error('Failed to apply patches');
process.exit(1);
}
// Check for and maybe assign any unmarked deprecations in the codebase.
if (this.updateDeprecations !== 'yes') {
const unmarkedDeprecations = await getUnmarkedDeprecations();
const unmarkedDepCount = unmarkedDeprecations.length;
if (unmarkedDepCount > 0) {
cli.startSpinner('Assigning deprecation numbers to DEPOXXX items');
// Update items then stage files and amend the last commit.
await updateDeprecations(unmarkedDeprecations);
await runAsync('git', ['add', 'doc', 'lib', 'src', 'test']);
await runAsync('git', ['commit', '--amend', '--no-edit']);
cli
.stopSpinner(`Updated ${unmarkedDepCount} DEPOXXX items in codebase`);
}
}
cli.ok('Patches applied');
return commitInfo;
}
getRebaseSuggestion(subjects) {
const { upstream, branch } = this;
let command = `git rebase ${upstream}/${branch} -i`;
command += ' -x "git node land --amend"';
const squashes = subjects.filter(
line => line.includes('fixup!') || line.includes('squash!'));
if (squashes.length !== 0) {
command += ' --autosquash';
}
return command;
}
makeRebaseSuggestion(subjects) {
const suggestion = this.getRebaseSuggestion(subjects);
this.cli.log('Please run the following commands to complete landing\n\n' +
`$ ${suggestion}\n` +
'$ git node land --continue');
}
canAutomaticallyRebase(subjects) {
return subjects.every(line => !line.startsWith('squash!'));
}
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 tryCompleteLanding(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 shouldAmend = await cli.prompt(
'There is only one commit in this PR.\n' +
'do you want to amend the commit message?');
if (!shouldAmend) {
return;
}
const canFinal = await this.amend();
if (!canFinal) {
return;
}
return this.final();
} else if (this.autorebase && this.canAutomaticallyRebase(subjects)) {
// Run git rebase in interactive mode with autosquash but without editor
// so that it will perform everything automatically.
cli.log(`There are ${subjects.length} commits in the PR. ` +
'Attempting autorebase.');
const { upstream, branch } = this;
const assumeYes = this.cli.assumeYes ? '--yes' : '';
const msgAmend = `-x "git node land --amend ${assumeYes}"`;
try {
await forceRunAsync('git',
['rebase', `${upstream}/${branch}`, '-i', '--autosquash', msgAmend],
{
ignoreFailure: false,
spawnArgs: {
shell: true,
env: { ...process.env, GIT_SEQUENCE_EDITOR: ':' }
}
});
return this.final();
} catch (e) {
await runAsync('git', ['rebase', '--abort']);
const count = subjects.length;
cli.log(`Couldn't rebase ${count} commits in the PR automatically`);
this.makeRebaseSuggestion(subjects);
}
} else if (this.fixupAll) {
cli.log(`There are ${subjects.length} commits in the PR. ` +
'Attempting to fixup everything into first commit.');
await runAsync('git', ['reset', '--soft', `HEAD~${subjects.length - 1}`]);
await runAsync('git', ['commit', '--amend', '--no-edit']);
return await this.amend() && this.final();
} else {
this.makeRebaseSuggestion(subjects);
}
}
async apply() {
const { cli } = this;
// Bail if another landing session is currently in progress.
if (!this.isApplying()) {
cli.warn('Landing session already in progress - ' +
'to start a new one run `git node land --abort`');
return;
}
await this.tryResetBranch();
const commitInfo = await this.downloadAndPatch();
const cleanLint = await this.validateLint();
if (cleanLint === LINT_RESULTS.FAILED) {
const tryFixLint = await cli.prompt(
'Lint failed - try fixing with \'make lint-js-fix\'?');
if (tryFixLint) {
await runAsync('make', ['lint-js-fix']);
const fixed = await this.validateLint();
if (fixed === LINT_RESULTS.FAILED) {
cli.warn('Patch still contains lint errors. ' +
'Please fix manually before proceeding');
}
}
const correctedLint = await cli.prompt('Corrected all lint errors?');
if (correctedLint) {
await runAsync('git', ['add', '.']);
// Final message will be edited later - don't try to change it here.
await runAsync('git', ['commit', '--amend', '--no-edit']);
} else {
cli.info('Please fix lint errors and then run ' +
'`git node land --amend` followed by ' +
'`git node land --continue`.');
process.exit(1);
}
} else if (cleanLint === LINT_RESULTS.SUCCESS) {
cli.ok('Lint passed cleanly');
}
this.startAmending();
await this.tryCompleteLanding(commitInfo);
}
async amend() {
const { cli } = this;
if (!this.readyToAmend()) {
cli.warn('Not yet ready to amend, run `git node land --abort`');
return;
}
this.startAmending();
const rev = this.getCurrentRev();
const original = runSync('git', [
'show', 'HEAD', '-s', '--format=%B'
]).trim();
const metadata = this.metadata.trim().split('\n');
const amended = original.split('\n');
if (amended[amended.length - 1] !== '') {
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;
for (const line of metadata) {
if (original.includes(line)) {
if (line) {
cli.warn(`Found ${line}, skipping..`);
}
} 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);
}
}
}
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;
}
// TODO: fire the configured git editor on that file
cli.log(`Please manually edit ${messageFile}, then run\n` +
`\`git commit --amend -F ${messageFile}\` ` +
'to finish amending the message');
process.exit(1); // make it work with git rebase -x
}
async final() {
const {
cli, owner, repo, upstream, branch, prid, oneCommitMax
} = this;
// Check that git rebase/am has been completed.
if (!this.readyToFinal()) {
cli.warn('Not yet ready to final');
cli.log('A git rebase/am is in progress.' +
' Please complete it before running git node land --final');
return;
};
const stray = this.getStrayCommits();
if (stray.length > 1) {
const forceLand = await cli.prompt(
'There are more than one commit in the PR. ' +
'Do you still want to land it?',
{ defaultAnswer: !oneCommitMax });
if (!forceLand) {
cli.info(
'Use --fixupAll option, squash the PR manually or land the PR from ' +
'the command line.'
);
process.exit(1);
}
}
const strayVerbose = this.getStrayCommits(true);
const validateCommand = path.join(
__dirname,
'../node_modules/.bin/core-validate-commit' + (isWindows ? '.cmd' : '')
);
try {
await forceRunAsync(validateCommand, stray, { ignoreFailure: false });
} catch (e) {
let forceLand = false;
if (e.code === 1) {
forceLand = await cli.prompt(
'The commit did not pass the validation. ' +
'Do you still want to land it?',
{ defaultAnswer: false });
}
if (!forceLand) {
cli.info('Please fix the commit message and try again.');
process.exit(1);
}
}
cli.separator();
cli.log('The following commits are ready to be pushed to ' +
`${upstream}/${branch}`);
cli.log(`- ${strayVerbose.join('\n- ')}`);
cli.separator();
let willBeLanded = shortSha(stray[stray.length - 1]);
if (stray.length > 1) {
const head = shortSha(this.getUpstreamHead());
willBeLanded = `${head}...${willBeLanded}`;
}
this.cleanFiles();
cli.log('Temporary files removed');
cli.log('To finish landing:');
cli.log(`1. Run \`git push ${upstream} ${branch}\``);
const url = `https://github.com/${owner}/${repo}/pull/${prid}`;
cli.log(`2. Post "Landed in ${willBeLanded}" in ${url}`);
}
async continue() {
const { cli } = this;
if (this.readyToFinal()) {
cli.log('Running `final`..');
return this.final();
}
if (this.readyToAmend()) {
cli.log('Running `amend`..');
return this.amend();
}
if (this.isApplying()) {
// We're still resolving conflicts.
if (this.cherryPickInProgress()) {
cli.log('Looks like you are resolving a `git cherry-pick` conflict');
cli.log('Please run `git status` for help');
} else {
// Conflicts has been resolved - amend.
this.startAmending();
return this.tryCompleteLanding(this.commitInfo);
}
return;
}
if (this.hasStarted()) {
cli.log('Running `apply`..');
return this.apply();
}
cli.log(
'Please run `git node land <PRID> to start a landing session`');
}
async status() {
// TODO
}
}
module.exports = LandingSession;