-
Notifications
You must be signed in to change notification settings - Fork 1
97 lines (97 loc) · 3.82 KB
/
coverage-comment.yml
File metadata and controls
97 lines (97 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
---
name: Coverage Comment
on: # yamllint disable-line rule:truthy # zizmor: ignore[dangerous-triggers]
workflow_run:
workflows: [Test]
types: [completed]
permissions:
actions: read
pull-requests: write
jobs:
comment:
runs-on: ubuntu-latest
if: >-
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
steps:
- name: Download artifact
# yamllint disable-line rule:line-length
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # ratchet:actions/download-artifact@v4
with:
run-id: ${{ github.event.workflow_run.id }}
name: pr
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Read PR number
id: pr
run: |
pr_number=$(cat NR)
if ! [[ "$pr_number" =~ ^[0-9]+$ ]]; then
echo "Invalid PR number: $pr_number" >&2
exit 1
fi
echo "number=$pr_number" >> "$GITHUB_OUTPUT"
- name: Find baseline run
id: baseline
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
python3 - <<'PYEOF'
import json, os, urllib.request
token = os.environ['GITHUB_TOKEN']
repo = os.environ['GITHUB_REPOSITORY']
url = (
f'https://api.github.com/repos/{repo}/actions/workflows'
f'/coverage-baseline.yml/runs'
f'?branch=main&status=success&per_page=1'
)
req = urllib.request.Request(url, headers={
'Authorization': f'Bearer {token}',
'Accept': 'application/vnd.github+json',
})
with urllib.request.urlopen(req) as r:
runs = json.loads(r.read()).get('workflow_runs', [])
run_id = str(runs[0]['id']) if runs else ''
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
f.write(f'run-id={run_id}\n')
PYEOF
- name: Download baseline coverage
if: steps.baseline.outputs.run-id != ''
# yamllint disable-line rule:line-length
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # ratchet:actions/download-artifact@v4
with:
run-id: ${{ steps.baseline.outputs.run-id }}
name: main-coverage
path: main/
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Compute coverage delta
id: delta
if: steps.baseline.outputs.run-id != ''
run: |
python3 - <<'PYEOF'
import xml.etree.ElementTree as ET
import os, sys
try:
pr_rate = ET.parse('coverage.xml').getroot().get('line-rate')
main_xml = ET.parse('main/coverage.xml').getroot()
main_rate = main_xml.get('line-rate')
pr = float(pr_rate) * 100
main = float(main_rate) * 100
delta = pr - main
if not (-100 <= delta <= 100):
raise ValueError(f"delta out of range: {delta}")
sign = '+' if delta >= 0 else ''
title = f"Coverage Report (Δ {sign}{delta:.1f}%)"
except Exception as e:
print(f"Warning: could not compute delta: {e}", file=sys.stderr)
title = "Coverage Report"
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
f.write(f"title={title}\n")
PYEOF
- name: Post coverage comment
# yamllint disable-line rule:line-length
uses: MishaKav/pytest-coverage-comment@dd5b80bde6d16941f336518e92929e89069d8451 # ratchet:MishaKav/pytest-coverage-comment@v1.7.2
with:
pytest-xml-coverage-path: coverage.xml
unique-id-for-comment: coverage
issue-number: ${{ steps.pr.outputs.number }}
title: ${{ steps.delta.outputs.title || 'Coverage Report' }}