Skip to content

Commit a558edb

Browse files
committed
feat: add script to generate JaCoCo coverage badge and update workflow to commit badge on push
1 parent 16088cb commit a558edb

3 files changed

Lines changed: 80 additions & 1 deletion

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env bash
2+
# Generates an SVG coverage badge from a JaCoCo CSV report.
3+
#
4+
# Usage: generate-coverage-badge.sh [jacoco.csv] [output-dir]
5+
# jacoco.csv - Path to JaCoCo CSV report (default: target/site/jacoco-coverage/jacoco.csv)
6+
# output-dir - Directory for the badge SVG (default: .github/badges)
7+
set -euo pipefail
8+
9+
CSV="${1:-target/site/jacoco-coverage/jacoco.csv}"
10+
BADGES_DIR="${2:-.github/badges}"
11+
12+
if [ ! -f "$CSV" ]; then
13+
echo "⚠️ No JaCoCo CSV report found at $CSV"
14+
exit 0
15+
fi
16+
17+
# Sum INSTRUCTION_MISSED and INSTRUCTION_COVERED across all rows (skip header)
18+
read -r missed covered <<< "$(awk -F',' 'NR>1 { m+=$4; c+=$5 } END { print m, c }' "$CSV")"
19+
total=$((missed + covered))
20+
if [ "$total" -eq 0 ]; then
21+
pct="0"
22+
else
23+
pct=$(awk "BEGIN { printf \"%.1f\", ($covered / $total) * 100 }")
24+
# Drop trailing .0
25+
pct=$(echo "$pct" | sed 's/\.0$//')
26+
fi
27+
echo "Coverage: ${pct}%"
28+
29+
# Choose badge color based on coverage
30+
color="#e05d44" # red <60
31+
if awk "BEGIN{exit!($pct>=100)}"; then color="#4c1" # bright green
32+
elif awk "BEGIN{exit!($pct>=90)}"; then color="#97ca00" # green
33+
elif awk "BEGIN{exit!($pct>=80)}"; then color="#a4a61d" # yellow-green
34+
elif awk "BEGIN{exit!($pct>=70)}"; then color="#dfb317" # yellow
35+
elif awk "BEGIN{exit!($pct>=60)}"; then color="#fe7d37" # orange
36+
fi
37+
38+
# Generate SVG badge
39+
mkdir -p "$BADGES_DIR"
40+
label="coverage"
41+
value="${pct}%"
42+
lw=62; vw=46; tw=$((lw + vw))
43+
cat > "${BADGES_DIR}/jacoco.svg" <<EOF
44+
<svg xmlns="http://www.w3.org/2000/svg" width="${tw}" height="20">
45+
<linearGradient id="b" x2="0" y2="100%">
46+
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
47+
<stop offset="1" stop-opacity=".1"/>
48+
</linearGradient>
49+
<mask id="a"><rect width="${tw}" height="20" rx="3" fill="#fff"/></mask>
50+
<g mask="url(#a)">
51+
<rect width="${lw}" height="20" fill="#555"/>
52+
<rect x="${lw}" width="${vw}" height="20" fill="${color}"/>
53+
<rect width="${tw}" height="20" fill="url(#b)"/>
54+
</g>
55+
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
56+
<text x="$((lw/2))" y="15" fill="#010101" fill-opacity=".3">${label}</text>
57+
<text x="$((lw/2))" y="14">${label}</text>
58+
<text x="$((lw + vw/2))" y="15" fill="#010101" fill-opacity=".3">${value}</text>
59+
<text x="$((lw + vw/2))" y="14">${value}</text>
60+
</g>
61+
</svg>
62+
EOF
63+
64+
echo "Badge generated at ${BADGES_DIR}/jacoco.svg"

.github/workflows/build-test.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ on:
1919
merge_group:
2020

2121
permissions:
22-
contents: read
22+
contents: write
2323
checks: write
2424

2525
jobs:
@@ -86,6 +86,20 @@ jobs:
8686
target/surefire-reports/
8787
retention-days: 1
8888

89+
- name: Generate and commit JaCoCo badge
90+
if: success() && github.event_name == 'push' && github.ref == 'refs/heads/main'
91+
run: |
92+
.github/scripts/generate-coverage-badge.sh
93+
94+
# Commit if changed
95+
if [[ $(git status --porcelain .github/badges/) ]]; then
96+
git config --global user.name 'github-actions[bot]'
97+
git config --global user.email '41898282+github-actions[bot]@users.noreply.github.com'
98+
git add .github/badges/
99+
git commit -m "Update JaCoCo coverage badge"
100+
git push
101+
fi
102+
89103
- name: Generate Test Report Summary
90104
if: always()
91105
uses: ./.github/actions/test-report

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[![Build](https://github.com/copilot-community-sdk/copilot-sdk-java/actions/workflows/build-test.yml/badge.svg)](https://github.com/copilot-community-sdk/copilot-sdk-java/actions/workflows/build-test.yml)
44
[![Site](https://github.com/copilot-community-sdk/copilot-sdk-java/actions/workflows/deploy-site.yml/badge.svg)](https://github.com/copilot-community-sdk/copilot-sdk-java/actions/workflows/deploy-site.yml)
5+
[![Coverage](.github/badges/jacoco.svg)](https://copilot-community-sdk.github.io/copilot-sdk-java/snapshot/jacoco/index.html)
56
[![Documentation](https://img.shields.io/badge/docs-online-brightgreen)](https://copilot-community-sdk.github.io/copilot-sdk-java/)
67
[![Java 17+](https://img.shields.io/badge/Java-17%2B-blue)](https://openjdk.org/)
78
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

0 commit comments

Comments
 (0)