|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Check for version updates in registered plugin repositories. |
| 3 | +
|
| 4 | +For each plugin with a GitHub source, fetches the remote plugin.json |
| 5 | +and compares the version against registry.yaml. If updates are found, |
| 6 | +updates registry.yaml and regenerates marketplace.json. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python3 scripts/check_versions.py [--registry registry.yaml] [--dry-run] |
| 10 | +""" |
| 11 | + |
| 12 | +import argparse |
| 13 | +import base64 |
| 14 | +import json |
| 15 | +import shutil |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | +from urllib.parse import quote |
| 19 | + |
| 20 | +import yaml |
| 21 | + |
| 22 | + |
| 23 | +def load_registry(path: str = "registry.yaml") -> dict: |
| 24 | + with open(path) as f: |
| 25 | + return yaml.safe_load(f) |
| 26 | + |
| 27 | + |
| 28 | +def save_registry(registry: dict, path: str = "registry.yaml"): |
| 29 | + with open(path, "w") as f: |
| 30 | + yaml.dump(registry, f, default_flow_style=False, sort_keys=False, allow_unicode=True) |
| 31 | + |
| 32 | + |
| 33 | +def fetch_remote_version(repo: str, ref: str = "main") -> str | None: |
| 34 | + """Fetch version from remote plugin.json via GitHub API.""" |
| 35 | + gh_bin = shutil.which("gh") |
| 36 | + if not gh_bin: |
| 37 | + return None |
| 38 | + result = subprocess.run( |
| 39 | + [gh_bin, "api", |
| 40 | + f"repos/{repo}/contents/.claude-plugin/plugin.json?ref={quote(ref, safe='')}", |
| 41 | + "--jq", ".content"], |
| 42 | + capture_output=True, text=True, |
| 43 | + timeout=30, |
| 44 | + ) |
| 45 | + if result.returncode != 0: |
| 46 | + return None |
| 47 | + |
| 48 | + try: |
| 49 | + content = base64.b64decode(result.stdout.strip()).decode() |
| 50 | + data = json.loads(content) |
| 51 | + return data.get("version") |
| 52 | + except (json.JSONDecodeError, ValueError, UnicodeDecodeError): |
| 53 | + return None |
| 54 | + |
| 55 | + |
| 56 | +def main(): |
| 57 | + parser = argparse.ArgumentParser(description=__doc__, |
| 58 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 59 | + parser.add_argument("--registry", default="registry.yaml") |
| 60 | + parser.add_argument("--dry-run", action="store_true", |
| 61 | + help="Show updates without modifying files") |
| 62 | + args = parser.parse_args() |
| 63 | + |
| 64 | + registry = load_registry(args.registry) |
| 65 | + updates = [] |
| 66 | + |
| 67 | + for plugin in registry.get("plugins", []): |
| 68 | + source = plugin.get("source") or {} |
| 69 | + if source.get("type") != "github": |
| 70 | + continue |
| 71 | + |
| 72 | + # Only check strict-mode plugins (they have their own plugin.json) |
| 73 | + if plugin.get("strict", True) is False: |
| 74 | + continue |
| 75 | + |
| 76 | + repo = source.get("repo") |
| 77 | + if not repo: |
| 78 | + print(f" SKIP: {plugin.get('name', '<unknown>')} (missing source.repo)") |
| 79 | + continue |
| 80 | + current = plugin.get("version", "0.0.0") |
| 81 | + remote = fetch_remote_version(repo, source.get("ref", "main")) |
| 82 | + |
| 83 | + name = plugin.get("name", "<unknown>") |
| 84 | + if remote is None: |
| 85 | + print(f" SKIP: {name} (could not fetch remote version)") |
| 86 | + continue |
| 87 | + |
| 88 | + if remote != current: |
| 89 | + print(f" UPDATE: {name} {current} -> {remote}") |
| 90 | + updates.append((plugin, remote)) |
| 91 | + else: |
| 92 | + print(f" OK: {name} {current}") |
| 93 | + |
| 94 | + if not updates: |
| 95 | + print("\nAll plugins up to date.") |
| 96 | + return |
| 97 | + |
| 98 | + if args.dry_run: |
| 99 | + print(f"\n{len(updates)} update(s) found (dry run, no changes made)") |
| 100 | + return |
| 101 | + |
| 102 | + # Apply updates |
| 103 | + for plugin, new_version in updates: |
| 104 | + plugin["version"] = new_version |
| 105 | + |
| 106 | + save_registry(registry, args.registry) |
| 107 | + print(f"\nUpdated {len(updates)} plugin(s) in {args.registry}") |
| 108 | + |
| 109 | + # Regenerate marketplace.json |
| 110 | + result = subprocess.run( |
| 111 | + [sys.executable, "scripts/sync_marketplace.py", "--registry", args.registry], |
| 112 | + capture_output=True, text=True, |
| 113 | + timeout=60, |
| 114 | + ) |
| 115 | + if result.returncode == 0: |
| 116 | + print(result.stdout.strip()) |
| 117 | + else: |
| 118 | + print(f"WARNING: failed to regenerate marketplace.json: {result.stderr}", |
| 119 | + file=sys.stderr) |
| 120 | + sys.exit(1) |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + main() |
0 commit comments