|
| 1 | +#!/usr/bin/env python |
| 2 | +import subprocess |
| 3 | +import json |
| 4 | +from datetime import datetime |
| 5 | +import argparse |
| 6 | + |
| 7 | +# Parse command-line arguments |
| 8 | +parser = argparse.ArgumentParser(description="Manage GitHub repositories.") |
| 9 | +parser.add_argument("--fork", action="store_true", help="Show only forks.") |
| 10 | +parser.add_argument("-l", "--language", type=str, help="Filter by primary coding language.") |
| 11 | +parser.add_argument("-L", "--limit", type=int, default=30, help="Maximum number of repositories to list (default 30).") |
| 12 | +parser.add_argument("--no-archived", action="store_true", help="Omit archived repositories.") |
| 13 | +parser.add_argument("--topic", type=str, help="Filter by topic.") |
| 14 | +parser.add_argument("--visibility", type=str, choices=["public", "private", "internal"], help="Filter by repository visibility.") |
| 15 | +parser.add_argument("--public", action="store_true", help="Alias for --visibility=public") |
| 16 | +args = parser.parse_args() |
| 17 | + |
| 18 | +# Build the GitHub CLI command |
| 19 | +command = [ |
| 20 | + "gh", "repo", "list", |
| 21 | + "--limit", str(args.limit), |
| 22 | + "--json", "name,createdAt,visibility,isFork,isArchived,stargazerCount", |
| 23 | +] |
| 24 | + |
| 25 | +if args.fork: |
| 26 | + command.append("--fork") |
| 27 | +if args.language: |
| 28 | + command.extend(["-l", args.language]) |
| 29 | +if args.no_archived: |
| 30 | + command.append("--no-archived") |
| 31 | +if args.topic: |
| 32 | + command.extend(["--topic", args.topic]) |
| 33 | + |
| 34 | +if args.visibility: |
| 35 | + command.extend(["--visibility", args.visibility]) |
| 36 | +elif args.public: |
| 37 | + command.extend(["--visibility=public"]) |
| 38 | + |
| 39 | +# Run the GitHub CLI command to get the repository list |
| 40 | +result = subprocess.run(command, capture_output=True, text=True) |
| 41 | + |
| 42 | +# print ("Command stdout:", result.stdout) |
| 43 | +# print ("Command stderr:", result.stderr) |
| 44 | + |
| 45 | +if result.returncode != 0: |
| 46 | + print("Error fetching repository list:", result.stderr) |
| 47 | + exit(1) |
| 48 | + |
| 49 | +# Parse the JSON output |
| 50 | +repos = json.loads(result.stdout) |
| 51 | + |
| 52 | +# Filter out repositories with stargazerCount > 0 |
| 53 | +repos = [r for r in repos if r["stargazerCount"] == 0] |
| 54 | + |
| 55 | +# Sort repositories by createdAt in ascending order |
| 56 | +repos.sort(key=lambda repo: datetime.fromisoformat(repo["createdAt"].replace("Z", "+00:00"))) |
| 57 | + |
| 58 | +# Iterate over each repository and prompt the user for action |
| 59 | +for repo in repos: |
| 60 | + print(f"Repository: {repo['name']}") |
| 61 | + print(f" Stars: {repo['stargazerCount']}") |
| 62 | + print(f" Created At: {repo['createdAt']}") |
| 63 | + print(f" Visibility: {repo['visibility']}") |
| 64 | + print(f" Is Fork: {'Yes' if repo['isFork'] else 'No'}") |
| 65 | + print(f" Is Archived: {'Yes' if repo['isArchived'] else 'No'}") |
| 66 | + action = input("What would you like to do? ([a]rchive/[d]elete/[c]ontinue): ").strip().lower() |
| 67 | + |
| 68 | + if "archive".startswith(action): |
| 69 | + subprocess.run(["gh", "repo", "archive", repo["name"], "--yes"]) |
| 70 | + elif "delete".startswith(action): |
| 71 | + subprocess.run(["gh", "repo", "delete", repo["name"], "--yes"]) |
| 72 | + elif "continue".startswith(action): |
| 73 | + continue |
| 74 | + else: |
| 75 | + print("Invalid action. Skipping...") |
0 commit comments