|
| 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" |
0 commit comments