-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add initial python script #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rbarker-dev
merged 3 commits into
main
from
2-feat-create-a-script-to-query-all-issues-from-github-given-a-user-or-team
May 1, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,95 @@ | ||||||||||||||||
#!/usr/bin/env python3 | ||||||||||||||||
|
||||||||||||||||
import os | ||||||||||||||||
import json | ||||||||||||||||
import shutil | ||||||||||||||||
import requests | ||||||||||||||||
from pathlib import Path | ||||||||||||||||
from ghapi.all import GhApi | ||||||||||||||||
from rich import print | ||||||||||||||||
|
||||||||||||||||
MYTEAM = "platform-ci" | ||||||||||||||||
TEMP_DIR = Path(f"{MYTEAM}.dir") | ||||||||||||||||
OUTPUT_FILE = f"{MYTEAM}.items.json" | ||||||||||||||||
|
||||||||||||||||
# Create GitHub API client (assumes GITHUB_TOKEN is set in env) | ||||||||||||||||
api = GhApi() | ||||||||||||||||
|
||||||||||||||||
# Step 1: Get list of orgs | ||||||||||||||||
orgs = [org["login"] for org in api.orgs.list_for_authenticated_user()] | ||||||||||||||||
|
||||||||||||||||
# Step 2: Query projects for each org | ||||||||||||||||
all_projects = [] | ||||||||||||||||
for org in orgs: | ||||||||||||||||
print(f"[bold blue]Fetching projects for org: {org}[/bold blue]") | ||||||||||||||||
|
||||||||||||||||
query = """ | ||||||||||||||||
query($login: String!) { | ||||||||||||||||
organization(login: $login) { | ||||||||||||||||
projectsV2(first: 100) { | ||||||||||||||||
nodes { | ||||||||||||||||
id | ||||||||||||||||
number | ||||||||||||||||
title | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
""" | ||||||||||||||||
|
||||||||||||||||
headers = {"Authorization": f"Bearer {os.getenv('GITHUB_TOKEN')}"} | ||||||||||||||||
response = requests.post( | ||||||||||||||||
"https://api.github.com/graphql", | ||||||||||||||||
json={"query": query, "variables": {"login": org}}, | ||||||||||||||||
headers=headers | ||||||||||||||||
) | ||||||||||||||||
result = response.json() | ||||||||||||||||
projects = [ | ||||||||||||||||
{ | ||||||||||||||||
"org": org, | ||||||||||||||||
"number": project["number"], | ||||||||||||||||
"title": project["title"] | ||||||||||||||||
} | ||||||||||||||||
for project in result.get("data", {}).get("organization", {}).get("projectsV2", {}).get("nodes", []) | ||||||||||||||||
] | ||||||||||||||||
|
||||||||||||||||
all_projects.extend(projects) | ||||||||||||||||
|
||||||||||||||||
# Step 3: Filter by team name in title | ||||||||||||||||
matching_projects = [ | ||||||||||||||||
p for p in all_projects if MYTEAM.lower() in p["title"].lower() | ||||||||||||||||
] | ||||||||||||||||
|
||||||||||||||||
print(f"[green]Found {len(matching_projects)} matching projects for team '{MYTEAM}'[/green]") | ||||||||||||||||
|
||||||||||||||||
# Step 4: Fetch items for each matching project | ||||||||||||||||
TEMP_DIR.mkdir(exist_ok=True) | ||||||||||||||||
for project in matching_projects: | ||||||||||||||||
org = project["org"] | ||||||||||||||||
number = project["number"] | ||||||||||||||||
title = project["title"] | ||||||||||||||||
|
||||||||||||||||
print(f"[cyan]Fetching items from: {title} (Org: {org}, Project: {number})[/cyan]") | ||||||||||||||||
|
||||||||||||||||
# ghapi does not support the `gh project item-list` command yet, | ||||||||||||||||
# so we use a subprocess call or GitHub REST if available. | ||||||||||||||||
# We'll fallback to CLI for now. | ||||||||||||||||
out_path = TEMP_DIR / f"{org}-{number}.items.json" | ||||||||||||||||
os.system( f'gh project item-list --owner "{org}" {number} --format json > "{out_path}"') | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using subprocess.run instead of os.system for improved error handling and to reduce potential command injection risks.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||
|
||||||||||||||||
# Step 5: Consolidate items | ||||||||||||||||
all_items = [] | ||||||||||||||||
for file in TEMP_DIR.glob("*.items.json"): | ||||||||||||||||
with open(file) as f: | ||||||||||||||||
data = json.load(f) | ||||||||||||||||
items = data.get("items", []) | ||||||||||||||||
all_items.extend(items) | ||||||||||||||||
|
||||||||||||||||
print(all_items) | ||||||||||||||||
with open(OUTPUT_FILE, "w") as f: | ||||||||||||||||
json.dump(all_items, f, indent=2) | ||||||||||||||||
|
||||||||||||||||
print(f"[bold green]Saved all items to {OUTPUT_FILE}[/bold green]") | ||||||||||||||||
|
||||||||||||||||
# Step 6: Cleanup | ||||||||||||||||
shutil.rmtree(TEMP_DIR) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider checking the HTTP response status code before processing the JSON to ensure the API call succeeded.
Copilot uses AI. Check for mistakes.