Skip to content

Commit

Permalink
Add comment on PR workflow
Browse files Browse the repository at this point in the history
Co-authored-by: Colin Rotherham <[email protected]>
  • Loading branch information
domoscargin and colinrotherham committed May 22, 2023
1 parent 3335d78 commit 1183bd8
Show file tree
Hide file tree
Showing 7 changed files with 439 additions and 19 deletions.
23 changes: 9 additions & 14 deletions .github/workflows/diff-change-to-dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ jobs:
generate-diff:
name: Generate Diff
runs-on: ubuntu-latest
# Abort if run from a fork, as token won't have write access to post comment
if: github.event.pull_request.head.repo.full_name == github.repository

# Skip when token write permissions are unavailable on forks
if: ${{ !github.event.pull_request.head.repo.fork }}

steps:
- uses: actions/checkout@v3
with:
Expand Down Expand Up @@ -49,19 +51,12 @@ jobs:
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const fs = require('fs').promises
const diff = await fs.readFile(
process.env.GITHUB_WORKSPACE + '/dist.diff', 'utf8'
)
const { readFile } = require('node:fs/promises')
const diff = await readFile('${{ github.workspace }}/dist.diff', 'utf8')
const commentText = '## Changes to dist\n' +
const { commentOnPr } = await import('${{ github.workspace }}/.github/workflows/scripts/comments.mjs')
await commentOnPr({ github, context }, 'Diff release', '## Changes to dist\n' +
'```diff\n' +
diff +
'\n```'
github.rest.issues.createComment({
issue_number: context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentText
})
)
58 changes: 58 additions & 0 deletions .github/workflows/scripts/comments.mjs
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
*/
14 changes: 14 additions & 0 deletions .github/workflows/scripts/package.json
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"
}
}
7 changes: 7 additions & 0 deletions .github/workflows/scripts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../../tsconfig.base.json",
"include": ["**/*.mjs"],
"compilerOptions": {
"strict": true
}
}
Loading

0 comments on commit 1183bd8

Please sign in to comment.