-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Colin Rotherham <[email protected]>
- Loading branch information
1 parent
3335d78
commit 1183bd8
Showing
7 changed files
with
439 additions
and
19 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,58 @@ | ||
/** | ||
* @param {object} githubContext - Github Action context | ||
* @param {import('@octokit/rest').Octokit} githubContext.github | ||
* @param {import('@actions/github').context} githubContext.context | ||
* @param {string} markerText - A unique marker used to identify the correct comment | ||
* @param {string} bodyText - The body of the comment | ||
*/ | ||
export async function commentOnPr ({ github, context }, markerText, bodyText) { | ||
if (!context.payload.pull_request) { | ||
throw new Error('Workflow "comment on PR" requires a PR') | ||
} | ||
|
||
const marker = `<!-- ${markerText} -->\n` | ||
const sha = `\nSHA: ${context.sha}` | ||
const body = marker + bodyText + sha | ||
|
||
let commentId | ||
|
||
/** | ||
* @satisfies {IssueCommentsListParams} | ||
*/ | ||
const issueParameters = { | ||
issue_number: context.payload.pull_request.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo | ||
} | ||
|
||
/** | ||
* Finds the first issue comment for which the `matcher` function returns `true` | ||
* | ||
* {@link https://github.com/peter-evans/find-comment/blob/main/src/find.ts} | ||
*/ | ||
for await (const { data: comments } of github.paginate.iterator( | ||
github.rest.issues.listComments, | ||
issueParameters | ||
)) { | ||
const comment = comments.find((comment) => !!comment.body?.includes(marker)) | ||
|
||
if (comment) { | ||
commentId = comment.id | ||
} | ||
} | ||
|
||
/** | ||
* Create GitHub issue comment | ||
* | ||
* Updates existing commentby ID if available | ||
*/ | ||
await (!commentId | ||
? github.rest.issues.createComment({ ...issueParameters, body }) | ||
: github.rest.issues.updateComment({ ...issueParameters, body, comment_id: commentId })) | ||
} | ||
|
||
/** | ||
* @typedef {import('@octokit/plugin-rest-endpoint-methods').RestEndpointMethodTypes["issues"]} IssuesEndpoint | ||
* @typedef {IssuesEndpoint["updateComment"]["parameters"]} IssueCommentUpdateParams | ||
* @typedef {IssuesEndpoint["listComments"]["parameters"]} IssueCommentsListParams | ||
*/ |
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,14 @@ | ||
{ | ||
"private": true, | ||
"name": "govuk-frontend-workflow-scripts", | ||
"description": "GOV.UK Frontend GitHub Actions workflow scripts", | ||
"engines": { | ||
"node": "^18.12.0", | ||
"npm": "^8.1.0 || ^9.1.0" | ||
}, | ||
"license": "MIT", | ||
"optionalDependencies": { | ||
"@actions/github": "^5.1.1", | ||
"@octokit/rest": "19.0.11" | ||
} | ||
} |
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,7 @@ | ||
{ | ||
"extends": "../../../tsconfig.base.json", | ||
"include": ["**/*.mjs"], | ||
"compilerOptions": { | ||
"strict": true | ||
} | ||
} |
Oops, something went wrong.