Skip to content

Commit 162b960

Browse files
authored
#149 update (#152)
1 parent d87613b commit 162b960

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

.github/workflows/code-coverage.yml

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
name: "3. Code Coverage"
22

33
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
77

88
on:
99
push:
10-
branches:
11-
- "main"
12-
- "feature/*"
10+
branches: ["main", "feature/*"]
1311
paths-ignore:
1412
- "**/README.md"
15-
- "coverage-badge.svg" # prevent loops when the badge PR merges
13+
- "coverage-badge.svg" # avoid loops after merge
1614
pull_request:
17-
branches:
18-
- "main"
15+
branches: ["main"]
1916
workflow_dispatch:
2017

2118
jobs:
@@ -28,7 +25,7 @@ jobs:
2825
with:
2926
ref: ${{ github.ref }}
3027
fetch-depth: 0
31-
persist-credentials: false # PR action will handle auth
28+
persist-credentials: false
3229

3330
- name: Setup .NET
3431
uses: actions/setup-dotnet@v4
@@ -60,15 +57,37 @@ jobs:
6057
if: github.ref == 'refs/heads/main'
6158
run: cp coverage/badge_linecoverage.svg ./coverage-badge.svg
6259

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)
6562
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'
6685
id: cpr
6786
uses: peter-evans/create-pull-request@v6
6887
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
7291
title: "Update coverage badge"
7392
commit-message: "Update coverage badge"
7493
body: "Automated update of coverage-badge.svg"
@@ -79,15 +98,9 @@ jobs:
7998
automated
8099
81100
- name: Report CPR result
101+
if: github.ref == 'refs/heads/main'
82102
run: |
103+
echo "exists=${{ steps.badge.outputs.exists }}"
104+
echo "changed=${{ steps.badge.outputs.changed }}"
83105
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

Comments
 (0)