-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathncu-ci.js
More file actions
executable file
·633 lines (589 loc) · 16.2 KB
/
ncu-ci.js
File metadata and controls
executable file
·633 lines (589 loc) · 16.2 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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#!/usr/bin/env node
import yargs from 'yargs';
import clipboardy from 'clipboardy';
import {
JobParser,
parseJobFromURL,
CI_TYPES_KEYS
} from '../lib/ci/ci_type_parser.js';
import { setVerbosityFromEnv } from '../lib/verbosity.js';
import { listBuilds } from '../lib/ci/ci_utils.js';
import { jobCache } from '../lib/ci/build-types/job.js';
import { PRBuild } from '../lib/ci/build-types/pr_build.js';
import { CommitBuild } from '../lib/ci/build-types/commit_build.js';
import { DailyBuild } from '../lib/ci/build-types/daily_build.js';
import { FailureAggregator } from '../lib/ci/failure_aggregator.js';
import { BenchmarkRun } from '../lib/ci/build-types/benchmark_run.js';
import { HealthBuild } from '../lib/ci/build-types/health_build.js';
import { CITGMBuild } from '../lib/ci/build-types/citgm_build.js';
import {
CITGMComparisonBuild
} from '../lib/ci/build-types/citgm_comparison_build.js';
import {
RunPRJob
} from '../lib/ci/run_ci.js';
import { writeJson, writeFile } from '../lib/file.js';
import { getMergedConfig } from '../lib/config.js';
import { runPromise } from '../lib/run.js';
import auth from '../lib/auth.js';
import Request from '../lib/request.js';
import CLI from '../lib/cli.js';
import { hideBin } from 'yargs/helpers';
setVerbosityFromEnv();
const {
PR,
COMMIT,
BENCHMARK,
CITGM,
CITGM_NOBUILD,
DAILY_MASTER
} = CI_TYPES_KEYS;
const commandKeys = [
'rate',
'walk',
'url',
'pr',
'commit',
'citgm'
];
const args = yargs(hideBin(process.argv))
.completion('completion')
.command({
command: 'rate <type>',
desc: 'Calculate the green rate of a CI job in the last 100 runs',
builder: (yargs) => {
yargs
.positional('type', {
describe: 'type of CI',
choices: ['commit', 'pr']
});
},
handler
})
.command({
command: 'walk <type>',
desc: 'Walk the CI and display the failures',
builder: (yargs) => {
yargs
.positional('type', {
describe: 'type of CI',
choices: ['commit', 'pr']
})
.option('stats', {
default: false,
type: 'boolean',
describe: 'Aggregate the results'
})
.option('cache', {
default: false,
type: 'boolean',
describe: 'Cache the responses from Jenkins in $tmpdir/ncu/cache for testing'
})
.option('limit', {
default: 99,
describe: 'Maximum number of CIs to get data from'
})
.option('since <date>', {
type: 'string',
describe: 'Time since when the CI results should be queried'
}).check(argv => {
try {
// eslint-disable-next-line no-new
new Date(argv.since);
} catch {
throw new Error('--since <date> should be string that can ' +
'be parsed by new Date()');
}
return true;
});
},
handler
})
.command({
command: 'run <prid>',
desc: 'Run CI for given PR',
builder: (yargs) => {
yargs
.positional('prid', {
describe: 'ID of the PR or URL to the PR or its head commit',
type: 'string'
})
.option('certify-safe', {
describe: 'SHA of the commit that is expected to be at the tip of the PR head. ' +
'If not provided, the command will use the SHA of the last approved commit.',
type: 'string'
})
.option('check-for-duplicates', {
describe: 'When set, NCU will query Jenkins recent builds to ensure ' +
'there is not an existing job for the same commit.',
type: 'boolean'
})
.option('owner', {
default: '',
describe: 'GitHub repository owner'
})
.option('repo', {
default: '',
describe: 'GitHub repository name'
});
},
handler
})
.command({
command: 'url <url>',
desc: 'Automatically detect CI type and show results',
builder: (yargs) => {
yargs
.positional('url', {
describe: 'URL of the PR or the CI',
type: 'string'
});
},
handler
})
.command({
command: 'pr <jobid>',
desc: 'Show results of a node-test-pull-request CI job',
builder: (yargs) => {
yargs
.positional('jobid', {
describe: 'id of the job',
type: 'number'
});
},
handler
})
.command({
command: 'commit <jobid>',
desc: 'Show results of a node-test-commit CI job',
builder: (yargs) => {
yargs
.positional('jobid', {
describe: 'id of the first job',
type: 'number'
});
},
handler
})
.command({
command: 'benchmark <jobid>',
desc: 'Show results of a benchmark-node-micro-benchmarks CI job',
builder: (yargs) => {
yargs
.positional('jobid', {
describe: 'id of the job',
type: 'number'
});
},
handler
})
.command({
command: 'citgm <jobid> [jobid2]',
desc: 'Show results of a citgm-smoker CI job',
builder: (yargs) => {
yargs
.positional('jobid', {
describe: 'id of the job',
type: 'number'
})
.positional('jobid2', {
describe: 'id of the second job, if doing a comparison',
type: 'number'
});
},
handler
})
.command({
command: 'daily',
desc: 'Show recent results of node-daily-master',
builder: (yargs) => {
yargs
.option('stats', {
type: 'boolean',
default: false,
describe: 'Aggregate the results'
})
.option('cache', {
type: 'boolean',
default: false,
describe: 'Cache the responses from Jenkins in $tmpdir/ncu/cache for testing'
})
.option('limit', {
default: 15,
describe: 'Maximum number of CIs to get data from'
});
},
handler
})
.demandCommand(1, 'must provide a valid command')
.option('copy', {
describe: 'Write the results as markdown to clipboard',
default: false,
type: 'boolean'
})
.option('skip-more-than', {
describe: 'Skip jobs that fail more than <limit> builds, when --stat is true, default to 10.',
type: 'number',
default: Infinity
})
.option('nobuild', {
describe: 'If running cigtm, whether or not jobid is citgm-nobuild.',
type: 'boolean',
default: false
})
.option('json <path>', {
type: 'string',
describe: 'Write the results as json to <path>'
})
.option('markdown <path>', {
type: 'string',
describe: 'Write the results as markdown to <path>'
}).check(argv => {
if (argv.markdown && commandKeys.includes(argv.markdown)) {
throw new Error('--markdown <path> did not specify a valid path');
} else if (argv.json && commandKeys.includes(argv.json)) {
throw new Error('--json <path> did not specify a valid path');
}
return true;
})
.help();
args.parse();
const commandToType = {
commit: COMMIT,
pr: PR,
benchmark: BENCHMARK,
citgm: CITGM
};
class RunPRJobCommand {
constructor(cli, request, argv) {
this.cli = cli;
this.request = request;
this.dir = process.cwd();
this.argv = argv;
this.config = getMergedConfig(this.dir);
}
get owner() {
return this.argv.owner || this.config.owner;
}
get repo() {
return this.argv.repo || this.config.repo;
}
get prid() {
return this.argv.prid;
}
async start() {
const {
cli, request, prid, repo, owner
} = this;
let validArgs = true;
if (!repo) {
validArgs = false;
cli.error('GitHub repository is missing, please set it via ncu-config ' +
'or pass it via the --repo option');
}
if (!owner) {
cli.error('GitHub owner is missing, please set it via ncu-config ' +
'or pass it via the --owner option');
validArgs = false;
}
if (!validArgs) {
this.cli.setExitCode(1);
return;
}
const { certifySafe, checkForDuplicates } = this.argv;
const jobRunner = new RunPRJob(
cli, request, owner, repo, prid,
certifySafe, checkForDuplicates);
if (!(await jobRunner.start())) {
this.cli.setExitCode(1);
process.exitCode = 1;
}
}
}
class CICommand {
constructor(cli, request, argv) {
this.cli = cli;
this.request = request;
this.argv = argv;
this.queue = [];
this.json = [];
this.markdown = '';
}
async drain() {
if (this.queue.length === 0) {
return;
}
const { cli, queue, argv, request } = this;
for (let i = 0; i < queue.length; ++i) {
const job = queue[i];
cli.separator('');
const progress = `[${i + 1}/${queue.length}]`;
if (job.link) {
cli.log(`${progress} Running ${job.link}`);
} else if (job.jobid) {
cli.log(`${progress} Running ${job.type}: ${job.jobid}`);
} else {
cli.log(`${progress} Running ${job.type}`);
}
cli.separator('');
let build;
let { skipMoreThan } = argv;
if (argv.stats && argv.skipMoreThan === Infinity) {
skipMoreThan = 10;
}
switch (job.type) {
case 'health':
build = new HealthBuild(cli, request, job.ciType, job.builds);
break;
case PR:
build = new PRBuild(cli, request, job.jobid, skipMoreThan);
break;
case COMMIT:
build = new CommitBuild(cli, request, job.jobid, skipMoreThan);
break;
case CITGM:
case CITGM_NOBUILD:
if (job.jobid2) {
build = new CITGMComparisonBuild(cli, request, job);
} else {
build = new CITGMBuild(cli, request, job);
}
break;
case BENCHMARK:
build = new BenchmarkRun(cli, request, job.jobid);
break;
case DAILY_MASTER: {
const daily = new DailyBuild(cli, request, job.jobid);
const data = await daily.getBuildData();
const testCommitBuild = data.subBuilds.filter(subBuild => {
return subBuild.jobName === 'node-test-commit';
})[0];
if (testCommitBuild) {
build = new CommitBuild(cli, request, testCommitBuild.buildNumber);
break;
} else {
throw new Error('Could not find \'node-test-commit\' job');
}
}
default:
throw new Error(`Unknown job type ${job.type}`);
}
await build.getResults();
build.display();
// Set this.json regardless of whether the user has passed
// --json - it's needed for failure aggregation.
const json = build.formatAsJson();
if (json !== undefined) {
this.json = this.json.concat(json);
}
if ((argv.copy || argv.markdown) && !argv.stats) {
this.markdown += build.formatAsMarkdown();
}
}
}
async aggregate() {} // noop
async serialize() {
const { argv, cli } = this;
if (argv.copy) {
if (this.markdown) {
clipboardy.writeSync(this.markdown);
cli.separator('');
cli.log('Written markdown to clipboard');
} else {
cli.error('No markdown generated');
}
}
if (argv.markdown) {
if (this.markdown) {
writeFile(argv.markdown, this.markdown);
cli.separator('');
cli.log(`Written markdown to ${argv.markdown}`);
} else {
cli.error('No markdown generated');
}
}
if (argv.json) {
if (this.json.length) {
writeJson(argv.json, this.json);
cli.separator('');
cli.log(`Wrote JSON to ${argv.json}`);
} else {
cli.error('No JSON generated');
}
}
}
}
class RateCommand extends CICommand {
async initialize() {
this.queue.push({
type: 'health',
ciType: commandToType[this.argv.type]
});
}
}
class WalkCommand extends CICommand {
constructor(cli, request, argv) {
super(cli, request, argv);
if (argv.cache) {
jobCache.enable();
}
}
async initialize() {
const ciType = commandToType[this.argv.type];
const since = this.argv.since ? new Date(this.argv.since) : undefined;
const builds = await listBuilds(this.cli, this.request, ciType, since);
if (builds.count === 0) {
this.cli.log('No applicable builds found.');
return;
}
this.queue.push({ type: 'health', ciType, builds });
for (const build of builds.failed.slice(0, this.argv.limit)) {
this.queue.push(build);
}
}
async aggregate() {
const { argv, cli } = this;
if (this.queue.length === 0) {
return;
}
const aggregator = new FailureAggregator(cli, this.json);
this.json = aggregator.aggregate();
cli.log('');
cli.separator('Stats');
cli.log('');
aggregator.display();
if (argv.markdown || argv.copy) {
this.markdown = aggregator.formatAsMarkdown();
}
}
}
class JobCommand extends CICommand {
constructor(cli, request, argv, command) {
super(cli, request, argv);
this.command = command;
}
async initialize() {
const { queue, argv } = this;
queue.push({
type: commandToType[this.command],
jobid: argv.jobid,
jobid2: argv.jobid2,
noBuild: this.argv.nobuild
});
}
}
class URLCommand extends CICommand {
async initialize() {
const { argv, cli, request, queue } = this;
const parsed = parseJobFromURL(argv.url);
if (parsed) {
queue.push({
type: parsed.type,
jobid: parsed.jobid
});
return;
}
// Parse CI links from PR.
const parser = await JobParser.fromPR(argv.url, cli, request);
if (!parser) { // Not a valid PR URL
cli.error(`${argv.url} is not a valid PR URL`);
return;
}
const ciMap = parser.parse();
if (ciMap.size === 0) {
cli.info(`No CI run detected from ${argv.url}`);
}
for (const [type, ci] of ciMap) {
queue.push({
type,
jobid: ci.jobid
});
}
}
}
class DailyCommand extends CICommand {
constructor(cli, request, argv) {
super(cli, request, argv);
if (argv.cache) {
jobCache.enable();
}
}
async initialize() {
const ciType = DAILY_MASTER;
const builds = await listBuilds(this.cli, this.request, ciType);
this.queue.push({ type: 'health', ciType, builds });
for (const build of builds.failed.slice(0, this.argv.limit)) {
this.queue.push(build);
}
}
async aggregate() {
const { argv, cli } = this;
const aggregator = new FailureAggregator(cli, this.json);
this.json = aggregator.aggregate();
cli.log('');
cli.separator('Stats');
cli.log('');
aggregator.display();
if (argv.markdown || argv.copy) {
this.markdown = aggregator.formatAsMarkdown();
}
}
}
async function main(command, argv) {
const cli = new CLI();
const credentials = await auth({
github: true,
jenkins: true
});
const request = new Request(credentials);
let commandHandler;
// Prepare queue.
switch (command) {
case 'run': {
const maybeURL = URL.parse(argv.prid);
if (maybeURL?.host === 'github.com') {
const [, owner, repo, , prid, , commit_sha] = maybeURL.pathname.split('/');
argv.owner ||= owner;
argv.repo ||= repo;
argv.certifySafe ||= commit_sha;
argv.prid = prid;
}
argv.prid = Number(argv.prid);
const jobRunner = new RunPRJobCommand(cli, request, argv);
return jobRunner.start();
}
case 'rate': {
commandHandler = new RateCommand(cli, request, argv);
break;
}
case 'walk': {
commandHandler = new WalkCommand(cli, request, argv);
break;
}
case 'url': {
commandHandler = new URLCommand(cli, request, argv);
break;
}
case 'pr':
case 'commit':
case 'citgm':
case 'benchmark': {
commandHandler = new JobCommand(cli, request, argv, command);
break;
}
case 'daily': {
commandHandler = new DailyCommand(cli, request, argv, command);
break;
}
default:
return args.showHelp();
}
await commandHandler.initialize();
await commandHandler.drain();
await commandHandler.aggregate();
await commandHandler.serialize();
}
function handler(argv) {
const [command] = argv._;
runPromise(main(command, argv));
}