forked from nodejs/node-core-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanding_session.js
More file actions
324 lines (283 loc) · 9.46 KB
/
Copy pathlanding_session.js
File metadata and controls
324 lines (283 loc) · 9.46 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
'use strict';
const path = require('path');
const {
getUnmarkedDeprecations,
updateDeprecations
} = require('./deprecations');
const {
runAsync, runSync, forceRunAsync
} = require('./run');
const Session = require('./session');
const { shortSha } = require('./utils');
const isWindows = process.platform === 'win32';
class LandingSession extends Session {
constructor(cli, req, dir, prid, backport) {
super(cli, dir, prid);
this.req = req;
this.backport = backport;
}
get argv() {
const args = super.argv;
args.backport = this.backport;
return args;
}
async start(metadata) {
const { cli } = this;
this.startLanding();
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, req, repo, owner, prid } = this;
// TODO(joyeecheung): restore previously downloaded patches
cli.startSpinner(`Downloading patch for ${prid}`);
const patch = await req.text(
`https://github.com/${owner}/${repo}/pull/${prid}.patch`);
this.savePatch(patch);
cli.stopSpinner(`Downloaded patch to ${this.patchPath}`);
cli.separator();
// TODO: check that patches downloaded match metadata.commits
try {
await forceRunAsync('git', ['am', this.patchPath], {
ignoreFailure: false
});
} catch (ex) {
const should3Way = await cli.prompt(
'The normal `git am` failed. Do you want to retry with 3-way merge?');
if (should3Way) {
await forceRunAsync('git', ['am', '--abort']);
await runAsync('git', [
'am',
'-3',
this.patchPath
]);
} else {
cli.error('Failed to apply patches');
process.exit(1);
}
}
// Check for and maybe assign any unmarked deprecations in the codebase.
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 patch;
}
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;
}
async suggestAfterPatch(patch) {
const { cli } = this;
const subjects = patch.match(/Subject: \[PATCH.*?\].*/g);
if (!subjects) {
cli.warn('Cannot get number of commits in the patch. ' +
'It seems to be malformed');
return;
}
// XXX(joyeecheung) we cannot guarantee that no one will put a subject
// line in the commit message but that seems unlikely (some deps update
// might do that).
if (subjects.length === 1) {
// assert(subjects[0].startsWith('Subject: [PATCH]'))
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();
}
const suggestion = this.getRebaseSuggestion(subjects);
cli.log(`There are ${subjects.length} commits in the PR`);
cli.log('Please run the following commands to complete landing\n\n' +
`$ ${suggestion}\n` +
'$ git node land --continue');
}
async apply() {
const { cli } = this;
if (!this.isApplying()) {
cli.warn('This session can not proceed to apply patches, ' +
'run `git node land --abort`');
return;
}
await this.tryResetBranch();
const patch = await this.downloadAndPatch();
this.startAmending();
await this.suggestAfterPatch(patch);
}
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 } = this;
if (!this.readyToFinal()) { // check git rebase/am has been done
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();
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 are resolving conflict
if (this.amInProgress()) {
cli.log('Looks like you are resolving a `git am` conflict');
cli.log('Please run `git status` for help');
} else { // The conflict has been resolved
this.startAmending();
return this.suggestAfterPatch(this.patch);
}
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;