Skip to content

Commit 4932d07

Browse files
priyank-pjoyeecheung
authored andcommitted
PRChecker: support new label fast-track (#104)
* PRChecker: support new label fast-track
1 parent b22d056 commit 4932d07

File tree

2 files changed

+152
-17
lines changed

2 files changed

+152
-17
lines changed

lib/pr_checker.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class PRChecker {
5353
const status = [
5454
this.checkReviews(comments),
5555
this.checkCommitsAfterReview(),
56-
this.checkPRWait(new Date()),
5756
this.checkCI(),
57+
this.checkPRWait(new Date()),
5858
this.checkMergeableState()
5959
];
6060

@@ -150,12 +150,31 @@ class PRChecker {
150150
* @param {Date} now
151151
*/
152152
checkPRWait(now) {
153-
const { pr } = this;
154-
const { cli } = this;
153+
const {
154+
pr, cli, reviewers, CIStatus
155+
} = this;
155156
const labels = pr.labels.nodes;
156-
const fast = labels.some((l) => l.name === 'code-and-learn') ||
157-
(labels.length === 1 && labels[0].name === 'doc');
158-
if (fast) { return true; }
157+
158+
const fast =
159+
labels.some((l) => ['fast-track'].includes(l.name));
160+
if (fast) {
161+
const { approved } = reviewers;
162+
if (approved.length > 1 && CIStatus) {
163+
cli.info('This PR is being fast-tracked');
164+
return true;
165+
} else {
166+
const msg = ['This PR is being fast-tracked, but awating '];
167+
if (approved.length < 2) msg.push('approvals of 2 contributors');
168+
if (!CIStatus) msg.push('a CI run');
169+
170+
let warnMsg = msg.length === 2
171+
? msg.join('') : `${msg[0] + msg[1]} and ${msg[2]}`;
172+
cli.warn(warnMsg);
173+
}
174+
175+
return false;
176+
}
177+
159178
const wait = this.getWait(now);
160179
if (wait.timeLeft > 0) {
161180
const dateStr = new Date(pr.createdAt).toDateString();
@@ -182,6 +201,7 @@ class PRChecker {
182201
let status = true;
183202
if (!ciMap.size) {
184203
cli.error('No CI runs detected');
204+
this.CIStatus = false;
185205
return false;
186206
} else if (!ciMap.get(FULL)) {
187207
status = false;
@@ -231,6 +251,7 @@ class PRChecker {
231251
}
232252
}
233253

254+
this.CIStatus = status;
234255
return status;
235256
}
236257

test/unit/pr_checker.test.js

Lines changed: 125 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,37 +201,151 @@ describe('PRChecker', () => {
201201
cli.assertCalledWith(expectedLogs);
202202
});
203203

204-
it('should skip wait check for Code & Learn PR', () => {
204+
it('should log as expected if PR can be fast-tracked', () => {
205205
const cli = new TestCLI();
206206

207-
const expectedLogs = {};
207+
const expectedLogs = {
208+
info: [
209+
[ 'This PR is being fast-tracked' ]
210+
]
211+
};
208212

209-
const now = new Date();
210-
const youngPR = Object.assign({}, firstTimerPR, {
211-
createdAt: '2017-10-27T14:25:41.682Z',
213+
const now = new Date('2017-11-01T14:25:41.682Z');
214+
const PR = Object.assign({}, firstTimerPR, {
215+
createdAt: '2017-10-31T13:00:41.682Z',
212216
labels: {
213217
nodes: [
214-
{
215-
name: 'code-and-learn'
216-
}
218+
{ name: 'fast-track' }
217219
]
218220
}
219221
});
220222

221223
const options = {
222-
pr: youngPR,
224+
pr: PR,
223225
reviewers: allGreenReviewers,
224-
comments: commentsWithLGTM,
226+
comments: commentsWithCI,
225227
reviews: approvingReviews,
226-
commits: simpleCommits,
228+
commits: [],
227229
collaborators
228230
};
229231
const checker = new PRChecker(cli, options, argv);
230232

233+
checker.checkCI();
234+
cli.clearCalls();
231235
const status = checker.checkPRWait(now);
232236
assert(status);
233237
cli.assertCalledWith(expectedLogs);
234238
});
239+
240+
it('should warn about approvals and CI for fast-tracked PR', () => {
241+
const cli = new TestCLI();
242+
243+
const expectedLogs = {
244+
warn: [
245+
[ 'This PR is being fast-tracked, but awating ' +
246+
'approvals of 2 contributors and a CI run' ]
247+
]
248+
};
249+
250+
const now = new Date('2017-11-01T14:25:41.682Z');
251+
const PR = Object.assign({}, firstTimerPR, {
252+
createdAt: '2017-10-31T13:00:41.682Z',
253+
labels: {
254+
nodes: [
255+
{ name: 'fast-track' }
256+
]
257+
}
258+
});
259+
260+
const options = {
261+
pr: PR,
262+
reviewers: requestedChangesReviewers,
263+
comments: [],
264+
reviews: requestingChangesReviews,
265+
commits: simpleCommits,
266+
collaborators
267+
};
268+
const checker = new PRChecker(cli, options, argv);
269+
270+
checker.checkCI();
271+
cli.clearCalls();
272+
const status = checker.checkPRWait(now);
273+
assert(!status);
274+
cli.assertCalledWith(expectedLogs);
275+
});
276+
277+
it('should warn cannot be fast-tracked because of approvals', () => {
278+
const cli = new TestCLI();
279+
280+
const expectedLogs = {
281+
warn: [
282+
[ 'This PR is being fast-tracked, but awating ' +
283+
'approvals of 2 contributors' ]
284+
]
285+
};
286+
287+
const now = new Date('2017-11-01T14:25:41.682Z');
288+
const PR = Object.assign({}, firstTimerPR, {
289+
createdAt: '2017-10-31T13:00:41.682Z',
290+
labels: {
291+
nodes: [
292+
{ name: 'fast-track' }
293+
]
294+
}
295+
});
296+
297+
const options = {
298+
pr: PR,
299+
reviewers: requestedChangesReviewers,
300+
comments: commentsWithCI,
301+
reviews: approvingReviews,
302+
commits: [],
303+
collaborators
304+
};
305+
const checker = new PRChecker(cli, options, argv);
306+
307+
checker.checkCI();
308+
cli.clearCalls();
309+
const status = checker.checkPRWait(now);
310+
assert(!status);
311+
cli.assertCalledWith(expectedLogs);
312+
});
313+
314+
it('should warn if the PR has no CI and cannot be fast-tracked', () => {
315+
const cli = new TestCLI();
316+
317+
const expectedLogs = {
318+
warn: [
319+
[ 'This PR is being fast-tracked, but awating a CI run' ]
320+
]
321+
};
322+
323+
const now = new Date('2017-11-01T14:25:41.682Z');
324+
const PR = Object.assign({}, firstTimerPR, {
325+
createdAt: '2017-10-31T13:00:41.682Z',
326+
labels: {
327+
nodes: [
328+
{ name: 'fast-track' }
329+
]
330+
}
331+
});
332+
333+
const options = {
334+
pr: PR,
335+
reviewers: allGreenReviewers,
336+
comments: [],
337+
reviews: approvingReviews,
338+
commits: simpleCommits,
339+
collaborators
340+
};
341+
const checker = new PRChecker(cli, options, argv);
342+
343+
checker.checkCI();
344+
cli.clearCalls();
345+
const status = checker.checkPRWait(now);
346+
assert(!status);
347+
cli.assertCalledWith(expectedLogs);
348+
});
235349
});
236350

237351
describe('checkCI', () => {

0 commit comments

Comments
 (0)