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

Do not compare dates to detect new commits #911

Merged
merged 3 commits into from
Mar 28, 2025
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
6 changes: 3 additions & 3 deletions bin/ncu-ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ const args = yargs(hideBin(process.argv))
type: 'number'
})
.positional('certify-safe', {
describe: 'If not provided, the command will reject PRs that have ' +
'been pushed since the last review',
type: 'boolean'
describe: 'SHA of the commit that is expected to be at the tip of the PR head. ' +
'If not provided, the command will use the SHA of the last approved commit.',
type: 'string'
})
.option('owner', {
default: '',
Expand Down
3 changes: 2 additions & 1 deletion lib/ci/run_ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class RunPRJob {
this.certifySafe =
certifySafe ||
Promise.all([this.prData.getReviews(), this.prData.getPR()]).then(() =>
new PRChecker(cli, this.prData, request, {}).checkCommitsAfterReviewOrLabel()
(this.certifySafe = new PRChecker(cli, this.prData, request, {}).getApprovedTipOfHead())
);
}

Expand All @@ -45,6 +45,7 @@ export class RunPRJob {
payload.append('json', JSON.stringify({
parameter: [
{ name: 'CERTIFY_SAFE', value: 'on' },
{ name: 'COMMIT_SHA_CHECK', value: this.certifySafe },
{ name: 'TARGET_GITHUB_ORG', value: this.owner },
{ name: 'TARGET_REPO_NAME', value: this.repo },
{ name: 'PR_ID', value: this.prid },
Expand Down
72 changes: 19 additions & 53 deletions lib/pr_checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,38 +524,17 @@ export default class PRChecker {
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() {
getApprovedTipOfHead() {
const {
commits, reviews, cli, argv
} = this;
const { maxCommits } = argv;

if (commits.length === 0) {
cli.warn('No commits found');
return false;
}

const reviewIndex = reviews.findLastIndex(
review => review.authorCanPushToRepository && review.state === 'APPROVED'
);
Expand All @@ -565,45 +544,32 @@ export default class PRChecker {
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;
}
const reviewedCommitIndex = commits
.findLastIndex(({ commit }) => commit.oid === reviews[reviewIndex].commit.oid);

if (totalCommits > 0) {
if (reviewedCommitIndex !== commits.length - 1) {
cli.warn('Commits were pushed since the last approving review:');
const sliceLength = maxCommits === 0 ? totalCommits : -maxCommits;
afterCommits.slice(sliceLength)
.forEach(commit => {
commits.slice(Math.max(reviewedCommitIndex + 1, commits.length - maxCommits))
.forEach(({ commit }) => {
cli.warn(`- ${commit.messageHeadline}`);
});

const totalCommits = commits.length - reviewedCommitIndex - 1;
if (totalCommits > maxCommits) {
const infoMsg = '...(use `' +
`--max-commits ${totalCommits}` +
'` to see the full list of commits)';
`--max-commits ${totalCommits}` +
'` to see the full list of commits)';
cli.warn(infoMsg);
}

return false;
}

return true;
return reviews[reviewIndex].commit.oid;
}

checkCommitsAfterReview() {
return !!this.getApprovedTipOfHead();
}

checkMergeableState() {
Expand Down
10 changes: 0 additions & 10 deletions lib/pr_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
} from './user_status.js';

// lib/queries/*.gql file names
const LABELED_EVENTS_QUERY = 'PRLabeledEvents';
const PR_QUERY = 'PR';
const REVIEWS_QUERY = 'Reviews';
const COMMENTS_QUERY = 'PRComments';
Expand Down Expand Up @@ -34,7 +33,6 @@ export default class PRData {
this.comments = [];
this.commits = [];
this.reviewers = [];
this.labeledEvents = [];
}

getThread() {
Expand Down Expand Up @@ -92,14 +90,6 @@ export default class PRData {
]);
}

async getLabeledEvents() {
const { prid, owner, repo, cli, request, prStr } = this;
const vars = { prid, owner, repo };
cli.updateSpinner(`Getting labels from ${prStr}`);
this.labeledEvents = (await request.gql(LABELED_EVENTS_QUERY, vars))
.repository.pullRequest.timelineItems.nodes;
}

async getComments() {
const { prid, owner, repo, cli, request, prStr } = this;
const vars = { prid, owner, repo };
Expand Down
3 changes: 0 additions & 3 deletions lib/queries/PR.gql
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ 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
19 changes: 0 additions & 19 deletions lib/queries/PRLabeledEvents.gql

This file was deleted.

9 changes: 0 additions & 9 deletions test/fixtures/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,3 @@ for (const subdir of readdirSync(path('./jenkins'))) {
readJSON(`./jenkins/${subdir}/${item}`);
}
};

export const labeledEvents = {};

for (const item of readdirSync(path('./labeled_events'))) {
if (!item.endsWith('.json')) {
continue;
}
labeledEvents[basename(item, '.json')] = readJSON(`./labeled_events/${item}`);
}
1 change: 0 additions & 1 deletion test/fixtures/first_timer_pr.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
}
]
},
"timelineItems": { "updatedAt": "2017-10-24T11:13:43Z" },
"title": "test: awesome changes",
"baseRefName": "main",
"headRefName": "awesome-changes"
Expand Down
12 changes: 0 additions & 12 deletions test/fixtures/labeled_events/no-request-ci.json

This file was deleted.

12 changes: 0 additions & 12 deletions test/fixtures/labeled_events/old-request-ci-collaborator.json

This file was deleted.

12 changes: 0 additions & 12 deletions test/fixtures/labeled_events/recent-request-ci-collaborator.json

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},{
"commit": {
"committedDate": "2017-09-25T11:27:02Z",
"oid": "f230e691459f8b0f448e1eec1b83fbf7f708eba3",
"oid": "f230e691459f8b0f448e1eec1b83fbf7f708eba4",
"messageHeadline": "src: add requested feature",
"message": "src: new functionality\n works really great",
"author": {
Expand All @@ -31,7 +31,7 @@
},{
"commit": {
"committedDate": "2017-10-25T12:42:02Z",
"oid": "9416475a6dc1b27c3e343dcbc07674a439c88db1",
"oid": "9416475a6dc1b27c3e343dcbc07674a439c88db2",
"messageHeadline": "nit: edit mistakes",
"message": "nit: fix mistakes\n fixed requested errors",
"author": {
Expand All @@ -41,7 +41,7 @@
},{
"commit": {
"committedDate": "2017-10-25T12:42:02Z",
"oid": "9416475a6dc1b27c3e343dcbc07674a439c88db1",
"oid": "9416475a6dc1b27c3e343dcbc07674a439c88db3",
"messageHeadline": "final: we should be good to go",
"message": "final: we should be good to go\n fixed requested errors",
"author": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"author": {
"login": "foo"
},
"commit": {"oid": "f230e691459f8b0f448e1eec1b83fbf7f708eba3"},
"authorCanPushToRepository": true,
"url": "https://github.com/nodejs/node/pull/16438#pullrequestreview-89923489",
"publishedAt": "2017-07-23T11:19:25Z"
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/multiple_commits_after_review_reviews.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"author": {
"login": "foo"
},
"commit": {"oid": "deadbeef"},
"authorCanPushToRepository": true,
"url": "https://github.com/nodejs/node/pull/16438#pullrequestreview-89923489",
"publishedAt": "2017-09-23T11:19:25Z"
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/reviews_approved.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"author": {
"login": "foo"
},
"commit": {"oid": "deadbeef"},
"authorCanPushToRepository": true,
"url": "https://github.com/nodejs/node/pull/16438#pullrequestreview-71480624",
"publishedAt": "2017-10-24T11:19:00Z"
Expand All @@ -14,6 +15,7 @@
"author": {
"login": "Baz"
},
"commit": {"oid": "deadbeef"},
"authorCanPushToRepository": true,
"url": "https://github.com/nodejs/node/pull/16438#pullrequestreview-71488392",
"publishedAt": "2017-10-24T11:50:52Z"
Expand All @@ -24,6 +26,7 @@
"author": {
"login": "Baz"
},
"commit": {"oid": "deadbeef"},
"authorCanPushToRepository": true,
"url": "https://github.com/nodejs/node/pull/16438#pullrequestreview-714882992",
"publishedAt": "2017-10-24T12:30:52Z"
Expand All @@ -34,6 +37,7 @@
"author": {
"login": "Quux"
},
"commit": {"oid": "deadbeef"},
"authorCanPushToRepository": true,
"url": "https://github.com/nodejs/node/pull/16438#pullrequestreview-71817236",
"publishedAt": "2017-10-24T14:49:01Z"
Expand All @@ -44,6 +48,7 @@
"author": {
"login": "Baz"
},
"commit": {"oid": "deadbeef"},
"authorCanPushToRepository": true,
"url": "https://github.com/nodejs/node/pull/16438#pullrequestreview-71488236",
"publishedAt": "2017-10-24T14:49:02Z"
Expand All @@ -54,6 +59,7 @@
"author": {
"login": "Quo"
},
"commit": {"oid": "deadbeef"},
"authorCanPushToRepository": true,
"url": "https://github.com/nodejs/node/pull/16438#pullrequestreview-71817236",
"publishedAt": "2017-10-24T19:09:52Z"
Expand All @@ -64,6 +70,7 @@
"author": {
"login": "bot"
},
"commit": {"oid": "ffdef335209c77f66d933bd873950747bfe42264"},
"authorCanPushToRepository": true,
"url": "https://github.com/nodejs/node/pull/16438#pullrequestreview-71839232",
"publishedAt": "2017-10-28T19:21:52Z"
Expand Down
3 changes: 0 additions & 3 deletions test/fixtures/semver_major_pr.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
}
]
},
"timelineItems": {
"updatedAt": "2017-10-24T11:13:43Z"
},
"title": "lib: awesome changes",
"baseRefName": "main",
"headRefName": "awesome-changes"
Expand Down
Loading
Loading