Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git-node: wait time for PR with single approval #290

Merged
merged 2 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/collaborators.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Collaborator {
}

getName() {
return `${this.name}(${this.login})`;
return `${this.name} (@${this.login})`;
}

getContact() {
Expand Down
163 changes: 75 additions & 88 deletions lib/pr_checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const SECOND = 1000;
const MINUTE = SECOND * 60;
const HOUR = MINUTE * 60;

const WAIT_TIME = 48;
const WAIT_TIME_MULTI_APPROVAL = 24 * 2;
const WAIT_TIME_SINGLE_APPROVAL = 24 * 7;

const {
REVIEW_SOURCES: { FROM_COMMENT, FROM_REVIEW_COMMENT }
Expand Down Expand Up @@ -48,12 +49,11 @@ class PRChecker {
);
}

checkAll(comments = false) {
checkAll(checkComments = false) {
const status = [
this.checkReviews(comments),
this.checkCommitsAfterReview(),
this.checkCI(),
this.checkPRWait(new Date()),
this.checkReviewsAndWait(new Date(), checkComments),
this.checkMergeableState(),
this.checkPRState(),
this.checkGitConfig()
Expand All @@ -69,117 +69,104 @@ class PRChecker {
return status.every((i) => i);
}

getTSCHint(people) {
const tsc = people
getTSC(people) {
return people
.filter((p) => p.reviewer.isTSC())
.map((p) => p.reviewer.login);
}

formatReview(reviewer, review) {
let hint = '';
if (tsc.length > 0) {
const list = `(${tsc.join(', ')})`;
hint = `, ${tsc.length} from TSC ${list}`;
if (reviewer.isTSC()) {
hint = ' (TSC)';
}
return hint;
return `- ${reviewer.getName()}${hint}: ${review.ref}`;
}

checkReviews(comments = false) {
const {
pr, cli, reviewers: { requestedChanges, approved }
} = this;
let status = true;

if (requestedChanges.length === 0) {
cli.ok(`Requested Changes: 0`);
} else {
status = false;
let hint = this.getTSCHint(requestedChanges);
cli.error(`Requested Changes: ${requestedChanges.length}${hint}`);
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(`- ${reviewer.getName()}: ${review.ref}`);
cli.error(this.formatReview(reviewer, review));
}
}

if (approved.length === 0) {
status = false;
cli.error(`Approvals: 0`);
} else {
let hint = this.getTSCHint(approved);
cli.ok(`Approvals: ${approved.length}${hint}`);

if (comments) {
for (const { reviewer, review } of approved) {
if (review.source === FROM_COMMENT ||
review.source === FROM_REVIEW_COMMENT) {
cli.info(
`- ${reviewer.getName()} approved in via LGTM in comments`);
}
}
}
return;
}

const labels = pr.labels.nodes.map((l) => l.name);
if (labels.includes('semver-major')) {
const tscApproval = approved.filter((p) => p.reviewer.isTSC()).length;
if (tscApproval < 2) {
status = false;
cli.error('semver-major requires at least two TSC approvals');
}
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`);
}
}

return status;
}

/**
* @param {Date} now
*/
getWait(now) {
checkReviewsAndWait(now, checkComments) {
const {
pr, cli, reviewers
} = this;
const { requestedChanges, approved } = reviewers;
const labels = pr.labels.nodes.map((l) => l.name);

const isFastTracked = labels.includes('fast-track');
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');
}

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
}
}

const createTime = new Date(this.pr.createdAt);
const timeLeft = WAIT_TIME - Math.ceil(
const hoursFromCreateTime = Math.ceil(
Copy link
Member

@joyeecheung joyeecheung Oct 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems rather complicated. I believe we can just combine checkReviews() and checkPRWait() so that the number of approvals and wait times are checked together with conditionals instead of independently, the logic would be much clearer that way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(The two methods are separated only because previously a human would check these two conditions separately, but now there is a relationship between the two we should do what a human brain would do here instead and combine the logic.)

(now.getTime() - createTime.getTime()) / HOUR
);
let timeLeftMulti = WAIT_TIME_MULTI_APPROVAL - hoursFromCreateTime;
const timeLeftSingle = WAIT_TIME_SINGLE_APPROVAL - hoursFromCreateTime;

return {
timeLeft
};
}

// TODO: skip some PRs...we might need a label for that
/**
* @param {Date} now
*/
checkPRWait(now) {
const {
pr, cli, reviewers, CIStatus
} = this;
const labels = pr.labels.nodes;

const fast =
labels.some((l) => ['fast-track'].includes(l.name));
if (fast) {
const { approved } = reviewers;
if (approved.length > 1 && CIStatus) {
cli.info('This PR is being fast-tracked');
if (approved.length >= 2) {
if (isFastTracked) {
return true;
} else {
const msg = ['This PR is being fast-tracked, but awaiting '];
if (approved.length < 2) msg.push('approvals of 2 contributors');
if (!CIStatus) msg.push('a CI run');

let warnMsg = msg.length === 2
? msg.join('') : `${msg[0] + msg[1]} and ${msg[2]}`;
cli.warn(warnMsg);
}

if (timeLeftMulti < 0) {
return true;
}
cli.error(`This PR needs to wait ${timeLeftMulti} more hours to land`);
return false;
}

const wait = this.getWait(now);
if (wait.timeLeft > 0) {
const dateStr = new Date(pr.createdAt).toDateString();
cli.info(`This PR was created on ${dateStr}`);
cli.warn(`Wait at least ${wait.timeLeft} more hours before landing`);
if (approved.length === 1) {
if (timeLeftSingle < 0) {
return true;
}
timeLeftMulti = timeLeftMulti < 0 ? 0 : timeLeftMulti;
cli.error(`This PR needs to wait ${timeLeftSingle} more hours to land ` +
`(or ${timeLeftMulti} hours if there is one more approval)`);
return false;
}

return true;
}

hasFullOrLiteCI(ciMap) {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const allGreenReviewers = {
approved,
requestedChanges: []
};
const singleGreenReviewer = {
approved: [approved[0]],
requestedChanges: []
};
const requestedChangesReviewers = {
requestedChanges,
approved: []
Expand Down Expand Up @@ -76,6 +80,7 @@ module.exports = {
approved,
requestedChanges,
allGreenReviewers,
singleGreenReviewer,
requestedChangesReviewers,
approvingReviews,
requestingChangesReviews,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/collaborators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('collaborators', function() {
collaborators.forEach(collaborator => {
assert.strictEqual(
collaborator.getName(),
`${collaborator.name}(${collaborator.login})`);
`${collaborator.name} (@${collaborator.login})`);
});
});
});
Expand Down
Loading