-
Notifications
You must be signed in to change notification settings - Fork 51
83 lines (70 loc) · 2.61 KB
/
coverage.yml
File metadata and controls
83 lines (70 loc) · 2.61 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
name: Coverage
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:
jobs:
coverage:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[dev]
- name: Run tests with coverage
run: |
python -m pytest --cov=balance --cov-report=xml --cov-report=html --cov-report=term-missing
- name: Upload coverage reports to artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: htmlcov/
- name: Upload coverage XML
if: always()
uses: actions/upload-artifact@v4
with:
name: coverage-xml
path: coverage.xml
- name: Display coverage percentage
run: |
coverage report --precision=2
- name: Update coverage badge
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
env:
GIST_TOKEN: ${{ secrets.GIST_TOKEN_COVERAGE }}
run: |
COVERAGE=$(coverage report --precision=2 | grep TOTAL | awk '{print $NF}' | sed 's/%//')
echo "Coverage: $COVERAGE%"
# Determine badge color based on coverage (finer gradient)
if (( $(echo "$COVERAGE == 100" | bc -l) )); then
COLOR="brightgreen"
elif (( $(echo "$COVERAGE >= 97" | bc -l) )); then
COLOR="green"
elif (( $(echo "$COVERAGE >= 95" | bc -l) )); then
COLOR="yellowgreen"
elif (( $(echo "$COVERAGE >= 90" | bc -l) )); then
COLOR="yellow"
elif (( $(echo "$COVERAGE >= 80" | bc -l) )); then
COLOR="orange"
else
COLOR="red"
fi
# Create badge JSON content as a string (escape quotes for nested JSON)
BADGE_CONTENT="{\\\"schemaVersion\\\": 1, \\\"label\\\": \\\"coverage\\\", \\\"message\\\": \\\"${COVERAGE}%\\\", \\\"color\\\": \\\"${COLOR}\\\"}"
# Update the Gist (content must be a JSON-escaped string)
curl -s -X PATCH \
-H "Authorization: token $GIST_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/gists/89d05034d314ebda47c1e16607e1ee22 \
-d "{\"files\": {\"coverage-balance.json\": {\"content\": \"$BADGE_CONTENT\"}}}" | head -20
echo ""
echo "Badge updated successfully!"