-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathland.js
203 lines (187 loc) · 5.3 KB
/
land.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
'use strict';
const { parsePRFromURL } = require('../../lib/links');
const getMetadata = require('../metadata');
const CLI = require('../../lib/cli');
const Request = require('../../lib/request');
const PRChecker = require('../../lib/pr_checker');
const { runPromise } = require('../../lib/run');
const LandingSession = require('../../lib/landing_session');
const epilogue = require('./epilogue');
const yargs = require('yargs');
const availableChecks = Object.values(PRChecker.availableChecks);
const landOptions = {
apply: {
describe: 'Apply a patch with the given PR id',
type: 'number'
},
amend: {
describe: 'Amend the current commit',
type: 'boolean'
},
continue: {
alias: 'c',
describe: 'Continue the landing session',
type: 'boolean'
},
final: {
describe: 'Verify the landed PR and clean up',
type: 'boolean'
},
abort: {
describe: 'Abort the current landing session',
type: 'boolean'
},
yes: {
type: 'boolean',
default: false,
describe: 'Assume "yes" as answer to all prompts and run ' +
'non-interactively. If an undesirable situation occurs, such as a pull ' +
'request or commit check fails, then git node land will abort.'
},
backport: {
describe: 'Land a backport PR onto a staging branch',
default: false,
type: 'boolean'
},
skipChecks: {
describe: 'Comma-separated list of checks to skip while landing. ' +
`Available options are: ${availableChecks}`,
coerce: (arg) => {
const checks = arg.split(',');
for (const check of checks) {
if (!availableChecks.includes(check)) {
throw new Error(`Invalid check '${check}'. Available options are: ` +
`${availableChecks}`);
}
}
return checks;
},
type: 'string'
}
};
function builder(yargs) {
return yargs
.options(landOptions).positional('prid', {
describe: 'ID or URL of the Pull Request'
})
.epilogue(epilogue)
.example('git node land https://github.com/nodejs/node/pull/12344',
'Land https://github.com/nodejs/node/pull/12344 in the current directory')
.example('git node land 12344',
'Land https://github.com/nodejs/node/pull/12344 in the current directory')
.example('git node land --abort',
'Abort the current session')
.example('git node land --amend',
'Append metadata to the current commit message')
.example('git node land --final',
'Verify the landed PR and clean up')
.example('git node land --continue',
'Continue the current landing session');
}
const START = 'start';
const APPLY = 'apply';
const AMEND = 'amend';
const FINAL = 'final';
const CONTINUE = 'continue';
const ABORT = 'abort';
function handler(argv) {
if (argv.prid) {
if (Number.isInteger(argv.prid)) {
return land(START, argv);
} else {
const parsed = parsePRFromURL(argv.prid);
if (parsed) {
Object.assign(argv, parsed);
return land(START, argv);
}
}
yargs.showHelp();
return;
}
const provided = [];
for (const type of Object.keys(landOptions)) {
if (['yes', 'skipChecks'].includes(type)) {
// Those are not actions
continue;
}
if (argv[type]) {
provided.push(type);
}
}
if (provided.length === 1) {
return land(provided[0], argv);
}
// If the more than one action is provided or no valid action
// is provided, show help.
yargs.showHelp();
}
function land(state, argv) {
const cli = new CLI(process.stderr);
if (argv.yes) {
cli.setAssumeYes();
}
const req = new Request();
const dir = process.cwd();
return runPromise(main(state, argv, cli, req, dir)).catch((err) => {
if (cli.spinner.enabled) {
cli.spinner.fail();
}
throw err;
});
}
module.exports = {
command: 'land [prid|options]',
describe:
'Manage the current landing session or start a new one for a pull request',
builder,
handler
};
async function main(state, argv, cli, req, dir) {
let session = new LandingSession(cli, req, dir);
if (state !== AMEND && state !== CONTINUE && session.warnForWrongBranch()) {
return;
}
if (argv.yes) {
cli.setAssumeYes();
}
try {
session.restore();
} catch (err) { // JSON error?
if (state === ABORT) {
await session.abort();
return;
}
cli.warn(
'Failed to detect previous session. ' +
'please run `git node land --abort`');
return;
}
if (state === START) {
if (session.hasStarted()) {
cli.warn(
'Previous `git node land` session for ' +
`${session.pullName} in progress.`);
cli.log('run `git node land --abort` before starting a new session');
return;
}
session = new LandingSession(cli, req, dir, argv.prid, argv.backport);
const metadata = await getMetadata(session.argv, cli, argv.skipChecks);
if (argv.backport) {
const split = metadata.metadata.split('\n')[0];
if (split === 'PR-URL: ') {
cli.error('Commit message is missing PR-URL');
}
}
return session.start(metadata);
} else if (state === APPLY) {
return session.apply();
} else if (state === AMEND) {
return session.amend();
} else if (state === FINAL) {
return session.final();
} else if (state === ABORT) {
return session.abort();
} else if (state === CONTINUE) {
return session.continue();
}
}