From a4a3dab4978af0162e671f340a1c6f08edd460fe Mon Sep 17 00:00:00 2001 From: Andrea Brancaleoni Date: Tue, 7 May 2024 15:18:03 +0200 Subject: [PATCH] action.yml: refactor nudgeUnverifiedCommits --- action.yml | 71 ++----------------------------- src/nudgeUnverifiedCommits.js | 78 +++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 67 deletions(-) create mode 100644 src/nudgeUnverifiedCommits.js diff --git a/action.yml b/action.yml index 8f27ebc6..0c6a15e6 100644 --- a/action.yml +++ b/action.yml @@ -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`) @@ -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: diff --git a/src/nudgeUnverifiedCommits.js b/src/nudgeUnverifiedCommits.js new file mode 100644 index 00000000..35eb41f2 --- /dev/null +++ b/src/nudgeUnverifiedCommits.js @@ -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}); + } + } +} \ No newline at end of file