Skip to content

Commit

Permalink
action.yml: unverifiedCommits refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
thypon committed May 7, 2024
1 parent 1b28bce commit b0c58d2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,16 @@ runs:
const number = commentsNumber({context, github})
cleanupComments({context, github})
return number
- id: comments-before2
if: ${{ steps.reviewdog-enabled-pr.outputs.result == 'true' }}
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const actionPath = '${{ github.action_path }}'
const unverifiedCommits = require(`${actionPath}/src/unverifiedCommits.js`)
return unverifiedCommits({context, github})
- id: unverified-commits
if: ${{ github.event_name == 'pull_request' }}
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
Expand Down Expand Up @@ -206,6 +215,7 @@ runs:
await github.graphql(deleteMutation, {comment: comment.id});
}
}
- if: ${{ steps.reviewdog-enabled.outputs.result == 'true' }}
uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
Expand Down
72 changes: 72 additions & 0 deletions src/unverifiedCommits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
export default async function unverifiedCommits ({
github,
githubToken,
context
}) {
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
let 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
})
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 b0c58d2

Please sign in to comment.