Skip to content

Commit

Permalink
fix(node): do not trust commiter date
Browse files Browse the repository at this point in the history
The date of a commit is provided by the user, and we should not trust it
blindly. Instead, we can check when was the last push event to the PR
base branch.
  • Loading branch information
aduh95 committed Dec 8, 2023
1 parent dbd8c65 commit 2968f30
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/pr_checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,15 @@ export default class PRChecker {
});

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;
Expand Down
3 changes: 3 additions & 0 deletions lib/queries/PR.gql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ query PR($prid: Int!, $owner: String!, $repo: String!) {
path
}
},
timelineItems(itemTypes: [HEAD_REF_FORCE_PUSHED_EVENT, PULL_REQUEST_COMMIT]) {
updatedAt
},
title,
baseRefName,
headRefName,
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/semver_major_pr.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
}
]
},
"timelineItems": {
"updatedAt": "2017-10-24T11:13:43Z"
},
"title": "lib: awesome changes",
"baseRefName": "main",
"headRefName": "awesome-changes"
Expand Down
25 changes: 25 additions & 0 deletions test/unit/pr_checker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2152,6 +2152,31 @@ describe('PRChecker', () => {
cli.assertCalledWith(expectedLogs);
});

it('should return true if PR can be landed', () => {
const expectedLogs = {
warn: [
['Something was pushed to the Pull Request branch since the last approving review.']
],
info: [],
error: []
};

const checker = new PRChecker(cli, {
pr: { ...semverMajorPR, timelineItems: { updatedAt: new Date().toISOString() } },
reviewers: allGreenReviewers,
comments: commentsWithLGTM,
reviews: approvingReviews,
commits: simpleCommits,
collaborators,
authorIsNew: () => true,
getThread: PRData.prototype.getThread
}, {}, argv);

const status = checker.checkCommitsAfterReview();
assert.deepStrictEqual(status, false);
cli.assertCalledWith(expectedLogs);
});

it('should skip the check if there are no reviews', () => {
const { commits } = multipleCommitsAfterReview;
const expectedLogs = {};
Expand Down

0 comments on commit 2968f30

Please sign in to comment.