-
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.
action.yml: refactor nudgeUnverifiedCommits
- Loading branch information
Showing
2 changed files
with
82 additions
and
67 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
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}); | ||
} | ||
} | ||
} |