|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Report drift between registry.json and each tool repo's GitHub About |
| 3 | +section (description, homepage, topics). Report-only: it never edits a |
| 4 | +repo, because applying changes needs an admin-scoped token per repo. |
| 5 | +
|
| 6 | +Usage: |
| 7 | + python scripts/check_fleet_about.py # human-readable report |
| 8 | + python scripts/check_fleet_about.py --json # machine-readable |
| 9 | + python scripts/check_fleet_about.py --strict # exit 1 when anything drifts |
| 10 | + python scripts/check_fleet_about.py --wording # also show description wording diffs |
| 11 | +
|
| 12 | +Requires the GitHub CLI (`gh`) authenticated with read access. |
| 13 | +""" |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import argparse |
| 17 | +import json |
| 18 | +import re |
| 19 | +import subprocess |
| 20 | +import sys |
| 21 | +from pathlib import Path |
| 22 | +from typing import Any, Callable |
| 23 | + |
| 24 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 25 | + |
| 26 | +Fetcher = Callable[[str], "dict[str, Any] | None"] |
| 27 | + |
| 28 | + |
| 29 | +def fetch_repo(repo: str) -> "dict[str, Any] | None": |
| 30 | + result = subprocess.run( |
| 31 | + ["gh", "repo", "view", repo, "--json", "description,homepageUrl,repositoryTopics"], |
| 32 | + capture_output=True, |
| 33 | + text=True, |
| 34 | + timeout=60, |
| 35 | + ) |
| 36 | + if result.returncode != 0: |
| 37 | + return None |
| 38 | + data = json.loads(result.stdout or "{}") |
| 39 | + return { |
| 40 | + "description": (data.get("description") or "").strip(), |
| 41 | + "homepage": (data.get("homepageUrl") or "").strip(), |
| 42 | + "topics": sorted(t["name"] for t in (data.get("repositoryTopics") or [])), |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | +_COUNT_PATTERNS = { |
| 47 | + "skills": re.compile(r"(\d+)\s+(?:AI\s+)?skills\b", re.IGNORECASE), |
| 48 | + "rules": re.compile(r"(\d+)\s+rules\b", re.IGNORECASE), |
| 49 | + "mcpTools": re.compile(r"(\d+)\s+(?:MCP\s+)?tools\b", re.IGNORECASE), |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +def stale_counts(entry: dict[str, Any], description: str) -> list[str]: |
| 54 | + """Counts quoted in a GitHub description that contradict the registry.""" |
| 55 | + out = [] |
| 56 | + for field, pattern in _COUNT_PATTERNS.items(): |
| 57 | + for match in pattern.finditer(description): |
| 58 | + quoted = int(match.group(1)) |
| 59 | + actual = int(entry.get(field) or 0) |
| 60 | + if actual and quoted != actual: |
| 61 | + out.append(f"description says {match.group(0)!r} but registry has {field}={actual}") |
| 62 | + return out |
| 63 | + |
| 64 | + |
| 65 | +def compare(entry: dict[str, Any], live: "dict[str, Any] | None", *, wording: bool = False) -> list[str]: |
| 66 | + """Human-readable differences between a registry entry and live About data. |
| 67 | +
|
| 68 | + Always flagged: unreadable repo, empty description or topics, counts in |
| 69 | + the description that contradict the registry, homepage mismatch, and |
| 70 | + registry topics missing on GitHub. Wording differences are only reported |
| 71 | + with ``wording=True``; GitHub descriptions are often deliberately richer.""" |
| 72 | + if live is None: |
| 73 | + return ["could not read repository (missing, private, or no access)"] |
| 74 | + diffs = [] |
| 75 | + if not live["description"]: |
| 76 | + diffs.append("GitHub description is empty") |
| 77 | + else: |
| 78 | + diffs.extend(stale_counts(entry, live["description"])) |
| 79 | + if wording and live["description"] != entry.get("description", "").strip(): |
| 80 | + diffs.append( |
| 81 | + "description differs:\n" |
| 82 | + f" registry: {entry.get('description', '')}\n" |
| 83 | + f" github: {live['description']}" |
| 84 | + ) |
| 85 | + want_home = (entry.get("homepage") or "").rstrip("/") |
| 86 | + if want_home and live["homepage"].rstrip("/") != want_home: |
| 87 | + diffs.append(f"homepage differs: registry {want_home!r}, github {live['homepage']!r}") |
| 88 | + want_topics = sorted(entry.get("topics") or []) |
| 89 | + if not live["topics"]: |
| 90 | + diffs.append("GitHub topics are empty") |
| 91 | + else: |
| 92 | + missing = sorted(set(want_topics) - set(live["topics"])) |
| 93 | + if missing: |
| 94 | + diffs.append(f"topics in registry but not on GitHub: {', '.join(missing)}") |
| 95 | + return diffs |
| 96 | + |
| 97 | + |
| 98 | +def check( |
| 99 | + entries: list[dict[str, Any]], fetch: Fetcher = fetch_repo, *, wording: bool = False |
| 100 | +) -> dict[str, list[str]]: |
| 101 | + report = {} |
| 102 | + for entry in entries: |
| 103 | + if entry.get("status") in ("archived",): |
| 104 | + continue |
| 105 | + diffs = compare(entry, fetch(entry["repo"]), wording=wording) |
| 106 | + if diffs: |
| 107 | + report[entry["repo"]] = diffs |
| 108 | + return report |
| 109 | + |
| 110 | + |
| 111 | +def main(argv: "list[str] | None" = None) -> int: |
| 112 | + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) |
| 113 | + parser.add_argument("--registry", type=Path, default=REPO_ROOT / "registry.json") |
| 114 | + parser.add_argument("--json", action="store_true", help="emit JSON") |
| 115 | + parser.add_argument("--strict", action="store_true", help="exit 1 when any repo drifts") |
| 116 | + parser.add_argument("--wording", action="store_true", help="also report description wording differences") |
| 117 | + args = parser.parse_args(argv) |
| 118 | + |
| 119 | + entries = json.loads(args.registry.read_text(encoding="utf-8")) |
| 120 | + report = check(entries, wording=args.wording) |
| 121 | + if args.json: |
| 122 | + print(json.dumps(report, indent=2)) |
| 123 | + elif not report: |
| 124 | + print(f"All {len(entries)} repos match registry.json.") |
| 125 | + else: |
| 126 | + for repo, diffs in report.items(): |
| 127 | + print(f"{repo}:") |
| 128 | + for d in diffs: |
| 129 | + print(f" - {d}") |
| 130 | + print(f"\n{len(report)} of {len(entries)} repos differ from registry.json.") |
| 131 | + return 1 if (args.strict and report) else 0 |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + sys.exit(main()) |
0 commit comments