@@ -179,6 +179,9 @@ This is the **official GitHub-recommended best practice** for writing PR comment
179179fork PRs. It uses the [`workflow_run`](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_run)
180180event 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
231234name : Commit Check Comment
232235
233236on :
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
238241jobs :
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`)
0 commit comments