mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-15 01:10:58 +01:00
"Are we on the newest version?" was not mechanically answerable across these repos: constraint styles had drifted per-ecosystem and a caret range is "newest-compatible" by definition, which gives a checker nothing to compare. This gate makes it answerable and then enforces the answer. Checks two things at once -- that every dependency carries an exact pin, and that the pin equals its registry's latest STABLE release -- across pub.dev, PyPI, npm, crates.io, the Go proxy, kuhyx/utils git tags, and the Flutter / Node / Python toolchains. Three deliberate design points: * Pre-releases are rejected per-ecosystem, because every ecosystem leaks them differently and each leak fails GREEN. npm lets a maintainer point `dist-tags.latest` at an rc; crates.io's `max_version` includes them; PyPI's legacy `info.version` includes yanked releases. Unit-tested with fixtures, since an end-to-end run cannot surface a wrong "up to date". * Offline, the gate serves an expired cache entry and PASSES with a DEGRADED banner (exit 3 only under --strict, which is CI). `--no-verify` is banned here, so a hook that hard-failed offline would leave no way to commit at all. * The allowlist has two entry classes. `blocked_by: transitive:<pkg>@<ver>` is a predicate that clears itself when upstream moves and must NOT carry a date; everything else is discretionary and requires `expires`, capped at 90 days. Exact pins plus latest-everywhere is provably unsatisfiable when a dependency internally constrains a shared transitive, and expiring the predicate class on a calendar would turn the gate red for a reason no commit could fix. An entry with nothing left to excuse is itself an error. One hook id, one shim, one shared implementation -- always delegating, so it cannot fork the way the 250-line cap forked into four ids, two of which were independent reimplementations. 256 tests, 100% branch coverage on every module of the gate. Also adds the workflow that runs them, which incidentally gives md_naming's 71 tests their first CI run. Not yet installed in any repo: a repo that gets the gate before its deps are bumped is instantly and permanently red, so the gate and the upgrade land together, per repo. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CRykabMwPgDzFHcARoqpaK
159 lines
5.6 KiB
Python
159 lines
5.6 KiB
Python
"""Gate: fail when any dependency is behind its ecosystem's latest stable.
|
|
|
|
Exit codes are the adjudication — no judgement call, no model in the loop:
|
|
|
|
0 everything current, or only validly-allowlisted staleness
|
|
1 a dependency is behind latest stable / unpinned / lock-mismatched
|
|
2 the allowlist is malformed, expired, over the 90-day cap, or stale
|
|
3 cannot determine: no network and no usable cache
|
|
|
|
CI runs `--all --strict` and treats 1/2/3 as failure. Pre-commit runs without
|
|
`--strict`, where exit 3 degrades to 0 with a loud banner: `--no-verify` is
|
|
banned here, so a hook that hard-fails offline would leave no way to commit.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
|
|
from dep_freshness import report
|
|
from dep_freshness.allowlist import AllowlistError, load
|
|
from dep_freshness.discover import find_manifests, is_manifest, parse_manifest
|
|
from dep_freshness.evaluate import judge
|
|
from dep_freshness.models import Finding, Severity
|
|
from dep_freshness.registries import http
|
|
from dep_freshness.resolve import Resolver
|
|
|
|
|
|
def repo_root(start: Path) -> Path:
|
|
try:
|
|
result = subprocess.run(
|
|
["git", "-C", str(start), "rev-parse", "--show-toplevel"],
|
|
capture_output=True, text=True, check=False,
|
|
)
|
|
except OSError:
|
|
return start
|
|
if result.returncode != 0:
|
|
return start
|
|
return Path(result.stdout.strip() or start)
|
|
|
|
|
|
def _excuse(findings: list[Finding], entries) -> tuple[list[Finding], list]:
|
|
"""Split findings into (still failing, excused), and flag dead entries.
|
|
|
|
A `transitive:` entry clears itself the moment its dependency is no longer
|
|
stale — that is what makes it a predicate rather than a date. An entry with
|
|
nothing left to excuse is allowlist rot and exits 2.
|
|
"""
|
|
by_label = {f"{e.ecosystem}:{e.package}": e for e in entries}
|
|
used: set[str] = set()
|
|
failing: list[Finding] = []
|
|
excused: list[Finding] = []
|
|
for finding in findings:
|
|
entry = by_label.get(finding.label)
|
|
if entry is None:
|
|
failing.append(finding)
|
|
continue
|
|
used.add(finding.label)
|
|
excused.append(Finding(
|
|
finding.dep, finding.severity, finding.latest, finding.detail,
|
|
excused=entry.reason,
|
|
))
|
|
dead = [e for e in entries if f"{e.ecosystem}:{e.package}" not in used]
|
|
return failing, dead
|
|
|
|
|
|
def collect(targets: list[Path], resolver: Resolver) -> list[Finding]:
|
|
deps = [dep for path in targets for dep in parse_manifest(path)]
|
|
resolver.prefetch(deps)
|
|
findings = []
|
|
for dep in deps:
|
|
finding = judge(dep, resolver.latest(dep))
|
|
if finding is not None:
|
|
findings.append(finding)
|
|
return findings
|
|
|
|
|
|
def _targets(args, root: Path) -> list[Path]:
|
|
if args.all:
|
|
return find_manifests(root)
|
|
return [p for p in args.paths if p.is_file() and is_manifest(p)]
|
|
|
|
|
|
def build_parser() -> argparse.ArgumentParser:
|
|
parser = argparse.ArgumentParser(
|
|
description="Fail if any dependency is behind latest stable."
|
|
)
|
|
parser.add_argument("paths", nargs="*", type=Path)
|
|
parser.add_argument("--all", action="store_true",
|
|
help="check every manifest in the repo")
|
|
parser.add_argument("--strict", action="store_true",
|
|
help="CI mode: an undeterminable answer is a failure")
|
|
parser.add_argument("--refresh", action="store_true",
|
|
help="ignore the cache and re-fetch every version")
|
|
parser.add_argument("--offline", action="store_true",
|
|
help="never touch the network; cache only")
|
|
parser.add_argument("--exceptions-only", action="store_true",
|
|
help="report the allowlist and nothing else")
|
|
parser.add_argument("--json", action="store_true", help="machine-readable output")
|
|
return parser
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
args = build_parser().parse_args(argv)
|
|
if not args.all and not args.paths and not args.exceptions_only:
|
|
build_parser().error("give manifest paths, --all, or --exceptions-only")
|
|
|
|
root = repo_root(Path.cwd())
|
|
if args.offline:
|
|
http.force_offline(True)
|
|
|
|
try:
|
|
entries = load(root)
|
|
except AllowlistError as exc:
|
|
print(f"Allowlist ERROR: {exc}", file=sys.stderr)
|
|
return 2
|
|
|
|
if args.exceptions_only:
|
|
if args.json:
|
|
print(report.as_json([], entries, 0))
|
|
else:
|
|
for line in report.machine_lines(entries):
|
|
print(line, file=sys.stderr)
|
|
return 0
|
|
|
|
resolver = Resolver(refresh=args.refresh)
|
|
findings = collect(_targets(args, root), resolver)
|
|
failing, dead = _excuse(findings, entries)
|
|
|
|
still_blocking = {f.label: True for f in findings}
|
|
report.exceptions_block(entries, still_blocking)
|
|
|
|
unknown = [f for f in failing if f.severity is Severity.UNKNOWN]
|
|
real = [f for f in failing if f.severity is not Severity.UNKNOWN]
|
|
|
|
if dead:
|
|
for entry in dead:
|
|
print(
|
|
f"Allowlist ERROR: {entry.ecosystem}:{entry.package} is no longer "
|
|
f"stale — remove the entry from {entry.source}",
|
|
file=sys.stderr,
|
|
)
|
|
return 2
|
|
|
|
if args.json:
|
|
code = 1 if real else (3 if unknown and args.strict else 0)
|
|
print(report.as_json(failing, entries, code))
|
|
return code
|
|
|
|
if real:
|
|
report.violations(real, root)
|
|
return 1
|
|
if unknown or resolver.degraded:
|
|
report.degraded(resolver.degraded or [f.label for f in unknown])
|
|
return 3 if args.strict else 0
|
|
return 0
|