Skip to content

Commit 42099f3

Browse files
authored
Fix permission issue in forks for coverage comment (#148)
## Problem When working with forks, the "Post Coverage Comment" fails due to the following error: Error: Resource not accessible by integration ## Diagnosis When a Pull Request is opened from a fork (which is common in open source), GitHub generates a `GITHUB_TOKEN` with read-only permissions for the target repository. This is a security measure to prevent malicious code in a fork from modifying your repository or stealing secrets. Even though you specified `permissions: pull-requests: write` in your YAML, GitHub ignores this elevation request for forks. Consequently, the action `marocchino/sticky-pull-request-comment` fails because it cannot write to the PR. ## Solution To fix this securely, the workflow must be split into two separate workflows: * The test workflow (the current `ci.yml`) * The comment workflow (new) The latter runs automatically *after* the test workflow finishes. Because this runs in your repo's context (not the fork's), it has write permissions and can download the artifact to post the comment.
1 parent a69ae3e commit 42099f3

3 files changed

Lines changed: 49 additions & 5 deletions

File tree

.github/workflows/ci.yml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,19 @@ jobs:
159159
echo "\`\`\`" >> coverage_comment.txt
160160
echo "</details>" >> coverage_comment.txt
161161
162-
- name: Post Coverage Comment
162+
- name: Save PR Number
163163
if: success() && github.event_name == 'pull_request' && matrix.python-version == '3.12'
164-
uses: marocchino/sticky-pull-request-comment@v2
164+
run: echo ${{ github.event.number }} > pr_number.txt
165+
166+
- name: Upload Coverage Artifact
167+
if: success() && github.event_name == 'pull_request' && matrix.python-version == '3.12'
168+
uses: actions/upload-artifact@v4
165169
with:
166-
path: coverage_comment.txt
167-
header: coverage
170+
name: coverage-artifact
171+
path: |
172+
coverage_comment.txt
173+
pr_number.txt
174+
168175
169176
test-macos:
170177
name: macos-python-${{ matrix.python-version }}
@@ -228,4 +235,4 @@ jobs:
228235
source .venv/bin/activate
229236
pytest --cov=src -vv
230237
env:
231-
PYTHONSTARTMETHOD: spawn
238+
PYTHONSTARTMETHOD: spawn
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Post Coverage Comment
2+
3+
on:
4+
workflow_run:
5+
# Must match the 'name' key in ci.yml exactly
6+
workflows: ["CI/Test"]
7+
types:
8+
- completed
9+
10+
permissions:
11+
pull-requests: write
12+
13+
jobs:
14+
comment:
15+
runs-on: ubuntu-latest
16+
if: >
17+
github.event.workflow_run.event == 'pull_request' &&
18+
github.event.workflow_run.conclusion == 'success'
19+
steps:
20+
- name: Download Coverage Artifact
21+
uses: actions/download-artifact@v4
22+
with:
23+
name: coverage-artifact
24+
run-id: ${{ github.event.workflow_run.id }}
25+
github-token: ${{ secrets.GITHUB_TOKEN }}
26+
27+
- name: Read PR Number
28+
id: pr
29+
run: echo "number=$(cat pr_number.txt)" >> $GITHUB_OUTPUT
30+
31+
- name: Post Coverage Comment
32+
uses: marocchino/sticky-pull-request-comment@v2
33+
with:
34+
number: ${{ steps.pr.outputs.number }}
35+
path: coverage_comment.txt
36+
header: coverage

changelog.d/148.infra.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix permission issue in forks for coverage comment.

0 commit comments

Comments
 (0)