"""Everything CI will check, run here first. CI is the gate, but it runs after the push - and on this repository a push is a deploy to two providers. Finding a leaked key or a failing suite here costs two minutes; finding it there costs a rollback. python scripts/preflight.py # from the repository root Exits non-zero on the first hard failure, so it can be chained before a push. """ from __future__ import annotations import re import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent # Provider key shapes, matching .github/workflows/ci.yml. Entropy heuristics # find more and are wrong more often; a false positive that blocks a deploy is # worse than a rule that reads clearly to whoever trips it. SECRETS = re.compile( r"sk-or-v1-[A-Za-z0-9]{32,}" r"|sk-[A-Za-z0-9]{40,}" r"|eyJhbGciOi[A-Za-z0-9_-]{20,}" r"|BEGIN (?:RSA |EC )?PRIVATE KEY" ) # Files that legitimately carry those shapes: templates, and this file. ALLOWED = ("/.env.example", "scripts/preflight.py", ".github/workflows/ci.yml") GREEN, RED, YELLOW, DIM, OFF = "\033[32m", "\033[31m", "\033[33m", "\033[2m", "\033[0m" def say(ok: bool | None, label: str, detail: str = "") -> None: mark = {True: f"{GREEN}ok {OFF}", False: f"{RED}FAIL{OFF}", None: f"{YELLOW}skip{OFF}"}[ok] print(f" {mark} {label}" + (f" {DIM}{detail}{OFF}" if detail else "")) def run(cmd: list[str], cwd: Path) -> tuple[int, str]: # `shell=True` on Windows only: npm and git ship as .cmd shims there, and # CreateProcess cannot execute a shim. Everywhere else a shell would only # add a quoting hazard. done = subprocess.run(cmd, cwd=cwd, capture_output=True, text=True, encoding="utf-8", errors="replace", shell=(sys.platform == "win32")) return done.returncode, (done.stdout or "") + (done.stderr or "") def tracked() -> list[str]: code, out = run(["git", "ls-files"], ROOT) return out.splitlines() if code == 0 else [] def venv_python() -> Path | None: for candidate in (ROOT / "apps/api/.venv/Scripts/python.exe", ROOT / "apps/api/.venv/bin/python"): if candidate.exists(): return candidate return None def main() -> int: failures: list[str] = [] files = tracked() print(f"\n{DIM}ContiAI — preflight{OFF}\n") # ── the repository ──────────────────────────────────────────────────── print("repository") if not files: say(False, "git", "no repository here, or nothing tracked") return 1 leaks = [f for f in files if f.endswith((".env", ".env.local")) or f.endswith(".db") or "serviceAccount" in f or f.endswith(".pem")] say(not leaks, "no state or credential files tracked", ", ".join(leaks[:3]) if leaks else f"{len(files)} files") if leaks: failures.append("gitignore") # Size is a proxy for a dependency directory having slipped in. total = sum((ROOT / f).stat().st_size for f in files if (ROOT / f).exists()) heavy = total > 40 * 1024 * 1024 say(not heavy, "tree size", f"{total / 1024 / 1024:.1f} MB") if heavy: failures.append("tree size") found: list[str] = [] for name in files: if any(a in "/" + name for a in ALLOWED): continue path = ROOT / name if not path.exists() or path.stat().st_size > 2_000_000: continue try: text = path.read_text(encoding="utf-8", errors="ignore") except OSError: continue if SECRETS.search(text): found.append(name) say(not found, "no credential in any tracked file", ", ".join(found[:3])) if found: failures.append("secret scan") # ── things the deploy needs to exist ────────────────────────────────── print("\ndeployment files") for name, why in [ ("render.yaml", "Render reads this from the root"), ("apps/web/vercel.json", "build and SPA rewrite"), ("apps/api/.env.example", "the variable reference"), ("apps/web/.env.example", ""), ("apps/api/pyproject.toml", ""), ("docs/deploy.html", "the runbook"), (".github/workflows/ci.yml", ""), ]: present = (ROOT / name).exists() say(present, name, why) if not present: failures.append(name) # The driver Alembic needs is easy to lose in a dependency cleanup, and it # only fails at boot on Render - after the build has already succeeded. pyproject = (ROOT / "apps/api/pyproject.toml").read_text(encoding="utf-8") has_sync = "psycopg2" in pyproject say(has_sync, "sync postgres driver", "alembic connects without +asyncpg") if not has_sync: failures.append("psycopg2") # ── the api ─────────────────────────────────────────────────────────── print("\napi") python = venv_python() if python is None: say(None, "virtualenv", "not found - skipping lint and tests") else: api = ROOT / "apps/api" code, out = run([str(python), "-m", "ruff", "check", "app", "tests"], api) say(code == 0, "ruff", "" if code == 0 else out.strip().splitlines()[-1][:70]) if code: failures.append("ruff") code, out = run([str(python), "-m", "pytest", "-q"], api) summary = next((ln for ln in reversed(out.splitlines()) if "passed" in ln or "failed" in ln), "") say(code == 0, "pytest", summary.strip()[:70]) if code: failures.append("pytest") # ── the web app ─────────────────────────────────────────────────────── print("\nweb") if not (ROOT / "apps/web/node_modules").exists(): say(None, "node_modules", "not installed - skipping build") else: code, out = run(["npm", "run", "build"], ROOT / "apps/web") say(code == 0, "vite build", "" if code == 0 else out.strip().splitlines()[-1][:70]) if code: failures.append("build") # ── verdict ─────────────────────────────────────────────────────────── print() if failures: print(f"{RED}not ready{OFF} — {', '.join(dict.fromkeys(failures))}\n") return 1 code, out = run(["git", "status", "--porcelain"], ROOT) dirty = bool(out.strip()) print(f"{GREEN}ready to push{OFF}\n") if dirty: print(f"{DIM} uncommitted changes present:{OFF}") print(" git add -A && git commit -m \"…\"") print(f"{DIM} then:{OFF}") print(" git push -u origin main\n") return 0 if __name__ == "__main__": sys.exit(main())