|
| 1 | +name: Comment on pull request |
| 2 | +on: |
| 3 | + workflow_run: |
| 4 | + workflows: [CI] |
| 5 | + types: [completed] |
| 6 | +jobs: |
| 7 | + pr_comment: |
| 8 | + if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' |
| 9 | + runs-on: ubuntu-latest |
| 10 | + steps: |
| 11 | + - uses: actions/github-script@v6 |
| 12 | + with: |
| 13 | + # This snippet is public-domain, taken from |
| 14 | + # https://github.com/oprypin/nightly.link/blob/master/.github/workflows/pr-comment.yml |
| 15 | + script: | |
| 16 | + async function upsertComment(owner, repo, issue_number, purpose, body) { |
| 17 | + const {data: comments} = await github.rest.issues.listComments( |
| 18 | + {owner, repo, issue_number}); |
| 19 | +
|
| 20 | + const marker = `<!-- bot: ${purpose} -->`; |
| 21 | + body = marker + "\n" + body; |
| 22 | +
|
| 23 | + const existing = comments.filter((c) => c.body.includes(marker)); |
| 24 | + if (existing.length > 0) { |
| 25 | + const last = existing[existing.length - 1]; |
| 26 | + core.info(`Updating comment ${last.id}`); |
| 27 | + await github.rest.issues.updateComment({ |
| 28 | + owner, repo, |
| 29 | + body, |
| 30 | + comment_id: last.id, |
| 31 | + }); |
| 32 | + } else { |
| 33 | + core.info(`Creating a comment in issue / PR #${issue_number}`); |
| 34 | + await github.rest.issues.createComment({issue_number, body, owner, repo}); |
| 35 | + } |
| 36 | + } |
| 37 | +
|
| 38 | + const {owner, repo} = context.repo; |
| 39 | + const run_id = ${{github.event.workflow_run.id}}; |
| 40 | +
|
| 41 | + const pull_requests = ${{ toJSON(github.event.workflow_run.pull_requests) }}; |
| 42 | + if (!pull_requests.length) { |
| 43 | + return core.error("This workflow doesn't match any pull requests!"); |
| 44 | + } |
| 45 | +
|
| 46 | + const artifacts = await github.paginate( |
| 47 | + github.rest.actions.listWorkflowRunArtifacts, {owner, repo, run_id}); |
| 48 | + if (!artifacts.length) { |
| 49 | + return core.error(`No artifacts found`); |
| 50 | + } |
| 51 | + let body = `Download the artifacts for this pull request:\n`; |
| 52 | + for (const art of artifacts) { |
| 53 | + body += `\n* [${art.name}.zip](https://nightly.link/${owner}/${repo}/actions/artifacts/${art.id}.zip)`; |
| 54 | + } |
| 55 | +
|
| 56 | + core.info("Review thread message body:", body); |
| 57 | +
|
| 58 | + for (const pr of pull_requests) { |
| 59 | + await upsertComment(owner, repo, pr.number, |
| 60 | + "nightly-link", body); |
| 61 | + } |
0 commit comments