|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""PR gate: require a valid evidence package when catalog status is promoted.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import os |
| 7 | +import re |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +GATE_STAGES = frozenset( |
| 13 | + { |
| 14 | + "ai_monitored_candidate", |
| 15 | + "shadow_candidate", |
| 16 | + "live_candidate", |
| 17 | + "runtime_enabled", |
| 18 | + } |
| 19 | +) |
| 20 | +STATUS_ADDED_RE = re.compile(r'^\+.*status="([^"]+)"') |
| 21 | +EVIDENCE_SUFFIXES = {".json", ".toml"} |
| 22 | + |
| 23 | + |
| 24 | +def _git_diff(base_ref: str) -> str: |
| 25 | + result = subprocess.run( |
| 26 | + ["git", "diff", f"origin/{base_ref}...HEAD", "--", "src"], |
| 27 | + capture_output=True, |
| 28 | + text=True, |
| 29 | + check=False, |
| 30 | + ) |
| 31 | + if result.returncode != 0: |
| 32 | + result = subprocess.run( |
| 33 | + ["git", "diff", f"{base_ref}...HEAD", "--", "src"], |
| 34 | + capture_output=True, |
| 35 | + text=True, |
| 36 | + check=True, |
| 37 | + ) |
| 38 | + return result.stdout |
| 39 | + |
| 40 | + |
| 41 | +def _promotion_detected(diff: str) -> bool: |
| 42 | + if "status=" not in diff: |
| 43 | + return False |
| 44 | + return any(match.group(1) in GATE_STAGES for line in diff.splitlines() if (match := STATUS_ADDED_RE.match(line))) |
| 45 | + |
| 46 | + |
| 47 | +def _evidence_paths_from_diff(diff: str) -> list[Path]: |
| 48 | + paths: list[Path] = [] |
| 49 | + for line in diff.splitlines(): |
| 50 | + if not line.startswith("+++ b/"): |
| 51 | + continue |
| 52 | + candidate = Path(line[6:]) |
| 53 | + if candidate.suffix.lower() not in EVIDENCE_SUFFIXES: |
| 54 | + continue |
| 55 | + if "evidence" in candidate.parts or candidate.parent.name == "evidence": |
| 56 | + paths.append(candidate) |
| 57 | + return paths |
| 58 | + |
| 59 | + |
| 60 | +def _discover_evidence_files(diff: str) -> list[Path]: |
| 61 | + discovered = _evidence_paths_from_diff(diff) |
| 62 | + for folder in (Path("docs/evidence"), Path("evidence")): |
| 63 | + if folder.is_dir(): |
| 64 | + discovered.extend(path for path in folder.iterdir() if path.suffix.lower() in EVIDENCE_SUFFIXES) |
| 65 | + explicit = os.environ.get("EVIDENCE_PACKAGE_PATH", "").strip() |
| 66 | + if explicit: |
| 67 | + discovered.append(Path(explicit)) |
| 68 | + return sorted({path for path in discovered if path.exists()}) |
| 69 | + |
| 70 | + |
| 71 | +def _validate_with_lifecycle(path: Path) -> tuple[bool, list[str]]: |
| 72 | + from quant_platform_kit.strategy_lifecycle.evidence_gate import validate_evidence_package_file |
| 73 | + |
| 74 | + result = validate_evidence_package_file(path) |
| 75 | + issues = list(result.issues) |
| 76 | + return result.valid, issues |
| 77 | + |
| 78 | + |
| 79 | +def _validate_with_promotion_standard(path: Path) -> tuple[bool, list[str]]: |
| 80 | + script = Path("external/QuantPlatformKit/scripts/validate_strategy_evidence_package.py") |
| 81 | + if not script.exists(): |
| 82 | + return True, [] |
| 83 | + result = subprocess.run( |
| 84 | + [sys.executable, str(script), str(path)], |
| 85 | + capture_output=True, |
| 86 | + text=True, |
| 87 | + check=False, |
| 88 | + ) |
| 89 | + if result.returncode == 0: |
| 90 | + return True, [] |
| 91 | + issues = [line for line in result.stderr.splitlines() if line.strip()] |
| 92 | + issues.extend(line for line in result.stdout.splitlines() if line.strip()) |
| 93 | + return False, issues or ["promotion evidence package validation failed"] |
| 94 | + |
| 95 | + |
| 96 | +def main() -> int: |
| 97 | + base_ref = os.environ.get("GITHUB_BASE_REF", "main").strip() or "main" |
| 98 | + diff = _git_diff(base_ref) |
| 99 | + |
| 100 | + if not _promotion_detected(diff): |
| 101 | + print("[evidence-gate] No lifecycle status promotion detected; skipping validation") |
| 102 | + return 0 |
| 103 | + |
| 104 | + evidence_files = _discover_evidence_files(diff) |
| 105 | + if not evidence_files: |
| 106 | + print( |
| 107 | + "::error::Catalog status promotion detected but no evidence package file was found. " |
| 108 | + "Add docs/evidence/<profile>.json with the 11 required artifacts.", |
| 109 | + file=sys.stderr, |
| 110 | + ) |
| 111 | + return 1 |
| 112 | + |
| 113 | + failed = False |
| 114 | + for path in evidence_files: |
| 115 | + lifecycle_ok, lifecycle_issues = _validate_with_lifecycle(path) |
| 116 | + standard_ok, standard_issues = _validate_with_promotion_standard(path) |
| 117 | + if lifecycle_ok and standard_ok: |
| 118 | + print(f"[evidence-gate] PASS {path}") |
| 119 | + continue |
| 120 | + failed = True |
| 121 | + print(f"[evidence-gate] FAIL {path}", file=sys.stderr) |
| 122 | + for issue in lifecycle_issues + standard_issues: |
| 123 | + print(f" - {issue}", file=sys.stderr) |
| 124 | + |
| 125 | + return 1 if failed else 0 |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + raise SystemExit(main()) |
0 commit comments