Debian Codegraph @

.github/workflows/coverage.yml

raw
1 name: Coverage
2
3 on:
4 push:
5 branches: [main]
6 pull_request:
7 branches: [main]
8
9 permissions:
10 contents: write
11 pull-requests: write
12
13 jobs:
14 coverage:
15 runs-on: ubuntu-latest
16
17 services:
18 postgres:
19 image: postgres:16-alpine
20 env:
21 PGUSER: postgres
22 POSTGRES_PASSWORD: postgres
23 options: >-
24 --health-cmd pg_isready
25 --health-interval 10s
26 --health-timeout 5s
27 --health-retries 5
28 ports:
29 - 5432:5432
30
31 env:
32 DJANGO_SETTINGS_MODULE: guardian.testapp.testsettings
33 DATABASE_URL: postgres://postgres:postgres@localhost/django_guardian
34 DEPENDENCY_GROUP: ci
35
36 steps:
37 - name: Checkout code
38 uses: actions/[email protected]
39 with:
40 token: ${{ secrets.GITHUB_TOKEN }}
41
42 - name: Install uv
43 uses: astral-sh/[email protected]
44 with:
45 enable-cache: true
46 cache-dependency-glob: pyproject.toml
47
48 - name: Install Python
49 uses: actions/[email protected]
50 with:
51 python-version: '3.13'
52
53 - name: Install tox
54 run: uv tool install --python-preference only-managed --python 3.13 tox --with . --with tox-uv --with tox-gh
55
56 - name: Create database
57 run: |
58 PGPASSWORD="postgres" psql -c 'create database django_guardian;' -h localhost -U postgres;
59 PGPASSWORD="postgres" psql -c 'create database test_django_guardian;' -h localhost -U postgres;
60
61 - name: Setup test suite
62 run: tox run -vv --notest --skip-missing-interpreters false
63 env:
64 TOX_GH_MAJOR_MINOR: '3.13'
65
66 - name: Run tests with coverage
67 env:
68 TOX_GH_MAJOR_MINOR: '3.13'
69 PYTEST_ADDOPTS: -vv --durations=20
70 DIFF_AGAINST: HEAD
71 UV_PYTHON_PREFERENCE: only-managed
72 run: tox run -vv --skip-pkg-install
73
74 - name: Code Coverage Summary Report
75 uses: irongut/[email protected]
76 with:
77 filename: coverage.xml
78 badge: true
79 fail_below_min: false
80 format: markdown
81 hide_branch_rate: false
82 hide_complexity: true
83 indicators: true
84 output: both
85 thresholds: 60 80
86
87
88 - name: Write to Job Summary
89 run: cat code-coverage-results.md >> $GITHUB_STEP_SUMMARY
90
91 - name: Update Coverage Badge
92 if: github.ref == 'refs/heads/main'
93 run: |
94 # Extract coverage percentage from coverage.xml
95 COVERAGE=$(python -c "
96 import xml.etree.ElementTree as ET
97 tree = ET.parse('coverage.xml')
98 root = tree.getroot()
99 coverage = root.attrib['line-rate']
100 percentage = round(float(coverage) * 100, 1)
101 print(f'{percentage}')
102 ")
103
104 # Create coverage badge URL
105 if (( $(echo "$COVERAGE >= 90" | bc -l) )); then
106 COLOR="brightgreen"
107 elif (( $(echo "$COVERAGE >= 80" | bc -l) )); then
108 COLOR="green"
109 elif (( $(echo "$COVERAGE >= 70" | bc -l) )); then
110 COLOR="yellowgreen"
111 elif (( $(echo "$COVERAGE >= 60" | bc -l) )); then
112 COLOR="yellow"
113 else
114 COLOR="red"
115 fi
116
117 BADGE_URL="https://img.shields.io/badge/coverage-${COVERAGE}%25-${COLOR}"
118 echo "Coverage: ${COVERAGE}%, Badge URL: ${BADGE_URL}"
119 echo "COVERAGE_PERCENTAGE=${COVERAGE}" >> $GITHUB_ENV
120 echo "BADGE_URL=${BADGE_URL}" >> $GITHUB_ENV
121
122 - name: Update README with Coverage Badge
123 if: github.ref == 'refs/heads/main'
124 run: |
125 # Update README.md with the new coverage badge
126 sed -i 's|!\[Coverage\]([^)]*)|![Coverage]('"$BADGE_URL"')|g' README.md
127
128 # If no coverage badge exists, add it after the existing badges
129 if ! grep -q "Coverage" README.md; then
130 sed -i '/^\[\!\[Published on Django Packages\]/a [![Coverage]('"$BADGE_URL"')](https://github.com/django-guardian/django-guardian/actions/workflows/coverage.yml)' README.md
131 fi
132
133 - name: Create Pull Request for Coverage Badge Update
134 if: github.ref == 'refs/heads/main'
135 uses: peter-evans/create-pull-request@v8
136 with:
137 token: ${{ secrets.GITHUB_TOKEN }}
138 commit-message: Update coverage badge to ${{ env.COVERAGE_PERCENTAGE }}%
139 title: 🤖 Update coverage badge to ${{ env.COVERAGE_PERCENTAGE }}%
140 body: |
141 This is an automated pull request to update the coverage badge in README.md.
142
143 **Coverage**: ${{ env.COVERAGE_PERCENTAGE }}%
144
145 This PR was automatically created by the coverage workflow.
146 branch: auto-update-coverage-badge
147 delete-branch: true
148 base: main
149 add-paths: README.md