-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathci_metrics_summary.py
More file actions
343 lines (295 loc) · 10.5 KB
/
Copy pathci_metrics_summary.py
File metadata and controls
343 lines (295 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python3
"""
Parse Maven QA metrics across all modules and generate:
1. GitHub Actions step summary with tables
2. Badge JSON files for Shields.io endpoints
Usage:
python scripts/ci_metrics_summary.py
Environment variables:
UPDATE_BADGES: Set to "true" to write badge JSON files
GITHUB_STEP_SUMMARY: Path to GitHub step summary file (set by Actions)
"""
import json
import os
import sys
import xml.etree.ElementTree as ET
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
BADGES_DIR = ROOT / "ci" / "badges"
MODULES = [
"modules/01-spring-hello-rest",
"modules/02-spring-scheduling-tasks",
"modules/03-quote-service",
"modules/03-spring-consuming-rest",
"modules/04-spring-relational-data-access",
]
def percent(part: int, whole: int) -> float:
"""Calculate percentage, returning 0.0 if whole is zero."""
return round((part / whole) * 100, 1) if whole > 0 else 0.0
def bar(pct: float, width: int = 20) -> str:
"""Create a text progress bar."""
filled = int(round((pct / 100) * width))
filled = max(0, min(width, filled))
return "█" * filled + "░" * (width - filled)
def load_jacoco_for_module(module: str) -> dict | None:
"""Parse JaCoCo XML for a specific module."""
report = ROOT / module / "target" / "site" / "jacoco" / "jacoco.xml"
if not report.exists():
return None
try:
tree = ET.parse(report)
# Get the LINE counter from the report root level (direct child of <report>)
for counter in tree.getroot().findall("counter"):
if counter.attrib.get("type") == "LINE":
covered = int(counter.attrib.get("covered", 0))
missed = int(counter.attrib.get("missed", 0))
return {
"covered": covered,
"missed": missed,
"pct": percent(covered, covered + missed),
}
except ET.ParseError:
pass
return None
def load_surefire_for_module(module: str) -> dict | None:
"""Parse Surefire XML reports for a specific module."""
report_dir = ROOT / module / "target" / "surefire-reports"
if not report_dir.exists():
return None
total = failures = errors = skipped = 0
for xml_path in report_dir.glob("TEST-*.xml"):
try:
root = ET.parse(xml_path).getroot()
total += int(root.attrib.get("tests", 0))
failures += int(root.attrib.get("failures", 0))
errors += int(root.attrib.get("errors", 0))
skipped += int(root.attrib.get("skipped", 0))
except ET.ParseError:
continue
if total == 0:
return None
return {
"tests": total,
"failures": failures,
"errors": errors,
"skipped": skipped,
}
def load_pitest_for_module(module: str) -> dict | None:
"""Parse PITest mutations.xml for a specific module."""
# PITest puts reports in target/pit-reports/ with timestamp subdirectories
pit_reports = ROOT / module / "target" / "pit-reports"
if not pit_reports.exists():
return None
# Find mutations.xml (could be in a timestamped subdirectory)
report = None
for candidate in [pit_reports / "mutations.xml"] + list(
pit_reports.glob("*/mutations.xml")
):
if candidate.exists():
report = candidate
break
if not report:
return None
try:
tree = ET.parse(report)
mutations = list(tree.getroot().iter("mutation"))
total = len(mutations)
if total == 0:
return {"total": 0, "killed": 0, "pct": 0.0}
killed = sum(1 for m in mutations if m.attrib.get("status") == "KILLED")
return {"total": total, "killed": killed, "pct": percent(killed, total)}
except ET.ParseError:
return None
def load_spotbugs_for_module(module: str) -> int | None:
"""Count SpotBugs issues for a module."""
for name in ("spotbugsXml.xml", "spotbugs.xml"):
report = ROOT / module / "target" / name
if report.exists():
try:
return sum(
1 for _ in ET.parse(report).getroot().iter("BugInstance")
)
except ET.ParseError:
pass
return None
def badge_color(pct: float) -> str:
"""Return hex color based on percentage threshold."""
if pct >= 90:
return "16A34A" # green-600
if pct >= 75:
return "F59E0B" # amber-500
if pct >= 60:
return "EA580C" # orange-600
return "DC2626" # red-600
def write_badges(
total_coverage: float, total_mutation: float, total_spotbugs: int
) -> None:
"""Write Shields.io endpoint badge JSON files for aggregate metrics."""
BADGES_DIR.mkdir(parents=True, exist_ok=True)
badges = {
"jacoco.json": {
"schemaVersion": 1,
"label": "coverage",
"message": f"{total_coverage:.1f}%",
"color": badge_color(total_coverage),
},
"mutation.json": {
"schemaVersion": 1,
"label": "mutation",
"message": f"{total_mutation:.1f}%",
"color": badge_color(total_mutation),
},
"spotbugs.json": {
"schemaVersion": 1,
"label": "spotbugs",
"message": "clean" if total_spotbugs == 0 else f"{total_spotbugs} issues",
"color": "16A34A" if total_spotbugs == 0 else "DC2626",
},
}
for filename, payload in badges.items():
(BADGES_DIR / filename).write_text(
json.dumps(payload, indent=2), encoding="utf-8"
)
print(f"[INFO] Updated aggregate badges in {BADGES_DIR}")
def write_module_badge(
module: str,
jacoco: dict | None,
pitest: dict | None,
spotbugs: int | None,
) -> None:
"""Write Shields.io endpoint badge JSON files for a specific module."""
# Strip "modules/" prefix to match badge directory convention
module_name = module.replace("modules/", "")
module_badge_dir = BADGES_DIR / module_name
module_badge_dir.mkdir(parents=True, exist_ok=True)
# Coverage badge
if jacoco:
cov_badge = {
"schemaVersion": 1,
"label": "coverage",
"message": f"{jacoco['pct']:.1f}%",
"color": badge_color(jacoco["pct"]),
}
else:
cov_badge = {
"schemaVersion": 1,
"label": "coverage",
"message": "n/a",
"color": "9CA3AF",
}
# Mutation badge
if pitest:
mut_badge = {
"schemaVersion": 1,
"label": "mutation",
"message": f"{pitest['pct']:.1f}%",
"color": badge_color(pitest["pct"]),
}
else:
mut_badge = {
"schemaVersion": 1,
"label": "mutation",
"message": "n/a",
"color": "9CA3AF",
}
# SpotBugs badge
if spotbugs is not None:
bug_badge = {
"schemaVersion": 1,
"label": "spotbugs",
"message": "clean" if spotbugs == 0 else f"{spotbugs} issues",
"color": "16A34A" if spotbugs == 0 else "DC2626",
}
else:
bug_badge = {
"schemaVersion": 1,
"label": "spotbugs",
"message": "n/a",
"color": "9CA3AF",
}
(module_badge_dir / "jacoco.json").write_text(
json.dumps(cov_badge, indent=2), encoding="utf-8"
)
(module_badge_dir / "mutation.json").write_text(
json.dumps(mut_badge, indent=2), encoding="utf-8"
)
(module_badge_dir / "spotbugs.json").write_text(
json.dumps(bug_badge, indent=2), encoding="utf-8"
)
def main() -> int:
"""Main entry point."""
lines = [
"## QA Metrics Summary",
"",
"| Module | Tests | Coverage | Mutation | SpotBugs |",
"|--------|-------|----------|----------|----------|",
]
total_tests = 0
total_covered = 0
total_lines = 0
total_killed = 0
total_mutations = 0
total_bugs = 0
# Store per-module data for badge generation
module_data = {}
for module in MODULES:
tests = load_surefire_for_module(module)
jacoco = load_jacoco_for_module(module)
pitest = load_pitest_for_module(module)
spotbugs = load_spotbugs_for_module(module)
# Store for badge generation
module_data[module] = {
"jacoco": jacoco,
"pitest": pitest,
"spotbugs": spotbugs,
}
test_str = f"{tests['tests']}" if tests else "—"
cov_str = f"{jacoco['pct']}%" if jacoco else "—"
mut_str = f"{pitest['pct']}%" if pitest else "—"
bug_str = str(spotbugs) if spotbugs is not None else "—"
lines.append(f"| {module} | {test_str} | {cov_str} | {mut_str} | {bug_str} |")
if tests:
total_tests += tests["tests"]
if jacoco:
total_covered += jacoco["covered"]
total_lines += jacoco["covered"] + jacoco["missed"]
if pitest:
total_killed += pitest["killed"]
total_mutations += pitest["total"]
if spotbugs is not None:
total_bugs += spotbugs
# Calculate totals
total_cov_pct = percent(total_covered, total_lines)
total_mut_pct = percent(total_killed, total_mutations)
lines.append("")
lines.append(
f"**Totals:** {total_tests} tests, {total_cov_pct}% line coverage, "
f"{total_mut_pct}% mutation score, {total_bugs} SpotBugs issues"
)
lines.append("")
lines.append(f"Coverage: `{bar(total_cov_pct)}` {total_cov_pct}%")
lines.append(f"Mutation: `{bar(total_mut_pct)}` {total_mut_pct}%")
summary = "\n".join(lines) + "\n"
# Write to GitHub step summary or stdout
summary_path = os.environ.get("GITHUB_STEP_SUMMARY")
if summary_path:
with open(summary_path, "a", encoding="utf-8") as f:
f.write(summary)
print("[INFO] Appended metrics to GitHub step summary")
else:
print(summary)
# Update badges if enabled
if os.environ.get("UPDATE_BADGES", "").lower() in {"1", "true", "yes"}:
write_badges(total_cov_pct, total_mut_pct, total_bugs)
# Write per-module badges
for module, data in module_data.items():
write_module_badge(
module,
data["jacoco"],
data["pitest"],
data["spotbugs"],
)
print(f"[INFO] Updated per-module badges for {len(module_data)} modules")
return 0
if __name__ == "__main__":
sys.exit(main())