ci(bstein-home): publish per-test case metrics for flaky tracking

This commit is contained in:
codex 2026-04-20 08:35:18 -03:00
parent 930b9edbcf
commit d3e47d78a9
2 changed files with 43 additions and 2 deletions

View File

@ -7,7 +7,7 @@ import json
import os import os
from pathlib import Path from pathlib import Path
from .summary import load_junit_summary, publish_quality_metrics from .summary import load_junit_cases, load_junit_summary, publish_quality_metrics
def _build_parser() -> argparse.ArgumentParser: def _build_parser() -> argparse.ArgumentParser:
@ -92,7 +92,9 @@ def main(argv: list[str] | None = None) -> int:
parser = _build_parser() parser = _build_parser()
args = parser.parse_args(argv) args = parser.parse_args(argv)
summary = load_junit_summary(Path(path) for path in args.junit) junit_paths = [Path(path) for path in args.junit]
summary = load_junit_summary(junit_paths)
test_cases = load_junit_cases(junit_paths)
coverage_percent, source_lines_over_500, checks = _load_quality_report(Path(args.quality_report)) coverage_percent, source_lines_over_500, checks = _load_quality_report(Path(args.quality_report))
publish_quality_metrics( publish_quality_metrics(
gateway=args.gateway, gateway=args.gateway,
@ -103,6 +105,7 @@ def main(argv: list[str] | None = None) -> int:
workspace_line_coverage_percent=coverage_percent, workspace_line_coverage_percent=coverage_percent,
source_lines_over_500=source_lines_over_500, source_lines_over_500=source_lines_over_500,
checks=checks, checks=checks,
test_cases=test_cases,
) )
return 0 return 0

View File

@ -10,6 +10,11 @@ from pathlib import Path
from typing import Iterable from typing import Iterable
def _escape_label(value: str) -> str:
"""Escape Prometheus label values."""
return value.replace("\\", "\\\\").replace("\n", "\\n").replace('"', '\\"')
@dataclass(frozen=True) @dataclass(frozen=True)
class RunSummary: class RunSummary:
"""Aggregate counts from a collection of JUnit XML files.""" """Aggregate counts from a collection of JUnit XML files."""
@ -48,6 +53,30 @@ def load_junit_summary(paths: Iterable[Path]) -> RunSummary:
return RunSummary(**totals) return RunSummary(**totals)
def load_junit_cases(paths: Iterable[Path]) -> list[tuple[str, str]]:
"""Load per-test outcomes from one or more JUnit XML files."""
cases: list[tuple[str, str]] = []
for path in paths:
if not path.exists():
continue
root = ET.parse(path).getroot()
suites = [root] if root.tag == "testsuite" else list(root.findall("testsuite")) if root.tag == "testsuites" else []
for suite in suites:
for test_case in suite.findall("testcase"):
case_name = (test_case.attrib.get("name") or "").strip()
class_name = (test_case.attrib.get("classname") or "").strip()
if not case_name:
continue
full_name = f"{class_name}.{case_name}" if class_name else case_name
status = "passed"
if test_case.find("failure") is not None or test_case.find("error") is not None:
status = "failed"
elif test_case.find("skipped") is not None:
status = "skipped"
cases.append((full_name, status))
return cases
def read_pushgateway_counters(text: str, *, suite: str, job: str) -> dict[str, float]: def read_pushgateway_counters(text: str, *, suite: str, job: str) -> dict[str, float]:
"""Read the current quality-gate counters for a suite from Pushgateway text.""" """Read the current quality-gate counters for a suite from Pushgateway text."""
@ -72,6 +101,7 @@ def render_payload(
workspace_line_coverage_percent: float = 0.0, workspace_line_coverage_percent: float = 0.0,
source_lines_over_500: int = 0, source_lines_over_500: int = 0,
checks: dict[str, str] | None = None, checks: dict[str, str] | None = None,
test_cases: list[tuple[str, str]] | None = None,
) -> str: ) -> str:
"""Render the Pushgateway payload for the quality-gate counters.""" """Render the Pushgateway payload for the quality-gate counters."""
payload = ( payload = (
@ -94,6 +124,12 @@ def render_payload(
f'bstein_home_quality_gate_checks_total{{suite="{suite}",check="{check_name}",result="{check_status}"}} 1\n' f'bstein_home_quality_gate_checks_total{{suite="{suite}",check="{check_name}",result="{check_status}"}} 1\n'
for check_name, check_status in checks.items() for check_name, check_status in checks.items()
) )
if test_cases:
payload += "# TYPE platform_quality_gate_test_case_result gauge\n"
payload += "".join(
f'platform_quality_gate_test_case_result{{suite="{suite}",test="{_escape_label(test_name)}",status="{_escape_label(test_status)}"}} 1\n'
for test_name, test_status in test_cases
)
return payload return payload
@ -107,6 +143,7 @@ def publish_quality_metrics(
workspace_line_coverage_percent: float = 0.0, workspace_line_coverage_percent: float = 0.0,
source_lines_over_500: int = 0, source_lines_over_500: int = 0,
checks: dict[str, str] | None = None, checks: dict[str, str] | None = None,
test_cases: list[tuple[str, str]] | None = None,
) -> None: ) -> None:
"""Publish run and test totals to Pushgateway.""" """Publish run and test totals to Pushgateway."""
@ -126,6 +163,7 @@ def publish_quality_metrics(
workspace_line_coverage_percent=workspace_line_coverage_percent, workspace_line_coverage_percent=workspace_line_coverage_percent,
source_lines_over_500=source_lines_over_500, source_lines_over_500=source_lines_over_500,
checks=checks, checks=checks,
test_cases=test_cases,
) )
req = urllib.request.Request( req = urllib.request.Request(
f"{gateway}/metrics/job/{job}/suite/{suite}", f"{gateway}/metrics/job/{job}/suite/{suite}",