mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-10 19:07:17 +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
118 lines
4.5 KiB
Python
118 lines
4.5 KiB
Python
"""Parse and validate `dependency-freshness.allowlist.yaml`.
|
|
|
|
Two entry classes, and the distinction is the whole point:
|
|
|
|
* `blocked_by: transitive:<pkg>@<ver>` is a **predicate**. The blocker is
|
|
re-evaluated every run and the entry clears itself when upstream moves. It
|
|
never expires on a calendar, because a date nobody can act on turns the gate
|
|
red for a reason no commit can fix.
|
|
* anything else is **discretionary**: it needs `expires`, capped at 90 days,
|
|
and fails once past.
|
|
|
|
Malformed, expired, over-cap and no-longer-needed entries all exit 2 — an
|
|
allowlist that rots silently is a suppression, not an exception.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import date, datetime
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
|
|
from dep_freshness._tables import ALLOWLIST_FILE, ALLOWLIST_MAX_DAYS
|
|
from dep_freshness.models import Exception_
|
|
|
|
REQUIRED = ("ecosystem", "package", "pinned", "reason", "blocked_by")
|
|
|
|
|
|
class AllowlistError(Exception):
|
|
"""The allowlist itself is wrong; the gate refuses to run against it."""
|
|
|
|
|
|
def path_for(root: Path) -> Path:
|
|
return root / ALLOWLIST_FILE
|
|
|
|
|
|
def _parse_date(raw: object, where: str) -> date:
|
|
if isinstance(raw, date) and not isinstance(raw, datetime):
|
|
return raw
|
|
try:
|
|
return datetime.strptime(str(raw), "%Y-%m-%d").date()
|
|
except ValueError as exc:
|
|
raise AllowlistError(f"{where}: expires must be YYYY-MM-DD, got {raw!r}") from exc
|
|
|
|
|
|
def _entry(raw: object, source: Path, position: int) -> Exception_:
|
|
where = f"{source}: exceptions[{position}]"
|
|
if not isinstance(raw, dict):
|
|
raise AllowlistError(f"{where}: each exception must be a mapping")
|
|
missing = [field for field in REQUIRED if not str(raw.get(field, "")).strip()]
|
|
if missing:
|
|
raise AllowlistError(f"{where}: missing required field(s): {', '.join(missing)}")
|
|
entry = Exception_(
|
|
ecosystem=str(raw["ecosystem"]).strip(),
|
|
package=str(raw["package"]).strip(),
|
|
pinned=str(raw["pinned"]).strip(),
|
|
reason=str(raw["reason"]).strip(),
|
|
blocked_by=str(raw["blocked_by"]).strip(),
|
|
latest_known=(str(raw["latest_known"]).strip()
|
|
if raw.get("latest_known") else None),
|
|
expires=(str(raw["expires"]).strip() if raw.get("expires") else None),
|
|
source=source,
|
|
)
|
|
if entry.transitive:
|
|
if entry.expires:
|
|
raise AllowlistError(
|
|
f"{where}: a transitive: entry must NOT set expires -- it clears "
|
|
"itself when the blocker lifts, and a date nobody can act on "
|
|
"only turns the gate red for no actionable reason"
|
|
)
|
|
if not entry.blocker or not all(entry.blocker):
|
|
raise AllowlistError(
|
|
f"{where}: blocked_by must read transitive:<package>@<version>"
|
|
)
|
|
return entry
|
|
if not entry.expires:
|
|
raise AllowlistError(f"{where}: a discretionary entry requires expires")
|
|
return entry
|
|
|
|
|
|
def check_expiry(entry: Exception_, today: date | None = None) -> None:
|
|
"""Raise if a discretionary entry is past due or reaches too far out."""
|
|
if entry.transitive or not entry.expires:
|
|
return
|
|
now = today or date.today()
|
|
when = _parse_date(entry.expires, f"{entry.source}: {entry.package}")
|
|
if when < now:
|
|
raise AllowlistError(
|
|
f"{entry.source}: {entry.ecosystem}:{entry.package} expired on {when}"
|
|
)
|
|
if (when - now).days > ALLOWLIST_MAX_DAYS:
|
|
raise AllowlistError(
|
|
f"{entry.source}: {entry.ecosystem}:{entry.package} expires {when}, "
|
|
f"more than {ALLOWLIST_MAX_DAYS} days out"
|
|
)
|
|
|
|
|
|
def load(root: Path) -> list[Exception_]:
|
|
"""Every exception declared for `root`; [] when there is no allowlist."""
|
|
source = path_for(root)
|
|
if not source.is_file():
|
|
return []
|
|
try:
|
|
data = yaml.safe_load(source.read_text(encoding="utf-8"))
|
|
except (OSError, yaml.YAMLError) as exc:
|
|
raise AllowlistError(f"{source}: unreadable ({exc})") from exc
|
|
if data is None:
|
|
return []
|
|
if not isinstance(data, dict) or "exceptions" not in data:
|
|
raise AllowlistError(f"{source}: expected a top-level 'exceptions:' list")
|
|
raw_entries = data.get("exceptions") or []
|
|
if not isinstance(raw_entries, list):
|
|
raise AllowlistError(f"{source}: 'exceptions' must be a list")
|
|
entries = [_entry(raw, source, i) for i, raw in enumerate(raw_entries)]
|
|
for entry in entries:
|
|
check_expiry(entry)
|
|
return entries
|