Skip to content

Commit 0d9061f

Browse files
authored
Add code coverage badges for PRs (#5489)
1 parent d4fd7b2 commit 0d9061f

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

.github/workflows/main.yml

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ on:
1111
- "release-notes/*"
1212
permissions:
1313
contents: read
14+
pull-requests: write
1415

1516
jobs:
1617
build:
@@ -60,16 +61,69 @@ jobs:
6061
CI_DEPLOY_PASSWORD: ${{ secrets.CENTRAL_DEPLOY_PASSWORD }}
6162
# MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
6263
run: ./mvnw -B -q -ff -DskipTests -ntp source:jar deploy
63-
- name: Generate code coverage
64-
if: ${{ matrix.release_build && github.event_name != 'pull_request' }}
65-
run: ./mvnw -B -q -ff -ntp test
64+
- name: Generate code coverage
65+
if: ${{ matrix.release_build }}
66+
run: ./mvnw -B -q -ff -ntp test jacoco:report
6667
- name: Publish code coverage
6768
if: ${{ matrix.release_build && github.event_name != 'pull_request' }}
6869
uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1
6970
with:
7071
token: ${{ secrets.CODECOV_TOKEN }}
7172
files: ./target/site/jacoco/jacoco.xml
7273
flags: unittests
74+
- name: Generate coverage summary
75+
if: ${{ matrix.release_build && github.event_name == 'pull_request' }}
76+
id: jacoco
77+
uses: cicirello/[email protected]
78+
with:
79+
jacoco-csv-file: target/site/jacoco/jacoco.csv
80+
generate-coverage-badge: false
81+
generate-branches-badge: false
82+
generate-summary: true
83+
- name: Add coverage comment to PR
84+
if: ${{ matrix.release_build && github.event_name == 'pull_request' }}
85+
run: |
86+
# Convert decimal to percentage and round to 1 decimal place
87+
COVERAGE=$(awk -v cov="${{ steps.jacoco.outputs.coverage }}" 'BEGIN { printf "%.1f", cov * 100 }')
88+
BRANCHES=$(awk -v br="${{ steps.jacoco.outputs.branches }}" 'BEGIN { printf "%.1f", br * 100 }')
89+
90+
# Determine color for coverage badge using awk (more portable than bc)
91+
COV_COLOR=$(awk -v cov="$COVERAGE" 'BEGIN {
92+
if (cov >= 80) print "brightgreen"
93+
else if (cov >= 60) print "green"
94+
else if (cov >= 40) print "yellow"
95+
else print "red"
96+
}')
97+
98+
BR_COLOR=$(awk -v br="$BRANCHES" 'BEGIN {
99+
if (br >= 80) print "brightgreen"
100+
else if (br >= 60) print "green"
101+
else if (br >= 40) print "yellow"
102+
else print "red"
103+
}')
104+
105+
COMMENT_BODY="## :test_tube: Code Coverage Report
106+
107+
| Metric | Coverage |
108+
|--------|----------|
109+
| **Instructions** | ![coverage](https://img.shields.io/badge/coverage-${COVERAGE}%25-${COV_COLOR}) **${COVERAGE}%** |
110+
| **Branches** | ![branches](https://img.shields.io/badge/branches-${BRANCHES}%25-${BR_COLOR}) **${BRANCHES}%** |
111+
112+
> Coverage data generated from JaCoCo test results
113+
114+
<!-- jacoco-coverage-comment -->"
115+
116+
# Find and delete existing coverage comment
117+
COMMENT_ID=$(gh pr view ${{ github.event.pull_request.number }} --json comments --jq '.comments[] | select(.body | contains("<!-- jacoco-coverage-comment -->")) | .id' | head -1)
118+
119+
if [ -n "$COMMENT_ID" ]; then
120+
gh api -X DELETE "repos/${{ github.repository }}/issues/comments/$COMMENT_ID"
121+
fi
122+
123+
# Post new comment
124+
gh pr comment ${{ github.event.pull_request.number }} --body "$COMMENT_BODY"
125+
env:
126+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
73127

74128
trigger-dep-build-v2:
75129
name: Trigger v2 dep builds

0 commit comments

Comments
 (0)