|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Run repository push gate checks aligned with CI lint/test workflows.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +ROOT = Path(__file__).resolve().parents[2] |
| 12 | + |
| 13 | + |
| 14 | +def run_step(command: list[str], *, title: str) -> None: |
| 15 | + print(f"\n==> {title}") |
| 16 | + print("$", " ".join(command)) |
| 17 | + subprocess.run(command, check=True, cwd=ROOT) |
| 18 | + |
| 19 | + |
| 20 | +def run_lint_gate() -> None: |
| 21 | + run_step( |
| 22 | + ["python", "-m", "isort", "--check-only", "lib", "apps"], |
| 23 | + title="Lint gate: isort", |
| 24 | + ) |
| 25 | + run_step( |
| 26 | + ["python", "-m", "black", "--check", "lib", "apps"], |
| 27 | + title="Lint gate: black", |
| 28 | + ) |
| 29 | + |
| 30 | + pylint_targets = [str(ROOT / "lib" / "src")] |
| 31 | + pylint_targets.extend( |
| 32 | + str(path) |
| 33 | + for path in sorted((ROOT / "apps").glob("*/src")) |
| 34 | + if (path / "pyproject.toml").exists() |
| 35 | + ) |
| 36 | + run_step( |
| 37 | + ["python", "-m", "pylint", *pylint_targets], |
| 38 | + title="Lint gate: pylint", |
| 39 | + ) |
| 40 | + |
| 41 | + run_step( |
| 42 | + [ |
| 43 | + "python", |
| 44 | + "scripts/ops/check_markdown_links.py", |
| 45 | + "--roots", |
| 46 | + "docs/governance", |
| 47 | + "docs/architecture/README.md", |
| 48 | + ], |
| 49 | + title="Lint gate: governance/architecture links", |
| 50 | + ) |
| 51 | + |
| 52 | + stale_tokens = ( |
| 53 | + "OPERATIONAL-WORKFLOWS.md", |
| 54 | + "REPOSITORY-SURFACES.md", |
| 55 | + "governance-map.md", |
| 56 | + ) |
| 57 | + agents_root = ROOT / ".github" / "agents" |
| 58 | + stale_refs: list[str] = [] |
| 59 | + if agents_root.exists(): |
| 60 | + for file_path in sorted(agents_root.rglob("*.md")): |
| 61 | + content = file_path.read_text(encoding="utf-8", errors="ignore") |
| 62 | + if any(token in content for token in stale_tokens): |
| 63 | + stale_refs.append(str(file_path.relative_to(ROOT))) |
| 64 | + |
| 65 | + if stale_refs: |
| 66 | + print("\nFound stale canonical governance references:") |
| 67 | + for item in stale_refs: |
| 68 | + print(f"- {item}") |
| 69 | + raise RuntimeError("Stale canonical governance references found in .github/agents") |
| 70 | + |
| 71 | + |
| 72 | +def run_test_gate() -> None: |
| 73 | + run_step( |
| 74 | + ["pytest", "lib/tests", "--maxfail=1"], |
| 75 | + title="Test gate: lib tests", |
| 76 | + ) |
| 77 | + |
| 78 | + app_test_dirs = [ |
| 79 | + str(path) |
| 80 | + for path in sorted((ROOT / "apps").glob("*/tests")) |
| 81 | + if path.is_dir() and path.name == "tests" |
| 82 | + ] |
| 83 | + run_step( |
| 84 | + ["pytest", *app_test_dirs, "--ignore=apps/ui/tests"], |
| 85 | + title="Test gate: app tests (excluding UI tests)", |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +def main() -> int: |
| 90 | + try: |
| 91 | + run_lint_gate() |
| 92 | + run_test_gate() |
| 93 | + except subprocess.CalledProcessError as exc: |
| 94 | + print(f"\nPush gate failed at exit code {exc.returncode}") |
| 95 | + return exc.returncode |
| 96 | + except Exception as exc: # pylint: disable=broad-exception-caught |
| 97 | + print(f"\nPush gate failed: {exc}") |
| 98 | + return 1 |
| 99 | + |
| 100 | + print("\nPush gate passed: lint + test checks match CI workflow expectations.") |
| 101 | + return 0 |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + sys.exit(main()) |
0 commit comments