diff --git a/Jenkinsfile b/Jenkinsfile index fb6e006..eb51964 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -42,6 +42,8 @@ spec: environment { SUITE_NAME = 'pegasus' PUSHGATEWAY_URL = 'http://platform-quality-gateway.monitoring.svc.cluster.local:9091' + QUALITY_GATE_SONARQUBE_REPORT = 'build/sonarqube-quality-gate.json' + QUALITY_GATE_IRONBANK_REPORT = 'build/ironbank-compliance.json' } options { @@ -59,6 +61,73 @@ spec: } } + stage('Collect SonarQube evidence') { + steps { + container('publisher') { + sh ''' + set -eu + mkdir -p build + python3 - <<'PY' +import base64 +import json +import os +import urllib.parse +import urllib.request + +host = os.getenv('SONARQUBE_HOST_URL', '').strip().rstrip('/') +project_key = os.getenv('SONARQUBE_PROJECT_KEY', '').strip() +token = os.getenv('SONARQUBE_TOKEN', '').strip() +report_path = os.getenv('QUALITY_GATE_SONARQUBE_REPORT', 'build/sonarqube-quality-gate.json') +payload = {"status": "ERROR", "note": "missing SONARQUBE_HOST_URL and/or SONARQUBE_PROJECT_KEY"} +if host and project_key: + query = urllib.parse.urlencode({"projectKey": project_key}) + request = urllib.request.Request(f"{host}/api/qualitygates/project_status?{query}", method="GET") + if token: + encoded = base64.b64encode(f"{token}:".encode("utf-8")).decode("utf-8") + request.add_header("Authorization", f"Basic {encoded}") + try: + with urllib.request.urlopen(request, timeout=12) as response: + payload = json.loads(response.read().decode("utf-8")) + except Exception as exc: # noqa: BLE001 + payload = {"status": "ERROR", "error": str(exc)} +with open(report_path, "w", encoding="utf-8") as handle: + json.dump(payload, handle, indent=2, sort_keys=True) + handle.write("\\n") +PY + ''' + } + } + } + + stage('Collect Supply Chain evidence') { + steps { + container('publisher') { + sh ''' + set -eu + mkdir -p build + python3 - <<'PY' +import json +import os +from pathlib import Path + +report_path = Path(os.getenv('QUALITY_GATE_IRONBANK_REPORT', 'build/ironbank-compliance.json')) +if report_path.exists(): + raise SystemExit(0) +status = os.getenv('IRONBANK_COMPLIANCE_STATUS', '').strip() +compliant = os.getenv('IRONBANK_COMPLIANT', '').strip().lower() +payload = {"status": status or "unknown", "compliant": compliant in {"1", "true", "yes", "on"} if compliant else None} +payload = {k: v for k, v in payload.items() if v is not None} +if "status" not in payload: + payload["status"] = "unknown" +payload["note"] = "Set IRONBANK_COMPLIANCE_STATUS/IRONBANK_COMPLIANT or write build/ironbank-compliance.json in image-building repos." +report_path.parent.mkdir(parents=True, exist_ok=True) +report_path.write_text(json.dumps(payload, indent=2, sort_keys=True) + "\\n", encoding="utf-8") +PY + ''' + } + } + } + stage('Backend unit tests') { steps { container('go-tester') { @@ -110,7 +179,7 @@ spec: } } - stage('Quality gate report') { + stage('Run quality gate') { steps { container('publisher') { sh ''' @@ -132,7 +201,7 @@ spec: } } - stage('Quality gate') { + stage('Enforce quality gate') { steps { container('publisher') { sh ''' diff --git a/scripts/publish_test_metrics.py b/scripts/publish_test_metrics.py index e49223a..7673f0c 100755 --- a/scripts/publish_test_metrics.py +++ b/scripts/publish_test_metrics.py @@ -23,6 +23,7 @@ from pathlib import Path SOURCE_SCAN_ROOTS = ("backend", "frontend/src", "scripts", "testing") SOURCE_EXTENSIONS = {".go", ".py", ".ts", ".tsx", ".sh"} +QUALITY_SUCCESS_STATES = {"ok", "pass", "passed", "success", "compliant"} def _escape_label(value: str) -> str: @@ -168,8 +169,48 @@ def _count_source_files_over_limit(repo_root: Path, max_lines: int = 500) -> int return count +def _load_json(path: Path) -> dict | None: + if not path.exists(): + return None + try: + payload = json.loads(path.read_text(encoding="utf-8")) + except Exception: + return None + return payload if isinstance(payload, dict) else None + + +def _sonarqube_check_status(build_dir: Path) -> str: + report = _load_json(Path(os.getenv("QUALITY_GATE_SONARQUBE_REPORT", str(build_dir / "sonarqube-quality-gate.json")))) + if not report: + return "not_applicable" + status_candidates = [ + report.get("status"), + ((report.get("projectStatus") or {}).get("status") if isinstance(report.get("projectStatus"), dict) else None), + ((report.get("qualityGate") or {}).get("status") if isinstance(report.get("qualityGate"), dict) else None), + ] + for value in status_candidates: + if isinstance(value, str): + return "ok" if value.strip().lower() in QUALITY_SUCCESS_STATES else "failed" + return "failed" + + +def _supply_chain_check_status(build_dir: Path) -> str: + report = _load_json(Path(os.getenv("QUALITY_GATE_IRONBANK_REPORT", str(build_dir / "ironbank-compliance.json")))) + if not report: + return "not_applicable" + compliant = report.get("compliant") + if isinstance(compliant, bool): + return "ok" if compliant else "failed" + status_candidates = [report.get("status"), report.get("result"), report.get("compliance")] + for value in status_candidates: + if isinstance(value, str): + return "ok" if value.strip().lower() in QUALITY_SUCCESS_STATES else "failed" + return "failed" + + def main() -> int: repo_root = Path(__file__).resolve().parents[1] + build_dir = repo_root / "build" suite = os.getenv("SUITE_NAME", "pegasus") pushgateway_url = os.getenv( "PUSHGATEWAY_URL", "http://platform-quality-gateway.monitoring.svc.cluster.local:9091" @@ -226,6 +267,15 @@ def main() -> int: and totals["errors"] == 0 else "failed" ) + checks = { + "tests": "ok" if outcome == "ok" else "failed", + "coverage": "ok" if coverage_pct >= 95.0 else "failed", + "loc": "ok" if source_lines_over_500 == 0 else "failed", + "docs_naming": "ok" if not gate_issues else "failed", + "gate_glue": "ok", + "sonarqube": _sonarqube_check_status(build_dir), + "supply_chain": _supply_chain_check_status(build_dir), + } job_name = "platform-quality-ci" ok_count = _fetch_existing_counter( @@ -266,9 +316,14 @@ 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 pegasus_quality_gate_checks_total gauge", "# TYPE pegasus_quality_gate_build_info gauge", f"pegasus_quality_gate_build_info{_label_str(labels)} 1", ] + payload_lines.extend( + f'pegasus_quality_gate_checks_total{{suite="{suite}",check="{check_name}",result="{check_status}"}} 1' + for check_name, check_status in checks.items() + ) payload = "\n".join(payload_lines) + "\n" push_url = f"{pushgateway_url.rstrip('/')}/metrics/job/{job_name}/suite/{suite}"