1name: Comment on pull request
2on:
3 workflow_run:
4 workflows: [build]
5 types: [completed]
6jobs:
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@v5
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 const sha = "${{github.event.workflow_run.head_sha}}";
52 let body = `Download the artifacts for this pull request:\n`;
53 for (const art of artifacts) {
54 body += `\n* [${art.name}.zip](https://nightly.link/${owner}/${repo}/actions/artifacts/${art.id}.zip)`;
55 }
56 body += `\n\nOr use the following Docker image \`ghcr.io/${owner}/${repo}:devel-${sha.substr(0,7)}\``;
57
58 core.info("Review thread message body:", body);
59
60 for (const pr of pull_requests) {
61 await upsertComment(owner, repo, pr.number,
62 "nightly-link", body);
63 }