Skip to content

Instantly share code, notes, and snippets.

@lornajane
Last active January 27, 2025 12:03
Show Gist options
  • Select an option

  • Save lornajane/a1dfc26d0520646faa6506709992f129 to your computer and use it in GitHub Desktop.

Select an option

Save lornajane/a1dfc26d0520646faa6506709992f129 to your computer and use it in GitHub Desktop.

Revisions

  1. lornajane revised this gist Jan 27, 2025. 1 changed file with 4 additions and 24 deletions.
    28 changes: 4 additions & 24 deletions github-api.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    import requests
    import json
    import os
    import sys

    if len(sys.argv) != 2:
    @@ -11,32 +12,13 @@
    query ($login: String!) {
    user(login: $login) {
    login
    websiteUrl
    socialAccounts(first:10) {
    nodes{
    provider
    url
    }
    }
    contributionsCollection {
    pullRequestReviewContributions(last: 100) {
    totalCount
    nodes {
    pullRequestReview {
    state
    authorCanPushToRepository
    author {
    login
    }
    createdAt
    body
    }
    pullRequest {
    title
    url
    author {
    login
    }
    repository {
    name
    owner {
    @@ -57,13 +39,12 @@
    }

    url = 'https://api.github.com/graphql'
    headers = {'Authorization': 'Bearer ghp_xxx'} # put your access token here
    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()
    print(data)
    if 'errors' in data:
    print("Errors:")
    print(data['errors'])
    @@ -77,8 +58,7 @@
    repo = pr['repository']['name']
    owner = pr['repository']['owner']['login']

    review = pr_review['pullRequestReview']
    can_push = review['authorCanPushToRepository']
    can_push = pr_review['pullRequestReview']['authorCanPushToRepository']
    project_string = f"{owner}/{repo}"

    if can_push and (project_string not in projects):
    @@ -90,4 +70,4 @@
    print("Error: User data not found in the response.")

    else:
    print(f"Error: Request failed with status code {response.status_code}")
    print(f"Error: Request failed with status code {response.status_code}")
  2. lornajane created this gist Jan 27, 2025.
    93 changes: 93 additions & 0 deletions github-api.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    import requests
    import json
    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
    websiteUrl
    socialAccounts(first:10) {
    nodes{
    provider
    url
    }
    }
    contributionsCollection {
    pullRequestReviewContributions(last: 100) {
    totalCount
    nodes {
    pullRequestReview {
    state
    authorCanPushToRepository
    author {
    login
    }
    createdAt
    body
    }
    pullRequest {
    title
    url
    author {
    login
    }
    repository {
    name
    owner {
    login
    }
    }
    }
    }
    }
    }
    }
    }
    '''

    # variables for query
    variables = {
    "login": name
    }

    url = 'https://api.github.com/graphql'
    headers = {'Authorization': 'Bearer ghp_xxx'} # put your access token here
    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()
    print(data)
    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']

    review = pr_review['pullRequestReview']
    can_push = review['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}")