Skip to content

Commit 294ddde

Browse files
committed
docs: move full workflow_run examples to examples/ dir, keep concise snippets in README
1 parent 4d64e46 commit 294ddde

3 files changed

Lines changed: 116 additions & 35 deletions

File tree

README.md

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ This is the **official GitHub-recommended best practice** for writing PR comment
179179
fork PRs. It uses the [`workflow_run`](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_run)
180180
event with **no security risks**.
181181

182+
> 📁 Ready-to-use files: [`examples/commit-check-workflow-a.yml`](examples/commit-check-workflow-a.yml)
183+
> and [`examples/commit-check-workflow-b.yml`](examples/commit-check-workflow-b.yml)
184+
182185
**How it works:**
183186

184187
```
@@ -215,77 +218,54 @@ jobs:
215218
with:
216219
message: true
217220
branch: true
218-
pr-comments: false # comments handled by Workflow B
221+
pr-comments: false # comments handled by Workflow B
219222
job-summary: true
220-
221-
# Save results so Workflow B can post a PR comment
222223
- uses: actions/upload-artifact@v4
223224
with:
224225
name: commit-check-result-${{ github.event.number }}
225-
path: result.txt
226+
path: result.txt # saved for Workflow B
226227
```
227228

229+
> 📄 Full file: [`examples/commit-check-workflow-a.yml`](examples/commit-check-workflow-a.yml)
230+
228231
**Workflow B**`.github/workflows/commit-check-comment.yml` (triggered by `workflow_run`):
229232

230233
```yaml
231234
name: Commit Check Comment
232235

233236
on:
234237
workflow_run:
235-
workflows: ["Commit Check"] # must match Workflow A's name exactly
238+
workflows: ["Commit Check"] # must match Workflow A's name exactly
236239
types: [completed]
237240

238241
jobs:
239242
comment:
240243
runs-on: ubuntu-latest
241244
permissions:
242245
pull-requests: write
243-
actions: read # needed to download artifacts
246+
actions: read # needed to download artifacts
244247
steps:
245248
- uses: actions/download-artifact@v4
246249
with:
247250
name: commit-check-result-${{ github.event.workflow_run.pull_requests[0].number }}
248251
run-id: ${{ github.event.workflow_run.id }}
249252
github-token: ${{ github.token }}
250-
251253
- name: Read result and post PR comment
252254
uses: actions/github-script@v7
253255
with:
254256
script: |
257+
// See examples/commit-check-workflow-b.yml for full script
255258
const fs = require('fs');
256259
const prNumber = ${{ github.event.workflow_run.pull_requests[0].number }};
257260
const resultText = fs.readFileSync('result.txt', 'utf8').trim();
258-
259-
const successTitle = '# Commit-Check ✔️';
260-
const failureTitle = '# Commit-Check ❌';
261261
const body = resultText
262-
? `${failureTitle}\n\`\`\`\n${resultText}\n\`\`\``
263-
: successTitle;
264-
265-
const { data: comments } = await github.rest.issues.listComments({
266-
...context.repo,
267-
issue_number: prNumber,
268-
});
269-
270-
const existing = comments.find(c =>
271-
c.body.startsWith(successTitle) || c.body.startsWith(failureTitle)
272-
);
273-
274-
if (existing) {
275-
await github.rest.issues.updateComment({
276-
...context.repo,
277-
comment_id: existing.id,
278-
body,
279-
});
280-
} else {
281-
await github.rest.issues.createComment({
282-
...context.repo,
283-
issue_number: prNumber,
284-
body,
285-
});
286-
}
262+
? '# Commit-Check ❌\n```\n' + resultText + '\n```'
263+
: '# Commit-Check ✔️';
264+
// Creates or updates the matching PR comment
287265
```
288266
267+
> 📄 Full file: [`examples/commit-check-workflow-b.yml`](examples/commit-check-workflow-b.yml)
268+
289269
> **Key security benefits:**
290270
> - Workflow B runs in the **base repository's context**, so `GITHUB_TOKEN` has full write
291271
> permissions (you explicitly grant `pull-requests: write`)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Workflow A: Run commit checks on pull_request events.
2+
#
3+
# This workflow is triggered by pull_request and runs commit checks.
4+
# It uploads the result as an artifact so Workflow B (commit-check-comment.yml)
5+
# can read it and post a PR comment with full write permissions.
6+
#
7+
# See https://github.com/commit-check/commit-check-action#fork-pr-comments
8+
9+
name: Commit Check
10+
11+
on:
12+
pull_request:
13+
branches: ["main"]
14+
15+
jobs:
16+
check:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v5
20+
with:
21+
fetch-depth: 0
22+
- uses: commit-check/commit-check-action@v2
23+
with:
24+
message: true
25+
branch: true
26+
pr-comments: false # comments handled by Workflow B
27+
job-summary: true
28+
29+
# Save results so Workflow B can post a PR comment
30+
- uses: actions/upload-artifact@v4
31+
with:
32+
name: commit-check-result-${{ github.event.number }}
33+
path: result.txt
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Workflow B: Post PR comment after commit checks complete.
2+
#
3+
# This workflow is triggered by the workflow_run event from Workflow A.
4+
# It runs in the base repository's context with full write permissions,
5+
# making it safe for fork PRs (no checkout of fork code).
6+
#
7+
# Prerequisites:
8+
# - Workflow A (commit-check.yml) must exist and upload an artifact named
9+
# commit-check-result-<PR-number> containing result.txt
10+
#
11+
# See https://github.com/commit-check/commit-check-action#fork-pr-comments
12+
13+
name: Commit Check Comment
14+
15+
on:
16+
workflow_run:
17+
workflows: ["Commit Check"] # must match Workflow A's name exactly
18+
types: [completed]
19+
20+
jobs:
21+
comment:
22+
runs-on: ubuntu-latest
23+
permissions:
24+
pull-requests: write
25+
actions: read # needed to download artifacts
26+
steps:
27+
- uses: actions/download-artifact@v4
28+
with:
29+
name: commit-check-result-${{ github.event.workflow_run.pull_requests[0].number }}
30+
run-id: ${{ github.event.workflow_run.id }}
31+
github-token: ${{ github.token }}
32+
33+
- name: Read result and post PR comment
34+
uses: actions/github-script@v7
35+
with:
36+
script: |
37+
const fs = require('fs');
38+
const prNumber = ${{ github.event.workflow_run.pull_requests[0].number }};
39+
const resultText = fs.readFileSync('result.txt', 'utf8').trim();
40+
41+
const successTitle = '# Commit-Check ✔️';
42+
const failureTitle = '# Commit-Check ❌';
43+
const body = resultText
44+
? `${failureTitle}\n\`\`\`\n${resultText}\n\`\`\``
45+
: successTitle;
46+
47+
const { data: comments } = await github.rest.issues.listComments({
48+
...context.repo,
49+
issue_number: prNumber,
50+
});
51+
52+
const existing = comments.find(c =>
53+
c.body.startsWith(successTitle) || c.body.startsWith(failureTitle)
54+
);
55+
56+
if (existing) {
57+
await github.rest.issues.updateComment({
58+
...context.repo,
59+
comment_id: existing.id,
60+
body,
61+
});
62+
} else {
63+
await github.rest.issues.createComment({
64+
...context.repo,
65+
issue_number: prNumber,
66+
body,
67+
});
68+
}

0 commit comments

Comments
 (0)