|
1 | 1 | name: "3. Code Coverage" |
2 | 2 |
|
3 | 3 | permissions: |
4 | | - contents: write # commit & push |
5 | | - pull-requests: write # open/update PRs |
6 | | - issues: write # create/apply labels (fixes your error) |
| 4 | + contents: write |
| 5 | + pull-requests: write |
| 6 | + issues: write |
7 | 7 |
|
8 | 8 | on: |
9 | 9 | push: |
10 | | - branches: |
11 | | - - "main" |
12 | | - - "feature/*" |
| 10 | + branches: ["main", "feature/*"] |
13 | 11 | paths-ignore: |
14 | 12 | - "**/README.md" |
15 | | - - "coverage-badge.svg" # prevent loops when the badge PR merges |
| 13 | + - "coverage-badge.svg" # avoid loops after merge |
16 | 14 | pull_request: |
17 | | - branches: |
18 | | - - "main" |
| 15 | + branches: ["main"] |
19 | 16 | workflow_dispatch: |
20 | 17 |
|
21 | 18 | jobs: |
|
28 | 25 | with: |
29 | 26 | ref: ${{ github.ref }} |
30 | 27 | fetch-depth: 0 |
31 | | - persist-credentials: false # PR action will handle auth |
| 28 | + persist-credentials: false |
32 | 29 |
|
33 | 30 | - name: Setup .NET |
34 | 31 | uses: actions/setup-dotnet@v4 |
@@ -60,15 +57,37 @@ jobs: |
60 | 57 | if: github.ref == 'refs/heads/main' |
61 | 58 | run: cp coverage/badge_linecoverage.svg ./coverage-badge.svg |
62 | 59 |
|
63 | | - # Create/update a PR that contains ONLY the badge file |
64 | | - - name: Open / update PR with coverage badge (only on main) |
| 60 | + # Compare against current main and only proceed if the badge changed |
| 61 | + - name: Detect badge change (only on main) |
65 | 62 | if: github.ref == 'refs/heads/main' |
| 63 | + id: badge |
| 64 | + shell: bash |
| 65 | + run: | |
| 66 | + set -e |
| 67 | + if [ ! -f coverage-badge.svg ]; then |
| 68 | + echo "exists=false" >> $GITHUB_OUTPUT |
| 69 | + exit 0 |
| 70 | + fi |
| 71 | + # Get current version from main (if it exists) |
| 72 | + if git cat-file -e origin/main:coverage-badge.svg 2>/dev/null; then |
| 73 | + git show origin/main:coverage-badge.svg > /tmp/old-badge.svg |
| 74 | + if cmp -s coverage-badge.svg /tmp/old-badge.svg; then |
| 75 | + echo "exists=true" >> $GITHUB_OUTPUT |
| 76 | + echo "changed=false" >> $GITHUB_OUTPUT |
| 77 | + exit 0 |
| 78 | + fi |
| 79 | + fi |
| 80 | + echo "exists=true" >> $GITHUB_OUTPUT |
| 81 | + echo "changed=true" >> $GITHUB_OUTPUT |
| 82 | +
|
| 83 | + - name: Open / update PR with coverage badge (only when changed) |
| 84 | + if: github.ref == 'refs/heads/main' && steps.badge.outputs.exists == 'true' && steps.badge.outputs.changed == 'true' |
66 | 85 | id: cpr |
67 | 86 | uses: peter-evans/create-pull-request@v6 |
68 | 87 | with: |
69 | | - token: ${{ secrets.GITHUB_TOKEN }} # uses repo�s workflow token |
70 | | - branch: ci/update-coverage-badge # PR head branch |
71 | | - base: main # PR base branch |
| 88 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 89 | + branch: ci/update-coverage-badge |
| 90 | + base: main |
72 | 91 | title: "Update coverage badge" |
73 | 92 | commit-message: "Update coverage badge" |
74 | 93 | body: "Automated update of coverage-badge.svg" |
|
79 | 98 | automated |
80 | 99 |
|
81 | 100 | - name: Report CPR result |
| 101 | + if: github.ref == 'refs/heads/main' |
82 | 102 | run: | |
| 103 | + echo "exists=${{ steps.badge.outputs.exists }}" |
| 104 | + echo "changed=${{ steps.badge.outputs.changed }}" |
83 | 105 | echo "result=${{ steps.cpr.outputs.result }}" |
84 | | - echo "pr=${{ steps.cpr.outputs.pull-request-url || 'no PR (no changes)'}}" |
85 | | -
|
86 | | - # Optional: auto-merge the PR when allowed by branch protections |
87 | | - - name: Enable auto-merge for badge PR |
88 | | - if: steps.cpr.outputs.pull-request-number |
89 | | - uses: peter-evans/enable-pull-request-automerge@v3 |
90 | | - with: |
91 | | - token: ${{ secrets.GITHUB_TOKEN }} |
92 | | - pull-request-number: ${{ steps.cpr.outputs.pull-request-number }} |
93 | | - merge-method: squash |
| 106 | + echo "pr=${{ steps.cpr.outputs['pull-request-url'] || 'n/a' }}" |
0 commit comments