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