ci(metrics): add checks and platform coverage/loc metrics

This commit is contained in:
Brad Stein 2026-04-18 16:32:58 -03:00
parent 0f4e928622
commit 9d5cda94ca

View File

@ -20,6 +20,10 @@ import xml.etree.ElementTree as ET
from pathlib import Path
SOURCE_SUFFIXES = {".go", ".py", ".js", ".mjs", ".ts", ".tsx", ".json", ".yaml", ".yml", ".sh"}
SKIP_DIRS = {".git", ".venv", "venv", "node_modules", "build", "dist", "__pycache__", ".pytest_cache", "frontend-coverage"}
def _escape_label(value: str) -> str:
return value.replace("\\", "\\\\").replace("\n", "\\n").replace('"', '\\"')
@ -106,6 +110,25 @@ def _load_gate_summary(path: Path) -> dict[str, object]:
return {"ok": False, "issues": []}
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 _fetch_existing_counter(pushgateway_url: str, metric: str, labels: dict[str, str]) -> float:
text = _read_http(f"{pushgateway_url.rstrip('/')}/metrics")
if not text:
@ -161,6 +184,7 @@ def main() -> int:
backend_rc_file = Path(os.getenv("BACKEND_TEST_RC_FILE", "build/backend-test.rc"))
frontend_rc_file = Path(os.getenv("FRONTEND_TEST_RC_FILE", "build/frontend-test.rc"))
gate_summary = _load_gate_summary(Path(os.getenv("GATE_SUMMARY_FILE", "build/gate-summary.json")))
repo_root = Path(__file__).resolve().parents[1]
b = _load_junit(backend_junit)
f = _load_junit(frontend_junit)
@ -175,6 +199,7 @@ def main() -> int:
backend_pct = _load_backend_coverage_percent(backend_cov)
frontend_pct = _load_frontend_coverage_percent(frontend_cov)
coverage_pct = (backend_pct + frontend_pct) / 2 if (backend_pct or frontend_pct) else 0.0
over_500 = _count_lines_over_limit(repo_root)
backend_rc = _read_test_exit_code(backend_rc_file)
frontend_rc = _read_test_exit_code(frontend_rc_file)
@ -219,6 +244,10 @@ def main() -> int:
ok_count += 1
else:
failed_count += 1
tests_check = "ok" if outcome == "ok" else "failed"
coverage_check = "ok" if coverage_pct >= 95.0 else "failed"
loc_check = "ok" if over_500 == 0 else "failed"
gate_check = "ok" if gate_ok else "failed"
payload_lines = [
"# TYPE platform_quality_gate_runs_total counter",
@ -238,6 +267,15 @@ def main() -> int:
f'pegasus_quality_gate_status{{suite="{suite}",result="{"ok" if gate_ok else "failed"}"}} 1',
"# TYPE pegasus_quality_gate_issues_total gauge",
f'pegasus_quality_gate_issues_total{{suite="{suite}"}} {len(gate_issues)}',
"# TYPE platform_quality_gate_workspace_line_coverage_percent gauge",
f'platform_quality_gate_workspace_line_coverage_percent{{suite="{suite}"}} {coverage_pct:.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 pegasus_quality_gate_checks_total gauge",
f'pegasus_quality_gate_checks_total{{suite="{suite}",check="tests",result="{tests_check}"}} 1',
f'pegasus_quality_gate_checks_total{{suite="{suite}",check="coverage",result="{coverage_check}"}} 1',
f'pegasus_quality_gate_checks_total{{suite="{suite}",check="loc",result="{loc_check}"}} 1',
f'pegasus_quality_gate_checks_total{{suite="{suite}",check="gate",result="{gate_check}"}} 1',
"# TYPE pegasus_quality_gate_build_info gauge",
f"pegasus_quality_gate_build_info{_label_str(labels)} 1",
]
@ -254,6 +292,7 @@ def main() -> int:
"tests_errors": totals["errors"],
"tests_skipped": totals["skipped"],
"coverage_percent": round(coverage_pct, 3),
"source_lines_over_500": over_500,
"outcome": outcome,
"backend_rc": backend_rc,
"frontend_rc": frontend_rc,