Skip to content

Commit 5d1a760

Browse files
committed
feat(ncu-ci): parse comments to find a safe commit
1 parent ef135f5 commit 5d1a760

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/ci/run_ci.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ export const CI_PR_URL = `https://${CI_DOMAIN}/job/${CI_PR_NAME}/build`;
1616
const CI_V8_NAME = CI_TYPES.get(CI_TYPES_KEYS.V8).jobName;
1717
export const CI_V8_URL = `https://${CI_DOMAIN}/job/${CI_V8_NAME}/build`;
1818

19+
const REQUEST_CI_COMMENT = /^@nodejs-github-bot .+([0-9a-f]{40})$/;
20+
21+
async function findSafeFullCommitSHA(cli, prData, request) {
22+
// First, let's check if the head was approved.
23+
await Promise.all([prData.getReviews(), prData.getCommits()]);
24+
25+
const approvedHead = new PRChecker(cli, prData, request, {}).getApprovedTipOfHead();
26+
if (approvedHead) return approvedHead;
27+
28+
// Else, let's find out if a collaborator added a comment with a full commit SHA.
29+
await Promise.all([prData.getCollaborators(), prData.getComments()]);
30+
const collaborators = Array.from(prData.collaborators.values(),
31+
(c) => c.login.toLowerCase());
32+
return prData.comments
33+
.findLast(c =>
34+
collaborators.includes(c.author.login.toLowerCase()) &&
35+
REQUEST_CI_COMMENT.test(c.body))
36+
?.body.match(REQUEST_CI_COMMENT)[1];
37+
}
38+
1939
export class RunPRJob {
2040
constructor(cli, request, owner, repo, prid, certifySafe) {
2141
this.cli = cli;
@@ -26,9 +46,7 @@ export class RunPRJob {
2646
this.prData = new PRData({ prid, owner, repo }, cli, request);
2747
this.certifySafe =
2848
certifySafe ||
29-
Promise.all([this.prData.getReviews(), this.prData.getCommits()]).then(() =>
30-
(this.certifySafe = new PRChecker(cli, this.prData, request, {}).getApprovedTipOfHead())
31-
);
49+
findSafeFullCommitSHA(cli, this.prData, request).then((h) => (this.certifySafe = h));
3250
}
3351

3452
async getCrumb() {
@@ -72,6 +90,8 @@ export class RunPRJob {
7290
const { cli, certifySafe } = this;
7391

7492
if (!(await certifySafe)) {
93+
cli.info('If the tip of this PR looks safe, add a comment in the form of:');
94+
cli.info('@nodejs-github-bot test <commit-full-sha-or-URL>');
7595
cli.error('Refusing to run CI on potentially unsafe PR');
7696
return false;
7797
}

lib/queries/PRComments.gql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ query Comments($prid: Int!, $owner: String!, $repo: String!, $after: String) {
99
}
1010
nodes {
1111
publishedAt
12+
body
1213
bodyText
1314
author {
1415
login

0 commit comments

Comments
 (0)