-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
name: "Security Action (unverified commits)" | ||
description: "Check if there are unverified commits in the PR" | ||
inputs: | ||
github_token: | ||
description: | | ||
Secret token to push review comments, and | ||
interact with the repository systematically | ||
required: true | ||
runs: | ||
using: "composite" | ||
steps: | ||
- id: unverified-commits | ||
if: ${{ github.event_name == 'pull_request' }} | ||
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}); | ||
} | ||
} | ||
- uses: actions-ecosystem/action-add-labels@18f1af5e3544586314bbe15c0273249c770b2daf # v1.1.3 | ||
if: ${{ steps.unverified-commits.outputs.result == '"UNVERIFIED-CHANGED"' }} | ||
with: | ||
github_token: ${{ inputs.github_token }} | ||
labels: unverified-commits |