Skip to content

Instantly share code, notes, and snippets.

@lornajane
Last active January 27, 2025 12:03
Show Gist options
  • Save lornajane/a1dfc26d0520646faa6506709992f129 to your computer and use it in GitHub Desktop.
Save lornajane/a1dfc26d0520646faa6506709992f129 to your computer and use it in GitHub Desktop.
import requests
import json
import os
import sys
if len(sys.argv) != 2:
print("Usage: python3 github-api.py name")
sys.exit(1)
name = sys.argv[1]
query = '''
query ($login: String!) {
user(login: $login) {
login
contributionsCollection {
pullRequestReviewContributions(last: 100) {
nodes {
pullRequestReview {
authorCanPushToRepository
}
pullRequest {
repository {
name
owner {
login
}
}
}
}
}
}
}
}
'''
# variables for query
variables = {
"login": name
}
url = 'https://api.github.com/graphql'
headers = {'Authorization': 'Bearer ' + os.environ['GH_KEY']}
response = requests.post(url, json={'query': query, 'variables': variables}, headers=headers)
# be careful, errors are response status 200
if response.status_code == 200:
data = response.json()
if 'errors' in data:
print("Errors:")
print(data['errors'])
elif 'data' in data and 'user' in data['data'] and data['data']['user'] != None:
login = data['data']['user']['login']
projects = []
for pr_review in data['data']['user']['contributionsCollection']['pullRequestReviewContributions']['nodes']:
pr = pr_review['pullRequest']
repo = pr['repository']['name']
owner = pr['repository']['owner']['login']
can_push = pr_review['pullRequestReview']['authorCanPushToRepository']
project_string = f"{owner}/{repo}"
if can_push and (project_string not in projects):
projects.append(project_string)
print(f"{login} | {projects}")
else:
print("Error: User data not found in the response.")
else:
print(f"Error: Request failed with status code {response.status_code}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment