|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import json |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +ROOT_SRC = Path(__file__).resolve().parents[1] / "src" |
| 11 | +sys.path.insert(0, str(ROOT_SRC)) |
| 12 | + |
| 13 | +from heavy_coder.github_repo_metadata import load_and_validate # noqa: E402 |
| 14 | + |
| 15 | + |
| 16 | +def current_topics(repo: str | None) -> list[str]: |
| 17 | + view_cmd = ["gh", "repo", "view", "--json", "repositoryTopics"] |
| 18 | + if repo: |
| 19 | + view_cmd.insert(3, repo) |
| 20 | + completed = subprocess.run( |
| 21 | + view_cmd, check=True, capture_output=True, text=True |
| 22 | + ) |
| 23 | + payload = json.loads(completed.stdout) |
| 24 | + raw = payload.get("repositoryTopics") or [] |
| 25 | + return sorted( |
| 26 | + str(item["name"]) for item in raw if isinstance(item, dict) and item.get("name") |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def build_gh_command( |
| 31 | + repo: str | None, |
| 32 | + description: str, |
| 33 | + topics: list[str], |
| 34 | + *, |
| 35 | + remove_topics: list[str] | None = None, |
| 36 | +) -> list[str]: |
| 37 | + cmd = ["gh", "repo", "edit"] |
| 38 | + if repo: |
| 39 | + cmd.append(repo) |
| 40 | + cmd.extend(["-d", description]) |
| 41 | + for topic in remove_topics or []: |
| 42 | + cmd.extend(["--remove-topic", topic]) |
| 43 | + for topic in topics: |
| 44 | + cmd.extend(["--add-topic", topic]) |
| 45 | + return cmd |
| 46 | + |
| 47 | + |
| 48 | +def main() -> int: |
| 49 | + parser = argparse.ArgumentParser( |
| 50 | + description="Apply github-repo-metadata.yaml to GitHub via gh repo edit." |
| 51 | + ) |
| 52 | + parser.add_argument("root", nargs="?", default=".") |
| 53 | + parser.add_argument( |
| 54 | + "--repo", |
| 55 | + default=None, |
| 56 | + help="Optional OWNER/REPO; defaults to current directory remote.", |
| 57 | + ) |
| 58 | + parser.add_argument( |
| 59 | + "--execute", |
| 60 | + action="store_true", |
| 61 | + help="Run gh; default is dry-run (print planned command).", |
| 62 | + ) |
| 63 | + args = parser.parse_args() |
| 64 | + root = Path(args.root).resolve() |
| 65 | + payload, errors = load_and_validate(root) |
| 66 | + if errors: |
| 67 | + print(json.dumps({"ok": False, "errors": errors}, indent=2, sort_keys=True)) |
| 68 | + return 1 |
| 69 | + |
| 70 | + description = str(payload["description"]).strip() |
| 71 | + topics = [str(t) for t in payload["topics"]] |
| 72 | + desired = set(topics) |
| 73 | + remove_topics: list[str] = [] |
| 74 | + try: |
| 75 | + existing = current_topics(args.repo) |
| 76 | + remove_topics = sorted(t for t in existing if t not in desired) |
| 77 | + except (subprocess.CalledProcessError, FileNotFoundError, json.JSONDecodeError): |
| 78 | + existing = [] |
| 79 | + cmd = build_gh_command( |
| 80 | + args.repo, description, topics, remove_topics=remove_topics |
| 81 | + ) |
| 82 | + |
| 83 | + if not args.execute: |
| 84 | + print( |
| 85 | + json.dumps( |
| 86 | + { |
| 87 | + "ok": True, |
| 88 | + "dry_run": True, |
| 89 | + "command": cmd, |
| 90 | + "description_length": len(description), |
| 91 | + "topic_count": len(topics), |
| 92 | + "remove_topic_count": len(remove_topics), |
| 93 | + "existing_topics": existing, |
| 94 | + }, |
| 95 | + indent=2, |
| 96 | + sort_keys=True, |
| 97 | + ) |
| 98 | + ) |
| 99 | + return 0 |
| 100 | + |
| 101 | + try: |
| 102 | + subprocess.run(cmd, check=True, capture_output=True, text=True) |
| 103 | + except FileNotFoundError: |
| 104 | + print(json.dumps({"ok": False, "errors": ["gh CLI not found"]}, indent=2)) |
| 105 | + return 1 |
| 106 | + except subprocess.CalledProcessError as exc: |
| 107 | + detail = (exc.stderr or exc.stdout or str(exc)).strip() |
| 108 | + print( |
| 109 | + json.dumps( |
| 110 | + {"ok": False, "errors": [f"gh failed: {detail}"]}, |
| 111 | + indent=2, |
| 112 | + sort_keys=True, |
| 113 | + ) |
| 114 | + ) |
| 115 | + return 1 |
| 116 | + |
| 117 | + print(json.dumps({"ok": True, "applied": True, "topic_count": len(topics)}, indent=2)) |
| 118 | + return 0 |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + sys.exit(main()) |
0 commit comments