-
Notifications
You must be signed in to change notification settings - Fork 113
/
pr_checker.js
642 lines (547 loc) · 18.4 KB
/
pr_checker.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
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
634
635
636
637
638
639
640
641
642
import {
REVIEW_SOURCES
} from './reviews.js';
import {
CONFLICTING
} from './mergeable_state.js';
import {
shortSha
} from './utils.js';
import {
JobParser,
CI_TYPES,
CI_PROVIDERS,
isFullCI
} from './ci/ci_type_parser.js';
import { PRBuild } from './ci/build-types/pr_build.js';
const { FROM_COMMENT, FROM_REVIEW_COMMENT } = REVIEW_SOURCES;
const SECOND = 1000;
const MINUTE = SECOND * 60;
const HOUR = MINUTE * 60;
const WAIT_TIME_MULTI_APPROVAL = 24 * 2;
const WAIT_TIME_SINGLE_APPROVAL = 24 * 7;
const GITHUB_SUCCESS_CONCLUSIONS = ['SUCCESS', 'NEUTRAL', 'SKIPPED'];
const FAST_TRACK_RE = /^Fast-track has been requested by @(.+?)\. Please 👍 to approve\.$/;
const FAST_TRACK_MIN_APPROVALS = 2;
const GIT_CONFIG_GUIDE_URL = 'https://github.com/nodejs/node/blob/99b1ada/doc/guides/contributing/pull-requests.md#step-1-fork';
// eslint-disable-next-line no-extend-native
Array.prototype.findLastIndex ??= function findLastIndex(fn) {
const reversedIndex = Reflect.apply(
Array.prototype.findIndex,
this.slice().reverse(),
arguments);
return reversedIndex === -1 ? -1 : this.length - reversedIndex - 1;
};
export default class PRChecker {
/**
* @param {{}} cli
* @param {PRData} data
*/
constructor(cli, data, request, argv) {
this.cli = cli;
this.request = request;
this.data = data;
const {
pr, reviewers, comments, reviews, commits, collaborators
} = data;
this.reviewers = reviewers;
this.pr = pr;
this.comments = comments;
// this.reviews and this.commits must
// be in order as received from github api
// to check if new commits were pushed after
// the last review.
this.reviews = reviews;
this.commits = commits;
this.argv = argv;
this.collaboratorEmails = new Set(
Array.from(collaborators).map((c) => c[1].email)
);
}
get waitTimeSingleApproval() {
if (this.argv.waitTimeSingleApproval === undefined) {
return WAIT_TIME_SINGLE_APPROVAL;
}
return this.argv.waitTimeSingleApproval;
}
get waitTimeMultiApproval() {
if (this.argv.waitTimeMultiApproval === undefined) {
return WAIT_TIME_MULTI_APPROVAL;
}
return this.argv.waitTimeMultiApproval;
}
async checkAll(checkComments = false, checkCI = true) {
const status = [
this.checkCommitsAfterReview(),
this.checkReviewsAndWait(new Date(), checkComments),
this.checkMergeableState(),
this.checkPRState(),
this.checkGitConfig()
];
if (checkCI) {
status.push(await this.checkCI());
}
if (this.data.authorIsNew()) {
status.push(this.checkAuthor());
}
// TODO: check for pre-backport, Github API v4
// does not support reading files changed
return status.every((i) => i);
}
getTSC(people) {
return people
.filter((p) => p.reviewer.isTSC())
.map((p) => p.reviewer.login);
}
formatReview(reviewer, review) {
let hint = '';
if (reviewer.isTSC()) {
hint = ' (TSC)';
}
return `- ${reviewer.getName()}${hint}: ${review.ref}`;
}
displayReviews(checkComments) {
const { cli, reviewers: { requestedChanges, approved } } = this;
if (requestedChanges.length > 0) {
cli.error(`Requested Changes: ${requestedChanges.length}`);
for (const { reviewer, review } of requestedChanges) {
cli.error(this.formatReview(reviewer, review));
}
}
if (approved.length === 0) {
cli.error('Approvals: 0');
return;
}
cli.ok(`Approvals: ${approved.length}`);
for (const { reviewer, review } of approved) {
cli.ok(this.formatReview(reviewer, review));
if (checkComments &&
[FROM_COMMENT, FROM_REVIEW_COMMENT].includes(review.source)) {
cli.info(`- ${reviewer.getName()} approved in via LGTM in comments`);
}
}
}
checkReviewsAndWait(now, checkComments) {
const {
pr, cli, reviewers
} = this;
const { requestedChanges, approved } = reviewers;
const labels = pr.labels.nodes.map((l) => l.name);
let isFastTracked = labels.includes('fast-track');
const isCodeAndLearn = labels.includes('code-and-learn');
const isSemverMajor = labels.includes('semver-major');
const dateStr = new Date(pr.createdAt).toUTCString();
cli.info(`This PR was created on ${dateStr}`);
this.displayReviews(checkComments);
// NOTE: a semver-major PR with fast-track should have either one of
// these labels removed because that doesn't make sense
if (isFastTracked) {
cli.info('This PR is being fast-tracked');
} else if (isCodeAndLearn) {
cli.info('This PR is being fast-tracked because ' +
'it is from a Code and Learn event');
}
if (approved.length === 0 || requestedChanges.length > 0) {
return false;
}
if (isSemverMajor) {
const tscApproved = approved
.filter((p) => p.reviewer.isTSC())
.map((p) => p.reviewer.login);
if (tscApproved.length < 2) {
cli.error('semver-major requires at least 2 TSC approvals');
return false; // 7 day rule doesn't matter here
}
}
let fastTrackAppendix = '';
if (isFastTracked) {
const comment = [...this.comments].reverse().find((c) =>
FAST_TRACK_RE.test(c.bodyText));
if (!comment) {
cli.error('Unable to find the fast-track request comment.');
return false;
}
const [, requester] = comment.bodyText.match(FAST_TRACK_RE);
const collaborators = Array.from(this.data.collaborators.values(),
(c) => c.login.toLowerCase());
const approvals = comment.reactions.nodes.filter((r) =>
r.user.login !== requester &&
r.user.login !== pr.author.login &&
collaborators.includes(r.user.login.toLowerCase())).length;
const missingFastTrackApprovals = FAST_TRACK_MIN_APPROVALS - approvals -
(requester === pr.author.login ? 0 : 1);
if (missingFastTrackApprovals > 0) {
isFastTracked = false;
fastTrackAppendix = ' (or 0 hours if there ' +
`${missingFastTrackApprovals === 1 ? 'is' : 'are'} ` +
`${missingFastTrackApprovals} more approval` +
`${missingFastTrackApprovals === 1 ? '' : 's'} (👍) of ` +
'the fast-track request from collaborators).';
}
}
const createTime = new Date(this.pr.createdAt);
const msFromCreateTime = now.getTime() - createTime.getTime();
const minutesFromCreateTime = Math.ceil(msFromCreateTime / MINUTE);
const hoursFromCreateTime = Math.ceil(msFromCreateTime / HOUR);
let timeLeftMulti = this.waitTimeMultiApproval - hoursFromCreateTime;
const timeLeftSingle = this.waitTimeSingleApproval - hoursFromCreateTime;
if (approved.length >= 2) {
if (isFastTracked || isCodeAndLearn) {
return true;
}
if (timeLeftMulti < 0) {
return true;
}
if (timeLeftMulti === 0) {
const timeLeftMins =
this.waitTimeMultiApproval * 60 - minutesFromCreateTime;
cli.error(`This PR needs to wait ${timeLeftMins} ` +
`more minutes to land${fastTrackAppendix}`);
return false;
}
cli.error(`This PR needs to wait ${timeLeftMulti} more ` +
`hours to land${fastTrackAppendix}`);
return false;
}
if (approved.length === 1) {
if (timeLeftSingle < 0) {
return true;
}
timeLeftMulti = timeLeftMulti < 0 || isFastTracked ? 0 : timeLeftMulti;
cli.error(`This PR needs to wait ${timeLeftSingle} more hours to land ` +
`(or ${timeLeftMulti} hours if there is one more approval)` +
fastTrackAppendix);
return false;
}
}
hasFullCI(ciMap) {
const cis = [...ciMap.keys()];
return cis.find(isFullCI);
}
async checkCI() {
const ciType = this.argv.ciType || CI_PROVIDERS.NODEJS;
const providers = Object.values(CI_PROVIDERS);
if (!providers.includes(ciType)) {
this.cli.error(
`Invalid ciType ${ciType} - must be one of ${providers.join(', ')}`);
return false;
}
let status = false;
if (ciType === CI_PROVIDERS.NODEJS) {
status = await this.checkNodejsCI();
} else if (ciType === CI_PROVIDERS.GITHUB) {
status = this.checkGitHubCI();
}
return status;
}
// TODO: we might want to check CI status when it's less flaky...
// TODO: not all PR requires CI...labels?
async checkJenkinsCI() {
const { cli, commits, request, argv } = this;
const { maxCommits } = argv;
const thread = this.data.getThread();
const ciMap = new JobParser(thread).parse();
let status = true;
if (!ciMap.size) {
cli.error('No Jenkins CI runs detected');
this.CIStatus = false;
return false;
} else if (!this.hasFullCI(ciMap)) {
status = false;
cli.error('No full Jenkins CI runs detected');
}
let lastCI;
for (const [type, ci] of ciMap) {
const name = CI_TYPES.get(type).name;
cli.info(`Last ${name} CI on ${ci.date}: ${ci.link}`);
if (!lastCI || lastCI.date < ci.date) {
lastCI = {
typeName: name,
date: ci.date,
jobId: ci.jobid
};
}
}
if (lastCI) {
const afterCommits = [];
commits.forEach((commit) => {
commit = commit.commit;
if (commit.committedDate > lastCI.date) {
status = false;
afterCommits.push(commit);
}
});
const totalCommits = afterCommits.length;
if (totalCommits > 0) {
const warnMsg = 'Commits were pushed after the last ' +
`${lastCI.typeName} CI run:`;
cli.warn(warnMsg);
const sliceLength = maxCommits === 0 ? totalCommits : -maxCommits;
afterCommits.slice(sliceLength)
.forEach(commit => {
cli.warn(`- ${commit.messageHeadline}`);
});
if (totalCommits > maxCommits) {
const infoMsg = '...(use `' +
`--max-commits ${totalCommits}` +
'` to see the full list of commits)';
cli.warn(infoMsg);
}
}
// Check the last CI run for its results.
const build = new PRBuild(cli, request, lastCI.jobId);
const { result, failures } = await build.getResults();
if (result === 'FAILURE') {
cli.error(
`${failures.length} failure(s) on the last Jenkins CI run`);
status = false;
// NOTE(mmarchini): not sure why PEDING returns null
} else if (result === null) {
cli.error(
'Last Jenkins CI still running');
status = false;
} else {
cli.ok('Last Jenkins CI successful');
}
}
this.CIStatus = status;
return status;
}
checkGitHubCI() {
const { cli, commits } = this;
if (!commits || commits.length === 0) {
cli.error('No commits detected');
return false;
}
// NOTE(mmarchini): we only care about the last commit. Maybe in the future
// we'll want to check all commits for a successful CI.
const { commit } = commits[commits.length - 1];
this.CIStatus = false;
const checkSuites = commit.checkSuites || { nodes: [] };
if (!commit.status && checkSuites.nodes.length === 0) {
cli.error('No GitHub CI runs detected');
return false;
}
// GitHub new Check API
for (const { status, conclusion, app } of checkSuites.nodes) {
if (app.slug !== 'github-actions') {
// Ignore all non-github check suites, such as Dependabot and Codecov.
// They are expected to show up on PRs whose head branch is not on a
// fork and never complete.
continue;
}
if (status !== 'COMPLETED') {
cli.error('GitHub CI is still running');
return false;
}
if (!GITHUB_SUCCESS_CONCLUSIONS.includes(conclusion)) {
cli.error('Last GitHub CI failed');
return false;
}
}
// GitHub old commit status API
if (commit.status) {
const { state } = commit.status;
if (state === 'PENDING') {
cli.error('GitHub CI is still running');
return false;
}
if (!['SUCCESS', 'EXPECTED'].includes(state)) {
cli.error('Last GitHub CI failed');
return false;
}
}
cli.ok('Last GitHub CI successful');
this.CIStatus = true;
return true;
}
requiresJenkinsRun() {
const { pr } = this;
// NOTE(mmarchini): if files not present, fallback
// to old behavior. This should only be the case on old tests
// TODO(mmarchini): add files to all fixtures on old tests
if (!pr.files) {
return false;
}
const files = pr.files.nodes;
// Don't require Jenkins run for doc-only change.
if (files.every(({ path }) => path.endsWith('.md'))) {
return false;
}
const ciNeededFolderRx = /^(deps|lib|src|test)\//;
const ciNeededToolFolderRx =
/^tools\/(code_cache|gyp|icu|inspector|msvs|snapshot|v8_gypfiles)/;
const ciNeededFileRx = /^tools\/\.+.py$/;
const ciNeededFileList = [
'tools/build-addons.js',
'configure',
'configure.py',
'Makefile'
];
const ciNeededExtensionList = ['.gyp', '.gypi', '.bat'];
return files.some(
({ path }) =>
ciNeededFolderRx.test(path) ||
ciNeededToolFolderRx.test(path) ||
ciNeededFileRx.test(path) ||
ciNeededFileList.includes(path) ||
ciNeededExtensionList.some((ext) => path.endsWith(ext))
);
}
async checkNodejsCI() {
let status = this.checkGitHubCI();
if (
this.pr.labels.nodes.some((l) => l.name === 'needs-ci') ||
this.requiresJenkinsRun()
) {
status &= await this.checkJenkinsCI();
} else {
this.cli.info('Green GitHub CI is sufficient');
}
return status;
}
checkAuthor() {
const { cli, commits, pr } = this;
const oddCommits = this.filterOddCommits(commits);
if (!oddCommits.length) {
return true;
}
const prAuthor = `${pr.author.login}(${pr.author.email})`;
cli.warn(`PR author is a new contributor: @${prAuthor}`);
for (const c of oddCommits) {
const { oid, author } = c.commit;
const hash = shortSha(oid);
cli.warn(`- commit ${hash} is authored by ${author.email}`);
}
return false;
}
filterOddCommits(commits) {
return commits.filter((c) => this.isOddAuthor(c.commit));
}
isOddAuthor(commit) {
const { pr } = this;
// They have turned on the private email feature, can't really check
// anything, GitHub should know how to link that, see nodejs/node#15489
if (!pr.author.email) {
return false;
}
// If they have added the alternative email to their account,
// commit.authoredByCommitter should be set to true by Github
if (commit.authoredByCommitter) {
return false;
}
if (commit.author.email === pr.author.email) {
return false;
}
// At this point, the commit:
// 1. is not authored by the commiter i.e. author email is not in the
// committer's Github account
// 3. is not authored by the people opening the PR
return true;
}
checkGitConfig() {
const { cli, commits } = this;
for (const { commit } of commits) {
if (commit.author.user === null) {
cli.warn('GitHub cannot link the author of ' +
`'${commit.messageHeadline}' to their GitHub account.`);
cli.warn('Please suggest them to take a look at ' +
`${GIT_CONFIG_GUIDE_URL}`);
}
}
return true;
}
async checkCommitsAfterReviewOrLabel() {
if (this.checkCommitsAfterReview()) return true;
await Promise.all([this.data.getLabeledEvents(), this.data.getCollaborators()]);
const {
cli, data, pr
} = this;
const { updatedAt } = pr.timelineItems;
const requestCiLabels = data.labeledEvents.findLast(
({ createdAt, label: { name } }) => name === 'request-ci' && createdAt > updatedAt
);
if (requestCiLabels == null) return false;
const { actor: { login } } = requestCiLabels;
const collaborators = Array.from(data.collaborators.values(),
(c) => c.login.toLowerCase());
if (collaborators.includes(login.toLowerCase())) {
cli.info('request-ci label was added by a Collaborator after the last push event.');
return true;
}
return false;
}
checkCommitsAfterReview() {
const {
commits, reviews, cli, argv
} = this;
const { maxCommits } = argv;
const reviewIndex = reviews.findLastIndex(
review => review.authorCanPushToRepository && review.state === 'APPROVED'
);
if (reviewIndex === -1) {
cli.warn('No approving reviews found');
return false;
}
const reviewDate = reviews[reviewIndex].publishedAt;
const afterCommits = [];
commits.forEach((commit) => {
commit = commit.commit;
if (commit.committedDate > reviewDate) {
afterCommits.push(commit);
}
});
const totalCommits = afterCommits.length;
if (totalCommits === 0 && this.pr.timelineItems.updatedAt > reviewDate) {
// Some commits were pushed, but all the commits have a commit date prior
// to the last review. It means that either that a long time elapsed
// between the commit and the push, or that the clock on the dev machine
// is wrong, or the commit date was forged.
cli.warn('Something was pushed to the Pull Request branch since the last approving review.');
return false;
}
if (totalCommits > 0) {
cli.warn('Commits were pushed since the last approving review:');
const sliceLength = maxCommits === 0 ? totalCommits : -maxCommits;
afterCommits.slice(sliceLength)
.forEach(commit => {
cli.warn(`- ${commit.messageHeadline}`);
});
if (totalCommits > maxCommits) {
const infoMsg = '...(use `' +
`--max-commits ${totalCommits}` +
'` to see the full list of commits)';
cli.warn(infoMsg);
}
return false;
}
return true;
}
checkMergeableState() {
const {
pr, cli
} = this;
if (pr.mergeable && pr.mergeable === CONFLICTING) {
cli.warn('This PR has conflicts that must be resolved');
return false;
}
return true;
}
checkPRState() {
const {
pr: { closed, closedAt, merged, mergedAt },
cli
} = this;
if (merged) {
const dateStr = new Date(mergedAt).toUTCString();
cli.warn(`This PR was merged on ${dateStr}`);
return false;
}
if (closed) {
const dateStr = new Date(closedAt).toUTCString();
cli.warn(`This PR was closed on ${dateStr}`);
return false;
}
return true;
}
}