Skip to content

Commit 8b46966

Browse files
committed
🔧 Add test coverage comment bot
1 parent 2811558 commit 8b46966

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

.github/workflows/test_code.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,19 @@ jobs:
3232
run: uv run --group dev pytest
3333
- name: Run mypy
3434
run: uv run --group dev mypy .
35+
# For security reasons, PRs created from forks cannot generate PR comments directly
36+
# (see https://securitylab.github.com/research/github-actions-preventing-pwn-requests/).
37+
# Instead we need to trigger another workflow after this one completes.
38+
- name: Generate test coverage comment
39+
id: coverage_comment
40+
uses: py-cov-action/python-coverage-comment-action@v3
41+
with:
42+
GITHUB_TOKEN: ${{ github.token }}
43+
# Save the coverage comment for later use
44+
# See https://github.com/py-cov-action/python-coverage-comment-action/blob/main/README.md
45+
- name: Save coverage comment as an artifact
46+
uses: actions/upload-artifact@v4
47+
if: steps.coverage_comment.outputs.COMMENT_FILE_WRITTEN == 'true'
48+
with:
49+
name: python-coverage-comment-action
50+
path: python-coverage-comment-action.txt
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
name: Post test coverage GitHub comment
3+
4+
# Run workflow after test_code has completed
5+
on: # yamllint disable-line rule:truthy
6+
workflow_run:
7+
workflows: ["Run tests"]
8+
types:
9+
- completed
10+
11+
jobs:
12+
coverage:
13+
runs-on: ubuntu-latest
14+
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
15+
permissions:
16+
# Gives the action the necessary permissions for publishing new
17+
# comments in pull requests.
18+
pull-requests: write
19+
# Gives the action the necessary permissions for editing existing
20+
# comments (to avoid publishing multiple comments in the same PR)
21+
contents: write
22+
# Gives the action the necessary permissions for looking up the
23+
# workflow that launched this workflow, and download the related
24+
# artifact that contains the comment to be published
25+
actions: read
26+
steps:
27+
# Post the pre-generated coverage comment
28+
- name: Post coverage comment
29+
uses: py-cov-action/python-coverage-comment-action@v3
30+
with:
31+
GITHUB_TOKEN: ${{ github.token }}
32+
GITHUB_PR_RUN_ID: ${{ github.event.workflow_run.id }}

0 commit comments

Comments
 (0)