Skip to content

Commit 5f4e704

Browse files
authored
Implement #1521: code cov badges for CI (#1522)
1 parent d037d22 commit 5f4e704

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

.github/workflows/main.yml

Lines changed: 66 additions & 2 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:
@@ -62,15 +63,78 @@ jobs:
6263
# MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
6364
run: ./mvnw -B -q -ff -DskipTests -ntp source:jar deploy
6465
- name: Generate code coverage
65-
if: ${{ matrix.release_build && github.event_name != 'pull_request' }}
66-
run: ./mvnw -B -q -ff -ntp test
66+
if: ${{ matrix.release_build }}
67+
run: ./mvnw -B -q -ff -ntp test jacoco:report
6768
- name: Publish code coverage
6869
if: ${{ matrix.release_build && github.event_name != 'pull_request' }}
6970
uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1
7071
with:
7172
token: ${{ secrets.CODECOV_TOKEN }}
7273
files: ./target/site/jacoco/jacoco.xml
7374
flags: unittests
75+
- name: Add coverage delta report to PR
76+
if: ${{ matrix.release_build && github.event_name == 'pull_request' }}
77+
uses: madrapps/jacoco-report@v1.7.1
78+
with:
79+
paths: ${{ github.workspace }}/target/site/jacoco/jacoco.xml
80+
token: ${{ secrets.GITHUB_TOKEN }}
81+
min-coverage-overall: 40
82+
min-coverage-changed-files: 60
83+
title: ':test_tube: Coverage Report'
84+
update-comment: true
85+
- name: Generate coverage summary
86+
if: ${{ matrix.release_build && github.event_name == 'pull_request' }}
87+
id: jacoco
88+
uses: cicirello/jacoco-badge-generator@v2.12.1
89+
with:
90+
jacoco-csv-file: target/site/jacoco/jacoco.csv
91+
generate-coverage-badge: false
92+
generate-branches-badge: false
93+
generate-summary: true
94+
- name: Add coverage comment to PR
95+
if: ${{ matrix.release_build && github.event_name == 'pull_request' }}
96+
run: |
97+
# Convert decimal to percentage and round to 1 decimal place
98+
COVERAGE=$(awk -v cov="${{ steps.jacoco.outputs.coverage }}" 'BEGIN { printf "%.1f", cov * 100 }')
99+
BRANCHES=$(awk -v br="${{ steps.jacoco.outputs.branches }}" 'BEGIN { printf "%.1f", br * 100 }')
100+
101+
# Determine color for coverage badge using awk (more portable than bc)
102+
COV_COLOR=$(awk -v cov="$COVERAGE" 'BEGIN {
103+
if (cov >= 80) print "brightgreen"
104+
else if (cov >= 60) print "green"
105+
else if (cov >= 40) print "yellow"
106+
else print "red"
107+
}')
108+
109+
BR_COLOR=$(awk -v br="$BRANCHES" 'BEGIN {
110+
if (br >= 80) print "brightgreen"
111+
else if (br >= 60) print "green"
112+
else if (br >= 40) print "yellow"
113+
else print "red"
114+
}')
115+
116+
COMMENT_BODY="## :chart_with_upwards_trend: Overall Code Coverage
117+
118+
| Metric | Coverage |
119+
|--------|----------|
120+
| **Instructions** | ![coverage](https://img.shields.io/badge/coverage-${COVERAGE}%25-${COV_COLOR}) **${COVERAGE}%** |
121+
| **Branches** | ![branches](https://img.shields.io/badge/branches-${BRANCHES}%25-${BR_COLOR}) **${BRANCHES}%** |
122+
123+
> Overall project coverage from JaCoCo test results
124+
125+
<!-- jacoco-coverage-comment -->"
126+
127+
# Find and delete existing coverage comment
128+
COMMENT_ID=$(gh pr view ${{ github.event.pull_request.number }} --json comments --jq '.comments[] | select(.body | contains("<!-- jacoco-coverage-comment -->")) | .id' | head -1) || true
129+
130+
if [ -n "$COMMENT_ID" ]; then
131+
gh api -X DELETE "repos/${{ github.repository }}/issues/comments/$COMMENT_ID" || true
132+
fi
133+
134+
# Post new comment
135+
gh pr comment ${{ github.event.pull_request.number }} --body "$COMMENT_BODY"
136+
env:
137+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
74138

75139
trigger-dep-builds-v2:
76140
name: Trigger downstream builds for v2

0 commit comments

Comments
 (0)