Skip to content

Commit cdf05b8

Browse files
Copilotalexwolson
andauthored
Decouple Pa11y PR commenting from forked pull_request token permissions (#122)
* Initial plan * Add workflow_run commenter for fork PR Pa11y reports Agent-Logs-Url: https://github.com/CivicTechTO/civictech.ca/sessions/2a3ee380-6b3d-47e8-bb52-3aa6e9ffe680 Co-authored-by: alexwolson <8996640+alexwolson@users.noreply.github.com> * Harden fork PR Pa11y comment workflow error handling Agent-Logs-Url: https://github.com/CivicTechTO/civictech.ca/sessions/2a3ee380-6b3d-47e8-bb52-3aa6e9ffe680 Co-authored-by: alexwolson <8996640+alexwolson@users.noreply.github.com> * Refine fork comment workflow conditions and env reuse Agent-Logs-Url: https://github.com/CivicTechTO/civictech.ca/sessions/2a3ee380-6b3d-47e8-bb52-3aa6e9ffe680 Co-authored-by: alexwolson <8996640+alexwolson@users.noreply.github.com> * Improve fork Pa11y comment workflow guards and diagnostics Agent-Logs-Url: https://github.com/CivicTechTO/civictech.ca/sessions/2a3ee380-6b3d-47e8-bb52-3aa6e9ffe680 Co-authored-by: alexwolson <8996640+alexwolson@users.noreply.github.com> * Validate PR number handling in fork comment workflow Agent-Logs-Url: https://github.com/CivicTechTO/civictech.ca/sessions/2a3ee380-6b3d-47e8-bb52-3aa6e9ffe680 Co-authored-by: alexwolson <8996640+alexwolson@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: alexwolson <8996640+alexwolson@users.noreply.github.com>
1 parent 2bcc837 commit cdf05b8

1 file changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: Comment Pa11y report on fork PRs
2+
3+
on:
4+
workflow_run:
5+
workflows: ['CI']
6+
types:
7+
- completed
8+
9+
permissions:
10+
actions: read
11+
contents: read
12+
issues: write
13+
14+
jobs:
15+
comment:
16+
name: Post Pa11y report comment
17+
runs-on: ubuntu-latest
18+
env:
19+
REPORT_DIR: /tmp/pa11y-report
20+
if: >-
21+
github.event.workflow_run.event == 'pull_request' &&
22+
github.event.workflow_run.head_repository.fork &&
23+
github.event.workflow_run.conclusion != 'cancelled' &&
24+
github.event.workflow_run.pull_requests[0] != null
25+
26+
steps:
27+
- name: Download Pa11y report artifact from CI run
28+
id: download
29+
uses: actions/download-artifact@v4
30+
continue-on-error: true
31+
with:
32+
name: pa11y-report
33+
run-id: ${{ github.event.workflow_run.id }}
34+
path: ${{ env.REPORT_DIR }}
35+
36+
- name: Check report file
37+
id: report
38+
run: |
39+
report_file="$REPORT_DIR/pa11y_report.md"
40+
if [ -f "$report_file" ]; then
41+
echo "exists=true" >> "$GITHUB_OUTPUT"
42+
echo "report_file=$report_file" >> "$GITHUB_OUTPUT"
43+
else
44+
echo "exists=false" >> "$GITHUB_OUTPUT"
45+
if [ "${{ steps.download.outcome }}" = "failure" ]; then
46+
echo "::warning::Failed to download Pa11y report artifact from CI run ${{ github.event.workflow_run.id }}. Skipping comment."
47+
else
48+
echo "::warning::Pa11y report artifact was not uploaded by CI run ${{ github.event.workflow_run.id }}. Skipping comment."
49+
fi
50+
fi
51+
52+
- name: Post or update PR comment
53+
if: steps.report.outputs.exists == 'true'
54+
uses: actions/github-script@v8
55+
env:
56+
REPORT_BODY_FILE: ${{ steps.report.outputs.report_file }}
57+
PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }}
58+
with:
59+
script: |
60+
const fs = require('fs');
61+
const marker = '<!-- pa11y-report -->';
62+
const issue_number = Number(process.env.PR_NUMBER);
63+
if (!Number.isInteger(issue_number) || issue_number <= 0) {
64+
core.setFailed(`Invalid PR number: ${process.env.PR_NUMBER}`);
65+
return;
66+
}
67+
68+
let body;
69+
try {
70+
body = fs.readFileSync(process.env.REPORT_BODY_FILE, 'utf8');
71+
} catch (error) {
72+
core.setFailed(`Unable to read report file: ${error.message}`);
73+
return;
74+
}
75+
76+
const comments = await github.rest.issues.listComments({
77+
owner: context.repo.owner,
78+
repo: context.repo.repo,
79+
issue_number,
80+
});
81+
82+
const existing = comments.data.find(c => c.body && c.body.includes(marker));
83+
84+
if (existing) {
85+
await github.rest.issues.updateComment({
86+
owner: context.repo.owner,
87+
repo: context.repo.repo,
88+
comment_id: existing.id,
89+
body,
90+
});
91+
core.info(`Updated existing comment ${existing.id}`);
92+
} else {
93+
await github.rest.issues.createComment({
94+
owner: context.repo.owner,
95+
repo: context.repo.repo,
96+
issue_number,
97+
body,
98+
});
99+
core.info('Created new comment');
100+
}

0 commit comments

Comments
 (0)