From 6f9dfaa7142e31c22b7a4eedaca87084a52e0e60 Mon Sep 17 00:00:00 2001 From: Brad Stein Date: Sat, 18 Apr 2026 16:32:35 -0300 Subject: [PATCH] ci(metrics): emit checks and platform coverage/loc gauges --- scripts/publish_test_metrics.py | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/scripts/publish_test_metrics.py b/scripts/publish_test_metrics.py index 7f0b21a..f7d6e99 100644 --- a/scripts/publish_test_metrics.py +++ b/scripts/publish_test_metrics.py @@ -8,6 +8,11 @@ import os import sys import urllib.request import xml.etree.ElementTree as ET +from pathlib import Path + + +SOURCE_SUFFIXES = {".go", ".py", ".js", ".ts", ".tsx", ".json", ".yaml", ".yml", ".sh"} +SKIP_DIRS = {".git", ".venv", "venv", "node_modules", "build", "dist", "__pycache__", ".pytest_cache"} def _escape_label(value: str) -> str: @@ -57,6 +62,25 @@ def _load_junit(path: str) -> dict[str, int]: return totals +def _count_lines_over_limit(root: Path, *, max_lines: int = 500) -> int: + count = 0 + for path in root.rglob("*"): + if not path.is_file(): + continue + if any(part in SKIP_DIRS for part in path.parts): + continue + if path.name != "Jenkinsfile" and path.suffix.lower() not in SOURCE_SUFFIXES: + continue + try: + with path.open("r", encoding="utf-8", errors="ignore") as handle: + lines = sum(1 for _ in handle) + except OSError: + continue + if lines > max_lines: + count += 1 + return count + + def _load_exit_code(path: str) -> int | None: if not path or not os.path.exists(path): return None @@ -127,8 +151,10 @@ def main() -> int: if not os.path.exists(junit_path): raise RuntimeError(f"missing junit file {junit_path}") + repo_root = Path(__file__).resolve().parents[1] coverage = _load_coverage(coverage_path) totals = _load_junit(junit_path) + over_500 = _count_lines_over_limit(repo_root) test_exit_code = _load_exit_code(test_exit_code_path) passed = max(totals["tests"] - totals["failures"] - totals["errors"] - totals["skipped"], 0) @@ -156,6 +182,9 @@ def main() -> int: ok_count += 1 else: failed_count += 1 + tests_check = "ok" if outcome == "ok" else "failed" + coverage_check = "ok" if coverage >= 95.0 else "failed" + loc_check = "ok" if over_500 == 0 else "failed" labels = { "suite": suite, @@ -178,6 +207,14 @@ def main() -> int: f'metis_quality_gate_run_status{{suite="{suite}",status="failed"}} {1 if outcome == "failed" else 0}', "# TYPE metis_quality_gate_coverage_percent gauge", f'metis_quality_gate_coverage_percent{{suite="{suite}"}} {coverage:.3f}', + "# TYPE platform_quality_gate_workspace_line_coverage_percent gauge", + f'platform_quality_gate_workspace_line_coverage_percent{{suite="{suite}"}} {coverage:.3f}', + "# TYPE platform_quality_gate_source_lines_over_500_total gauge", + f'platform_quality_gate_source_lines_over_500_total{{suite="{suite}"}} {over_500}', + "# TYPE metis_quality_gate_checks_total gauge", + f'metis_quality_gate_checks_total{{suite="{suite}",check="tests",result="{tests_check}"}} 1', + f'metis_quality_gate_checks_total{{suite="{suite}",check="coverage",result="{coverage_check}"}} 1', + f'metis_quality_gate_checks_total{{suite="{suite}",check="loc",result="{loc_check}"}} 1', "# TYPE metis_quality_gate_build_info gauge", f"metis_quality_gate_build_info{_label_str(labels)} 1", ] @@ -195,6 +232,7 @@ def main() -> int: "tests_errors": totals["errors"], "tests_skipped": totals["skipped"], "coverage_percent": round(coverage, 3), + "source_lines_over_500": over_500, "test_exit_code": test_exit_code, "ok_counter": ok_count, "failed_counter": failed_count,