|
| 1 | +import json |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +import os |
| 5 | + |
| 6 | +def run_command(args): |
| 7 | + result = subprocess.run(args, capture_output=True, text=True) |
| 8 | + if result.returncode != 0: |
| 9 | + print(f"Error running {' '.join(args)}: {result.stderr}", file=sys.stderr) |
| 10 | + return None |
| 11 | + return result.stdout |
| 12 | + |
| 13 | +def get_repos(org): |
| 14 | + output = run_command(["gh", "repo", "list", org, "--limit", "1000", "--json", "nameWithOwner,isArchived"]) |
| 15 | + if not output: |
| 16 | + return [] |
| 17 | + repos = json.loads(output) |
| 18 | + return [r["nameWithOwner"] for r in repos if not r["isArchived"]] |
| 19 | + |
| 20 | +def get_repo_labels(repo): |
| 21 | + output = run_command(["gh", "label", "list", "--repo", repo, "--json", "name,description,color"]) |
| 22 | + if not output: |
| 23 | + return [] |
| 24 | + return json.loads(output) |
| 25 | + |
| 26 | +def sync_labels(repo, target_labels): |
| 27 | + existing_labels = {l["name"]: l for l in get_repo_labels(repo)} |
| 28 | + |
| 29 | + for target in target_labels: |
| 30 | + name = target["name"] |
| 31 | + color = target["color"] |
| 32 | + description = target["description"] |
| 33 | + |
| 34 | + if name in existing_labels: |
| 35 | + existing = existing_labels[name] |
| 36 | + if existing["color"].lower() != color.lower() or existing["description"] != description: |
| 37 | + print(f"Updating label '{name}' in {repo}") |
| 38 | + run_command(["gh", "label", "edit", name, "--repo", repo, "--color", color, "--description", description]) |
| 39 | + else: |
| 40 | + print(f"Creating label '{name}' in {repo}") |
| 41 | + run_command(["gh", "label", "create", name, "--repo", repo, "--color", color, "--description", description]) |
| 42 | + |
| 43 | +def cleanup_labels(repo, target_label_names): |
| 44 | + existing_labels = get_repo_labels(repo) |
| 45 | + extra_labels = [l["name"] for l in existing_labels if l["name"] not in target_label_names] |
| 46 | + |
| 47 | + usage_report = [] |
| 48 | + |
| 49 | + for label in extra_labels: |
| 50 | + # Check issues and PRs (gh issue list covers both if not specified, but let's be explicit) |
| 51 | + issues = json.loads(run_command(["gh", "issue", "list", "--repo", repo, "--label", label, "--json", "number,url,title"]) or "[]") |
| 52 | + prs = json.loads(run_command(["gh", "pr", "list", "--repo", repo, "--label", label, "--json", "number,url,title"]) or "[]") |
| 53 | + |
| 54 | + # Discussions might fail if not enabled |
| 55 | + discussions_output = run_command(["gh", "discussion", "list", "--repo", repo, "--label", label, "--json", "number,url,title"]) |
| 56 | + discussions = json.loads(discussions_output) if discussions_output else [] |
| 57 | + |
| 58 | + if not issues and not prs and not discussions: |
| 59 | + print(f"Deleting unused label '{label}' from {repo}") |
| 60 | + run_command(["gh", "label", "delete", label, "--repo", repo, "--yes"]) |
| 61 | + else: |
| 62 | + for item in issues: |
| 63 | + usage_report.append({"label": label, "repo": repo, "type": "Issue", "number": item["number"], "url": item["url"], "title": item["title"]}) |
| 64 | + for item in prs: |
| 65 | + usage_report.append({"label": label, "repo": repo, "type": "PR", "number": item["number"], "url": item["url"], "title": item["title"]}) |
| 66 | + for item in discussions: |
| 67 | + usage_report.append({"label": label, "repo": repo, "type": "Discussion", "number": item["number"], "url": item["url"], "title": item["title"]}) |
| 68 | + |
| 69 | + return usage_report |
| 70 | + |
| 71 | +def main(): |
| 72 | + org = "ChecKMarKDevTools" |
| 73 | + labels_file = ".github/org-labels.json" |
| 74 | + |
| 75 | + with open(labels_file, "r") as f: |
| 76 | + target_labels = json.load(f) |
| 77 | + |
| 78 | + target_label_names = [l["name"] for l in target_labels] |
| 79 | + repos = get_repos(org) |
| 80 | + |
| 81 | + all_usage = [] |
| 82 | + |
| 83 | + for repo in repos: |
| 84 | + print(f"Processing {repo}...") |
| 85 | + sync_labels(repo, target_labels) |
| 86 | + usage = cleanup_labels(repo, target_label_names) |
| 87 | + all_usage.extend(usage) |
| 88 | + |
| 89 | + if all_usage: |
| 90 | + # Group by repo for the summary |
| 91 | + all_usage.sort(key=lambda x: (x["repo"], x["label"])) |
| 92 | + current_repo = "" |
| 93 | + for item in all_usage: |
| 94 | + if item["repo"] != current_repo: |
| 95 | + if current_repo: |
| 96 | + print("::endgroup::") |
| 97 | + print(f"::group::Extra labels in {item['repo']}") |
| 98 | + current_repo = item["repo"] |
| 99 | + print(f"- Label '{item['label']}' is in use:") |
| 100 | + print(f" - {item['type']} [#{item['number']}]({item['url']}): {item['title']}") |
| 101 | + if current_repo: |
| 102 | + print("::endgroup::") |
| 103 | + else: |
| 104 | + print("No extra labels in use.") |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + main() |
0 commit comments