|
| 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/checkout@v4 |
| 39 | + |
| 40 | + -name:Install uv |
| 41 | +uses:astral-sh/setup-uv@v5 |
| 42 | +with: |
| 43 | +enable-cache:true |
| 44 | +cache-dependency-glob:pyproject.toml |
| 45 | + |
| 46 | + -name:Install Python |
| 47 | +uses:actions/setup-python@v5 |
| 48 | +with: |
| 49 | +python-version:'3.13' |
| 50 | + |
| 51 | + -name:Install tox |
| 52 | +run:uv tool install --python-preference only-managed --python 3.13 tox --with . --with tox-uv --with tox-gh |
| 53 | + |
| 54 | + -name:Create database |
| 55 | +run:| |
| 56 | + PGPASSWORD="postgres" psql -c 'create database django_guardian;' -h localhost -U postgres; |
| 57 | + PGPASSWORD="postgres" psql -c 'create database test_django_guardian;' -h localhost -U postgres; |
| 58 | +
|
| 59 | + -name:Setup test suite |
| 60 | +run:tox run -vv --notest --skip-missing-interpreters false |
| 61 | +env: |
| 62 | +TOX_GH_MAJOR_MINOR:'3.13' |
| 63 | + |
| 64 | + -name:Run tests with coverage |
| 65 | +env: |
| 66 | +TOX_GH_MAJOR_MINOR:'3.13' |
| 67 | +PYTEST_ADDOPTS:-vv --durations=20 |
| 68 | +DIFF_AGAINST:HEAD |
| 69 | +UV_PYTHON_PREFERENCE:only-managed |
| 70 | +run:tox run -vv --skip-pkg-install |
| 71 | + |
| 72 | + -name:Code Coverage Summary Report |
| 73 | +uses:irongut/CodeCoverageSummary@v1.3.0 |
| 74 | +with: |
| 75 | +filename:coverage.xml |
| 76 | +badge:true |
| 77 | +fail_below_min:false |
| 78 | +format:markdown |
| 79 | +hide_branch_rate:false |
| 80 | +hide_complexity:true |
| 81 | +indicators:true |
| 82 | +output:both |
| 83 | +thresholds:60 80 |
| 84 | + |
| 85 | + -name:Add Coverage PR Comment |
| 86 | +uses:marocchino/sticky-pull-request-comment@v2 |
| 87 | +if:github.event_name == 'pull_request' |
| 88 | +with: |
| 89 | +recreate:true |
| 90 | +path:code-coverage-results.md |
| 91 | + |
| 92 | + -name:Write to Job Summary |
| 93 | +run:cat code-coverage-results.md >> $GITHUB_STEP_SUMMARY |
| 94 | + |
| 95 | + -name:Update Coverage Badge |
| 96 | +if:github.ref == 'refs/heads/main' |
| 97 | +run:| |
| 98 | + # Extract coverage percentage from coverage.xml |
| 99 | + COVERAGE=$(python -c " |
| 100 | + import xml.etree.ElementTree as ET |
| 101 | + tree = ET.parse('coverage.xml') |
| 102 | + root = tree.getroot() |
| 103 | + coverage = root.attrib['line-rate'] |
| 104 | + percentage = round(float(coverage) * 100, 1) |
| 105 | + print(f'{percentage}') |
| 106 | + ") |
| 107 | +
|
| 108 | + # Create coverage badge URL |
| 109 | + if (( $(echo "$COVERAGE >= 90" | bc -l) )); then |
| 110 | + COLOR="brightgreen" |
| 111 | + elif (( $(echo "$COVERAGE >= 80" | bc -l) )); then |
| 112 | + COLOR="green" |
| 113 | + elif (( $(echo "$COVERAGE >= 70" | bc -l) )); then |
| 114 | + COLOR="yellowgreen" |
| 115 | + elif (( $(echo "$COVERAGE >= 60" | bc -l) )); then |
| 116 | + COLOR="yellow" |
| 117 | + else |
| 118 | + COLOR="red" |
| 119 | + fi |
| 120 | +
|
| 121 | + BADGE_URL="https://img.shields.io/badge/coverage-${COVERAGE}%25-${COLOR}" |
| 122 | + echo "Coverage: ${COVERAGE}%, Badge URL: ${BADGE_URL}" |
| 123 | + echo "COVERAGE_PERCENTAGE=${COVERAGE}" >> $GITHUB_ENV |
| 124 | + echo "BADGE_URL=${BADGE_URL}" >> $GITHUB_ENV |
| 125 | +
|
| 126 | + -name:Update README with Coverage Badge |
| 127 | +if:github.ref == 'refs/heads/main' |
| 128 | +run:| |
| 129 | + # Update README.md with the new coverage badge |
| 130 | + sed -i 's|!\[Coverage\]([^)]*)||g' README.md |
| 131 | +
|
| 132 | + # If no coverage badge exists, add it after the existing badges |
| 133 | + if ! grep -q "Coverage" README.md; then |
| 134 | + sed -i '/^\[\!\[Published on Django Packages\]/a [](https://github.com/django-guardian/django-guardian/actions/workflows/coverage.yml)' README.md |
| 135 | + fi |
| 136 | +
|
| 137 | + -name:Commit and push if changed |
| 138 | +if:github.ref == 'refs/heads/main' |
| 139 | +run:| |
| 140 | + git config --local user.email "action@github.com" |
| 141 | + git config --local user.name "GitHub Action" |
| 142 | + git add README.md |
| 143 | + if git diff --staged --quiet; then |
| 144 | + echo "No changes to commit" |
| 145 | + else |
| 146 | + git commit -m "Update coverage badge to ${{ env.COVERAGE_PERCENTAGE }}%" |
| 147 | + git push |
| 148 | + fi |