File size: 7,249 Bytes
7451968 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | """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())
|