|
| 1 | +name: "PR Coverage Report" |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + coverage: |
| 14 | + name: "Test Coverage Report" |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: "Checkout Repository" |
| 18 | + uses: actions/checkout@v6 |
| 19 | + with: |
| 20 | + ref: ${{ github.event.pull_request.head.sha }} |
| 21 | + |
| 22 | + - name: "Cache Maven repository" |
| 23 | + uses: actions/cache@v5.0.4 |
| 24 | + with: |
| 25 | + path: ~/.m2 |
| 26 | + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} |
| 27 | + restore-keys: ${{ runner.os }}-m2 |
| 28 | + |
| 29 | + - name: "JDK set-up" |
| 30 | + uses: actions/setup-java@v5 |
| 31 | + with: |
| 32 | + java-version: '17' |
| 33 | + distribution: 'temurin' |
| 34 | + |
| 35 | + - name: "Build and Test" |
| 36 | + uses: nick-invision/retry@v4 |
| 37 | + env: |
| 38 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 39 | + with: |
| 40 | + max_attempts: 3 |
| 41 | + timeout_minutes: 15 |
| 42 | + retry_on: error |
| 43 | + command: | |
| 44 | + mvn verify jacoco:report-aggregate -B -P compatibility-mode -DskipPerformanceTests=true \ |
| 45 | + -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn |
| 46 | +
|
| 47 | + - name: "Download baseline coverage" |
| 48 | + uses: dawidd6/action-download-artifact@v6 |
| 49 | + continue-on-error: true |
| 50 | + with: |
| 51 | + workflow: github-default-actions.yml |
| 52 | + branch: master |
| 53 | + name: jacoco-coverage-baseline |
| 54 | + path: baseline/ |
| 55 | + |
| 56 | + - name: "Generate coverage table" |
| 57 | + run: | |
| 58 | + python3 << 'EOF' |
| 59 | + import json, os, xml.etree.ElementTree as ET |
| 60 | +
|
| 61 | + def parse(xml_path): |
| 62 | + root = ET.parse(xml_path).getroot() |
| 63 | + result = {} |
| 64 | + for c in root.findall('counter'): |
| 65 | + covered = int(c.get('covered', 0)) |
| 66 | + missed = int(c.get('missed', 0)) |
| 67 | + total = covered + missed |
| 68 | + result[c.get('type')] = { |
| 69 | + 'covered': covered, |
| 70 | + 'total': total, |
| 71 | + 'pct': round(covered / total * 100, 2) if total > 0 else 0.0 |
| 72 | + } |
| 73 | + return result |
| 74 | +
|
| 75 | + def fmt(p): return f'{p:.2f}%' |
| 76 | + def frac(c, t): return f'{c}/{t}' |
| 77 | + def sign(d): return f'{d:+.2f}%' |
| 78 | + def emoji(d): |
| 79 | + if d > 0.01: return '💚' |
| 80 | + if d < -0.01: return '🔴' |
| 81 | + return '💙' |
| 82 | +
|
| 83 | + build_xml = 'bull-report/target/site/jacoco-aggregate/jacoco.xml' |
| 84 | + baseline_xml = 'baseline/jacoco.xml' |
| 85 | +
|
| 86 | + build = parse(build_xml) |
| 87 | + baseline = parse(baseline_xml) if os.path.exists(baseline_xml) else build |
| 88 | +
|
| 89 | + metrics = [ |
| 90 | + ('LINE', 'Lines'), |
| 91 | + ('BRANCH', 'Branches'), |
| 92 | + ('INSTRUCTION', 'Instructions'), |
| 93 | + ('COMPLEXITY', 'Complexity'), |
| 94 | + ('METHOD', 'Methods'), |
| 95 | + ('CLASS', 'Classes'), |
| 96 | + ] |
| 97 | +
|
| 98 | + rows = [] |
| 99 | + norm_base = norm_build = 0 |
| 100 | + for key, label in metrics: |
| 101 | + b = baseline.get(key, {}) |
| 102 | + c = build.get(key, {}) |
| 103 | + bp = b.get('pct', 0.0) |
| 104 | + cp = c.get('pct', 0.0) |
| 105 | + d = cp - bp |
| 106 | + norm_base += bp |
| 107 | + norm_build += cp |
| 108 | + rows.append( |
| 109 | + f'| {label} | {fmt(bp)} | {frac(b.get("covered",0), b.get("total",0))} ' |
| 110 | + f'| {fmt(cp)} | {frac(c.get("covered",0), c.get("total",0))} ' |
| 111 | + f'| {sign(d)} | {emoji(d)} |' |
| 112 | + ) |
| 113 | +
|
| 114 | + count = len(metrics) |
| 115 | + nb = norm_base / count |
| 116 | + nc = norm_build / count |
| 117 | + nd = nc - nb |
| 118 | + rows.append(f'| **Normalised Score** | {fmt(nb)} | | {fmt(nc)} | | {sign(nd)} | {emoji(nd)} |') |
| 119 | +
|
| 120 | + header = ( |
| 121 | + '| Coverage | Baseline | | Build | | Diff | |\n' |
| 122 | + '|---|---|---|---|---|---|---|' |
| 123 | + ) |
| 124 | + table = header + '\n' + '\n'.join(rows) |
| 125 | + report = f'## Test Coverage Report\n\n{table}\n' |
| 126 | +
|
| 127 | + with open(os.environ['GITHUB_STEP_SUMMARY'], 'a') as f: |
| 128 | + f.write(report) |
| 129 | + with open('coverage_comment.md', 'w') as f: |
| 130 | + f.write(report) |
| 131 | + EOF |
| 132 | +
|
| 133 | + - name: "Post coverage comment" |
| 134 | + uses: marocchino/sticky-pull-request-comment@v2 |
| 135 | + with: |
| 136 | + path: coverage_comment.md |
| 137 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments