Skip to content

Commit 105843e

Browse files
committed
fix(git-node): harmonize time left error messages
1 parent 7342aff commit 105843e

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

lib/pr_checker.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const { FROM_COMMENT, FROM_REVIEW_COMMENT } = REVIEW_SOURCES;
1919

2020
const SECOND = 1000;
2121
const MINUTE = SECOND * 60;
22-
const HOUR = MINUTE * 60;
2322

2423
const WAIT_TIME_MULTI_APPROVAL = 24 * 2;
2524
const WAIT_TIME_SINGLE_APPROVAL = 24 * 7;
@@ -196,10 +195,18 @@ export default class PRChecker {
196195

197196
const createTime = new Date(this.pr.createdAt);
198197
const msFromCreateTime = now.getTime() - createTime.getTime();
199-
const minutesFromCreateTime = Math.ceil(msFromCreateTime / MINUTE);
200-
const hoursFromCreateTime = Math.ceil(msFromCreateTime / HOUR);
201-
let timeLeftMulti = this.waitTimeMultiApproval - hoursFromCreateTime;
202-
const timeLeftSingle = this.waitTimeSingleApproval - hoursFromCreateTime;
198+
const minutesFromCreateTime = msFromCreateTime / MINUTE;
199+
const timeLeftMulti = this.waitTimeMultiApproval * 60 - minutesFromCreateTime;
200+
const timeLeftSingle = this.waitTimeSingleApproval * 60 - minutesFromCreateTime;
201+
const timeToText = (time, liaison_word = undefined) => {
202+
let unity = 'minute';
203+
if (time > 59) {
204+
unity = 'hour';
205+
time /= 60;
206+
}
207+
time = Math.ceil(time);
208+
return `${time} ${liaison_word ? liaison_word + ' ' : ''}${unity}${time === 1 ? '' : 's'}`;
209+
};
203210

204211
if (approved.length >= 2) {
205212
if (isFastTracked || isCodeAndLearn) {
@@ -208,26 +215,19 @@ export default class PRChecker {
208215
if (timeLeftMulti < 0) {
209216
return true;
210217
}
211-
if (timeLeftMulti === 0) {
212-
const timeLeftMins =
213-
this.waitTimeMultiApproval * 60 - minutesFromCreateTime;
214-
cli.error(`This PR needs to wait ${timeLeftMins} ` +
215-
`more minutes to land${fastTrackAppendix}`);
216-
return false;
217-
}
218-
cli.error(`This PR needs to wait ${timeLeftMulti} more ` +
219-
`hours to land${fastTrackAppendix}`);
218+
cli.error(
219+
`This PR needs to wait ${timeToText(timeLeftMulti, 'more')} to land${fastTrackAppendix}`
220+
);
220221
return false;
221222
}
222223

223224
if (approved.length === 1) {
224225
if (timeLeftSingle < 0) {
225226
return true;
226227
}
227-
timeLeftMulti = timeLeftMulti < 0 || isFastTracked ? 0 : timeLeftMulti;
228-
cli.error(`This PR needs to wait ${timeLeftSingle} more hours to land ` +
229-
`(or ${timeLeftMulti} hours if there is one more approval)` +
230-
fastTrackAppendix);
228+
cli.error(`This PR needs to wait ${timeToText(timeLeftSingle, 'more')} to land (or ${
229+
timeToText(timeLeftMulti < 0 || isFastTracked ? 0 : timeLeftMulti)
230+
} if there is one more approval)${fastTrackAppendix}`);
231231
return false;
232232
}
233233
}

test/unit/pr_checker.test.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const GT_7D = '2018-11-23T17:50:44.477Z';
5050
const LT_7D_GT_48H = '2018-11-27T17:50:44.477Z';
5151
const LT_48H = '2018-11-30T17:50:44.477Z';
5252
const LT_48H_GT_47H = '2018-11-29T17:55:44.477Z';
53+
const LT_48H_GT_47_59 = '2018-11-29T17:51:43.477Z';
5354
const NOW = '2018-11-31T17:50:44.477Z';
5455

5556
const argv = { maxCommits: 3 };
@@ -304,6 +305,43 @@ describe('PRChecker', () => {
304305
cli.assertCalledWith(expectedLogs);
305306
});
306307

308+
it('should error when PR is younger than 48h and older than 47h58min', () => {
309+
const cli = new TestCLI();
310+
311+
const expectedLogs = {
312+
ok:
313+
[['Approvals: 4'],
314+
['- Foo User (@foo): https://github.com/nodejs/node/pull/16438#pullrequestreview-71480624'],
315+
['- Quux User (@Quux): LGTM'],
316+
['- Baz User (@Baz): https://github.com/nodejs/node/pull/16438#pullrequestreview-71488236'],
317+
['- Bar User (@bar) (TSC): lgtm']],
318+
info: [['This PR was created on Thu, 29 Nov 2018 17:51:43 GMT']],
319+
error: [['This PR needs to wait 1 more minute to land']]
320+
};
321+
322+
const youngPR = Object.assign({}, firstTimerPR, {
323+
createdAt: LT_48H_GT_47_59
324+
});
325+
326+
const data = {
327+
pr: youngPR,
328+
reviewers: allGreenReviewers,
329+
comments: commentsWithLGTM,
330+
reviews: approvingReviews,
331+
commits: simpleCommits,
332+
collaborators,
333+
authorIsNew: () => true,
334+
getThread() {
335+
return PRData.prototype.getThread.call(this);
336+
}
337+
};
338+
const checker = new PRChecker(cli, data, {}, argv);
339+
340+
const status = checker.checkReviewsAndWait(new Date(NOW));
341+
assert(!status);
342+
cli.assertCalledWith(expectedLogs);
343+
});
344+
307345
it('should error when PR has only 1 approval < 48h', () => {
308346
const cli = new TestCLI();
309347

@@ -350,7 +388,7 @@ describe('PRChecker', () => {
350388
info:
351389
[['This PR was created on Tue, 27 Nov 2018 17:50:44 GMT']],
352390
error: [['This PR needs to wait 72 more hours to land ' +
353-
'(or 0 hours if there is one more approval)']]
391+
'(or 0 minutes if there is one more approval)']]
354392
};
355393

356394
const youngPR = Object.assign({}, firstTimerPR, {

0 commit comments

Comments
 (0)