Skip to content

Commit 743446b

Browse files
authored
Use workflow runs to publish comments (#2000)
1 parent da64416 commit 743446b

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed

.github/workflows/artifacts.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Artifacts
2+
on:
3+
workflow_run:
4+
workflows: ["CI"]
5+
types:
6+
- completed
7+
8+
permissions:
9+
pull-requests: write
10+
11+
jobs:
12+
notify:
13+
runs-on: ubuntu-latest
14+
if: ${{ github.event.workflow_run.event == 'pull_request' }}
15+
steps:
16+
- uses: actions/github-script@v7
17+
with:
18+
script: |
19+
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
20+
owner: context.repo.owner,
21+
repo: context.repo.repo,
22+
run_id: context.payload.workflow_run.id
23+
});
24+
25+
if (artifacts.data.total_count !== 1) {
26+
throw new Error('Expected one artifact')
27+
}
28+
29+
const artifactUrl = artifacts.data.artifacts[0].archive_download_url;
30+
const commentBody = `<!-- build-artifact-comment -->\n📦 Docs artifacts are ready: ${artifactUrl}`;
31+
32+
const comments = await github.rest.issues.listComments({
33+
owner: context.repo.owner,
34+
repo: context.repo.repo,
35+
issue_number: context.issue.number,
36+
});
37+
38+
const botComment = comments.data.find(comment =>
39+
comment.user.type === 'Bot' &&
40+
comment.body.includes('<!-- build-artifact-comment -->')
41+
);
42+
43+
if (botComment) {
44+
await github.rest.issues.updateComment({
45+
owner: context.repo.owner,
46+
repo: context.repo.repo,
47+
comment_id: botComment.id,
48+
body: commentBody
49+
});
50+
} else {
51+
await github.rest.issues.createComment({
52+
owner: context.repo.owner,
53+
repo: context.repo.repo,
54+
issue_number: context.issue.number,
55+
body: commentBody
56+
});
57+
}

.github/workflows/ci.yml

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ name: CI
22

33
on: [pull_request, push]
44

5-
permissions:
6-
checks: write
7-
85
env:
96
MIX_ENV: test
107

@@ -41,7 +38,7 @@ jobs:
4138

4239
- run: mix deps.get
4340

44-
# Generate docs artifact
41+
# Generate and upload artifacts
4542
- name: Generate docs
4643
run: |
4744
mix build
@@ -54,30 +51,6 @@ jobs:
5451
with:
5552
name: docs
5653
path: doc/
57-
overwrite: true
58-
59-
- name: Docs report
60-
uses: actions/github-script@v7
61-
continue-on-error: true
62-
env:
63-
ARTIFACT_ID: ${{ steps.docs-upload.outputs.artifact-id }}
64-
with:
65-
script: |
66-
const artifactUrl = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts/${process.env.ARTIFACT_ID}`;
67-
68-
await github.rest.checks.create({
69-
owner: context.repo.owner,
70-
repo: context.repo.repo,
71-
name: 'Docs artifacts',
72-
head_sha: context.sha,
73-
status: 'completed',
74-
conclusion: 'success',
75-
output: {
76-
title: '',
77-
summary: '',
78-
text: artifactUrl
79-
}
80-
});
8154

8255
# Test Elixir
8356
- run: mix format --check-formatted

0 commit comments

Comments
 (0)