|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import json |
| 5 | +import shutil |
| 6 | +import requests |
| 7 | +from pathlib import Path |
| 8 | +from ghapi.all import GhApi |
| 9 | +from rich import print |
| 10 | + |
| 11 | +MYTEAM = "platform-ci" |
| 12 | +TEMP_DIR = Path(f"{MYTEAM}.dir") |
| 13 | +OUTPUT_FILE = f"{MYTEAM}.items.json" |
| 14 | + |
| 15 | +# Create GitHub API client (assumes GITHUB_TOKEN is set in env) |
| 16 | +api = GhApi() |
| 17 | + |
| 18 | +# Step 1: Get list of orgs |
| 19 | +orgs = [org["login"] for org in api.orgs.list_for_authenticated_user()] |
| 20 | + |
| 21 | +# Step 2: Query projects for each org |
| 22 | +all_projects = [] |
| 23 | +for org in orgs: |
| 24 | + print(f"[bold blue]Fetching projects for org: {org}[/bold blue]") |
| 25 | + |
| 26 | + query = """ |
| 27 | + query($login: String!) { |
| 28 | + organization(login: $login) { |
| 29 | + projectsV2(first: 100) { |
| 30 | + nodes { |
| 31 | + id |
| 32 | + number |
| 33 | + title |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + """ |
| 39 | + |
| 40 | + headers = {"Authorization": f"Bearer {os.getenv('GITHUB_TOKEN')}"} |
| 41 | + response = requests.post( |
| 42 | + "https://api.github.com/graphql", |
| 43 | + json={"query": query, "variables": {"login": org}}, |
| 44 | + headers=headers |
| 45 | + ) |
| 46 | + result = response.json() |
| 47 | + projects = [ |
| 48 | + { |
| 49 | + "org": org, |
| 50 | + "number": project["number"], |
| 51 | + "title": project["title"] |
| 52 | + } |
| 53 | + for project in result.get("data", {}).get("organization", {}).get("projectsV2", {}).get("nodes", []) |
| 54 | + ] |
| 55 | + |
| 56 | + all_projects.extend(projects) |
| 57 | + |
| 58 | +# Step 3: Filter by team name in title |
| 59 | +matching_projects = [ |
| 60 | + p for p in all_projects if MYTEAM.lower() in p["title"].lower() |
| 61 | +] |
| 62 | + |
| 63 | +print(f"[green]Found {len(matching_projects)} matching projects for team '{MYTEAM}'[/green]") |
| 64 | + |
| 65 | +# Step 4: Fetch items for each matching project |
| 66 | +TEMP_DIR.mkdir(exist_ok=True) |
| 67 | +for project in matching_projects: |
| 68 | + org = project["org"] |
| 69 | + number = project["number"] |
| 70 | + title = project["title"] |
| 71 | + |
| 72 | + print(f"[cyan]Fetching items from: {title} (Org: {org}, Project: {number})[/cyan]") |
| 73 | + |
| 74 | + # ghapi does not support the `gh project item-list` command yet, |
| 75 | + # so we use a subprocess call or GitHub REST if available. |
| 76 | + # We'll fallback to CLI for now. |
| 77 | + out_path = TEMP_DIR / f"{org}-{number}.items.json" |
| 78 | + os.system( f'gh project item-list --owner "{org}" {number} --format json > "{out_path}"') |
| 79 | + |
| 80 | +# Step 5: Consolidate items |
| 81 | +all_items = [] |
| 82 | +for file in TEMP_DIR.glob("*.items.json"): |
| 83 | + with open(file) as f: |
| 84 | + data = json.load(f) |
| 85 | + items = data.get("items", []) |
| 86 | + all_items.extend(items) |
| 87 | + |
| 88 | +print(all_items) |
| 89 | +with open(OUTPUT_FILE, "w") as f: |
| 90 | + json.dump(all_items, f, indent=2) |
| 91 | + |
| 92 | +print(f"[bold green]Saved all items to {OUTPUT_FILE}[/bold green]") |
| 93 | + |
| 94 | +# Step 6: Cleanup |
| 95 | +shutil.rmtree(TEMP_DIR) |
0 commit comments