Skip to content

Commit

Permalink
action.yml: refactor nudgeUnverifiedCommits
Browse files Browse the repository at this point in the history
  • Loading branch information
thypon committed May 7, 2024
1 parent 1e4ef4e commit a4a3dab
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 67 deletions.
71 changes: 4 additions & 67 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ runs:
with:
script: |
const actionPath = '${{ github.action_path }}'
const commentsNumber = require(`${actionPath}/src/commentsNumber.js`)
const cleanupComments = require(`${actionPath}/src/cleanupComments.js`)
Expand All @@ -140,72 +140,9 @@ runs:
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const commits = await github.rest.pulls.listCommits({
pull_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
});
const previousCommentsQuery = `query ($owner: String!, $name: String!, $prnumber: Int!) {
repository(owner: $owner, name: $name) {
pullRequest(number: $prnumber) {
comments(last: 50) {
nodes {
id
author {
login
}
body
}
}
}
}
}`;
const deleteMutation = `mutation($comment:ID!) {
deleteIssueComment(input: {id:$comment}) {
clientMutationId
}
}`;
const previousComments = await github.graphql(previousCommentsQuery, {
owner: context.repo.owner,
name: context.repo.repo,
prnumber: context.issue.number
});
const commentPrefix = "The following commits were not [verified](https://github.com/brave/handbook/blob/master/development/commit-and-tag-signing.md):\n";
const actionPreviousComments = previousComments.repository.pullRequest.comments.nodes.filter(
c => c.author.login === "github-actions" && c.body.startsWith(commentPrefix)
);
const unverifiedCommits = commits.data.filter(c => c.commit.verification.verified !== true);
if (unverifiedCommits.length) {
const commitList = unverifiedCommits.map(c => `${c.sha} (${c.commit.verification.reason})`).join("\n");
const body = commentPrefix + commitList;
var commentExists = false;
for (const comment of actionPreviousComments) {
if (comment.body == body) {
console.log("Good comment found:", comment);
commentExists = true;
} else {
console.log("Deleting", comment);
await github.graphql(deleteMutation, {comment: comment.id});
}
}
if (!commentExists) {
console.log("Creating new comment");
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body,
});
return "UNVERIFIED-CHANGED"; // A new comment was created
}
return "UNVERIFIED";
} else {
console.log("Commits verified");
for (const comment of actionPreviousComments) {
console.log("Deleting", comment);
await github.graphql(deleteMutation, {comment: comment.id});
}
}
const actionPath = '${{ github.action_path }}'
const nudgeUnverifiedCommits = require(`${actionPath}/src/nudgeUnverifiedCommits.js`)
return nudgeUnverifiedCommits({context, github})
- if: ${{ steps.reviewdog-enabled.outputs.result == 'true' }}
uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
Expand Down
78 changes: 78 additions & 0 deletions src/nudgeUnverifiedCommits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
export default async function nudgeUnverifiedCommits({
github,
githubToken,
context
}) {
if (!github && githubToken) {
const { Octokit } = await import("octokit");

github = new Octokit({ auth: githubToken })
}

const commits = await github.rest.pulls.listCommits({
pull_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
});
const previousCommentsQuery = `query ($owner: String!, $name: String!, $prnumber: Int!) {
repository(owner: $owner, name: $name) {
pullRequest(number: $prnumber) {
comments(last: 50) {
nodes {
id
author {
login
}
body
}
}
}
}
}`;
const deleteMutation = `mutation($comment:ID!) {
deleteIssueComment(input: {id:$comment}) {
clientMutationId
}
}`;
const previousComments = await github.graphql(previousCommentsQuery, {
owner: context.repo.owner,
name: context.repo.repo,
prnumber: context.issue.number
});
const commentPrefix = "The following commits were not [verified](https://github.com/brave/handbook/blob/master/development/commit-and-tag-signing.md):\n";
const actionPreviousComments = previousComments.repository.pullRequest.comments.nodes.filter(
c => c.author.login === "github-actions" && c.body.startsWith(commentPrefix)
);
const unverifiedCommits = commits.data.filter(c => c.commit.verification.verified !== true);
if (unverifiedCommits.length) {
const commitList = unverifiedCommits.map(c => `${c.sha} (${c.commit.verification.reason})`).join("\n");
const body = commentPrefix + commitList;
var commentExists = false;
for (const comment of actionPreviousComments) {
if (comment.body == body) {
console.log("Good comment found:", comment);
commentExists = true;
} else {
console.log("Deleting", comment);
await github.graphql(deleteMutation, {comment: comment.id});
}
}
if (!commentExists) {
console.log("Creating new comment");
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body,
});
return "UNVERIFIED-CHANGED"; // A new comment was created
}
return "UNVERIFIED";
} else {
console.log("Commits verified");
for (const comment of actionPreviousComments) {
console.log("Deleting", comment);
await github.graphql(deleteMutation, {comment: comment.id});
}
}
}

0 comments on commit a4a3dab

Please sign in to comment.