From 1713dd07c78061af5746a6280c5e86c0a31ba753 Mon Sep 17 00:00:00 2001 From: Brad Stein Date: Sat, 18 Apr 2026 16:32:20 -0300 Subject: [PATCH] ci(metrics): keep full payload with checks and platform stats --- Jenkinsfile | 90 --------------------------------- scripts/publish_test_metrics.py | 36 +++++++++++++ 2 files changed, 36 insertions(+), 90 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index a7445c4..88fe4df 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -179,96 +179,6 @@ spec: } } post { - success { - container('tester') { - sh ''' - set -euo pipefail - export QUALITY_STATUS=ok - python - <<'PY' -import os -import re -import urllib.request - -suite = os.environ.get("SUITE_NAME", "atlasbot") -status = os.environ.get("QUALITY_STATUS", "failed") -gateway = os.environ.get("PUSHGATEWAY_URL", "http://platform-quality-gateway.monitoring.svc.cluster.local:9091").rstrip("/") -text = urllib.request.urlopen(f"{gateway}/metrics", timeout=10).read().decode("utf-8", errors="replace") - -def counter(name: str) -> float: - pattern = re.compile( - rf'^platform_quality_gate_runs_total\\{{[^}}]*job="platform-quality-ci"[^}}]*suite="{re.escape(suite)}"[^}}]*status="{name}"[^}}]*\\}}\\s+([0-9]+(?:\\.[0-9]+)?)$', - re.M, - ) - match = pattern.search(text) - return float(match.group(1)) if match else 0.0 - -ok = counter("ok") -failed = counter("failed") -if status == "ok": - ok += 1 -else: - failed += 1 -payload = ( - "# TYPE platform_quality_gate_runs_total counter\\n" - f'platform_quality_gate_runs_total{{suite="{suite}",status="ok"}} {int(ok)}\\n' - f'platform_quality_gate_runs_total{{suite="{suite}",status="failed"}} {int(failed)}\\n' -) -req = urllib.request.Request( - f"{gateway}/metrics/job/platform-quality-ci/suite/{suite}", - data=payload.encode("utf-8"), - method="POST", - headers={"Content-Type": "text/plain"}, -) -urllib.request.urlopen(req, timeout=10).read() -PY - ''' - } - } - failure { - container('tester') { - sh ''' - set -euo pipefail - export QUALITY_STATUS=failed - python - <<'PY' -import os -import re -import urllib.request - -suite = os.environ.get("SUITE_NAME", "atlasbot") -status = os.environ.get("QUALITY_STATUS", "failed") -gateway = os.environ.get("PUSHGATEWAY_URL", "http://platform-quality-gateway.monitoring.svc.cluster.local:9091").rstrip("/") -text = urllib.request.urlopen(f"{gateway}/metrics", timeout=10).read().decode("utf-8", errors="replace") - -def counter(name: str) -> float: - pattern = re.compile( - rf'^platform_quality_gate_runs_total\\{{[^}}]*job="platform-quality-ci"[^}}]*suite="{re.escape(suite)}"[^}}]*status="{name}"[^}}]*\\}}\\s+([0-9]+(?:\\.[0-9]+)?)$', - re.M, - ) - match = pattern.search(text) - return float(match.group(1)) if match else 0.0 - -ok = counter("ok") -failed = counter("failed") -if status == "ok": - ok += 1 -else: - failed += 1 -payload = ( - "# TYPE platform_quality_gate_runs_total counter\\n" - f'platform_quality_gate_runs_total{{suite="{suite}",status="ok"}} {int(ok)}\\n' - f'platform_quality_gate_runs_total{{suite="{suite}",status="failed"}} {int(failed)}\\n' -) -req = urllib.request.Request( - f"{gateway}/metrics/job/platform-quality-ci/suite/{suite}", - data=payload.encode("utf-8"), - method="POST", - headers={"Content-Type": "text/plain"}, -) -urllib.request.urlopen(req, timeout=10).read() -PY - ''' - } - } always { script { if (fileExists('build.env')) { diff --git a/scripts/publish_test_metrics.py b/scripts/publish_test_metrics.py index 9a8a38d..582f767 100644 --- a/scripts/publish_test_metrics.py +++ b/scripts/publish_test_metrics.py @@ -19,6 +19,10 @@ import xml.etree.ElementTree as ET from pathlib import Path +SOURCE_SUFFIXES = {".py", ".js", ".mjs", ".ts", ".tsx", ".json", ".yaml", ".yml", ".sh"} +SKIP_DIRS = {".git", ".venv", "venv", "node_modules", "build", "dist", "__pycache__", ".pytest_cache"} + + def _as_int(node: ET.Element, name: str) -> int: raw = node.attrib.get(name) or "0" try: @@ -61,6 +65,25 @@ def _load_coverage_percent(path: Path) -> float: return 0.0 +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 _read_text(url: str) -> str: try: with urllib.request.urlopen(url, timeout=10) as resp: @@ -110,10 +133,15 @@ def main() -> int: junit_path = Path(os.getenv("JUNIT_PATH", "build/junit.xml")) coverage_path = Path(os.getenv("COVERAGE_PATH", "build/coverage.json")) + repo_root = Path(__file__).resolve().parents[1] totals = _load_junit(junit_path) coverage_pct = _load_coverage_percent(coverage_path) + over_500 = _count_lines_over_limit(repo_root) passed = max(totals["tests"] - totals["failures"] - totals["errors"] - totals["skipped"], 0) outcome = "ok" if totals["tests"] > 0 and totals["failures"] == 0 and totals["errors"] == 0 else "failed" + 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" metrics = _read_text(f"{pushgateway_url}/metrics") ok_count = _counter(metrics, suite, "ok") @@ -135,6 +163,14 @@ def main() -> int: f'atlasbot_quality_gate_tests_total{{suite="{suite}",result="skipped"}} {totals["skipped"]}', "# TYPE atlasbot_quality_gate_coverage_percent gauge", f'atlasbot_quality_gate_coverage_percent{{suite="{suite}"}} {coverage_pct:.3f}', + "# 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 atlasbot_quality_gate_checks_total gauge", + f'atlasbot_quality_gate_checks_total{{suite="{suite}",check="tests",result="{tests_check}"}} 1', + f'atlasbot_quality_gate_checks_total{{suite="{suite}",check="coverage",result="{coverage_check}"}} 1', + f'atlasbot_quality_gate_checks_total{{suite="{suite}",check="loc",result="{loc_check}"}} 1', ] ) + "\n"