7efc677bcd7bba144fae4f313c13d73296be2caa

Author
Ayman Bagabas <ayman.bagabas@gmail.com>
Committer
Ayman Bagabas <ayman.bagabas@gmail.com>
Date

Message

Add builds commenter bot

Diff

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