Skip to content

Instantly share code, notes, and snippets.

@parvezmrobin
Created October 22, 2021 17:56
Show Gist options
  • Select an option

  • Save parvezmrobin/0c3bcd886a0eeefbbf69b31fe9f49418 to your computer and use it in GitHub Desktop.

Select an option

Save parvezmrobin/0c3bcd886a0eeefbbf69b31fe9f49418 to your computer and use it in GitHub Desktop.

Revisions

  1. parvezmrobin created this gist Oct 22, 2021.
    132 changes: 132 additions & 0 deletions access-github-graphql-api.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,132 @@
    from gql import gql

    def create_graphql_query(repo: str, commit_id: str):
    """
    Create a GraphQL query
    Documentation: https://docs.github.com/en/graphql
    """

    return gql(f'''query {{
    search(
    type: ISSUE,
    query: "type:pr is:merged linked:issue repo:{repo} {commit_id}"
    first: 100
    ) {{
    issueCount
    pageInfo {{
    hasNextPage,
    endCursor,
    }}
    nodes {{
    ... on PullRequest {{
    id
    number
    title
    baseRefName
    createdAt
    repository {{
    name
    }}
    labels(first:100) {{
    totalCount
    nodes {{
    name
    }}
    }}
    mergeCommit {{
    authoredDate
    oid
    }}
    commits(first: 250) {{
    totalCount
    pageInfo {{
    hasNextPage
    endCursor
    }}
    nodes {{
    commit {{
    authoredDate
    oid
    message
    }}
    }}
    }}
    closingIssuesReferences(first: 10) {{
    totalCount
    nodes {{
    id
    number
    title
    body
    bodyText
    labels(first: 100) {{
    totalCount
    nodes {{
    name
    }}
    }}
    comments(first:100) {{
    totalCount
    nodes {{
    body
    bodyText
    }}
    }}
    }}
    }}
    }}
    }}
    }}
    }}''')

    import json
    from gql import Client
    from gql.transport.requests import RequestsHTTPTransport

    # Reading the GitHub token from a config file
    URL = 'https://api.github.com/graphql'
    with open('config.json') as configFile:
    config = json.load(configFile)
    GITHUB_TOKEN = config['githubToken']

    HEADERS = {
    'Authorization': f'Bearer {GITHUB_TOKEN}'
    }

    # Create necessary objects
    transport = RequestsHTTPTransport(url=URL, headers=HEADERS)
    client = Client(transport=transport)

    import winsound
    import json

    result, _repo, _commit = None, None, None
    try:
    for i, (_repo, _commit) in commit_repos.iterrows():
    query = create_graphql_query(
    repo_map[_repo],
    _commit,
    )
    result = client.execute(query)
    assert result['search']['issueCount'] in [0, 1]

    # if a corresponding issue found, write it
    if result['search']['issueCount'] == 1:
    result['repo'] = _repo
    with open(f'output/issues/json/{_commit}.json', 'w') as issueJsonFile:
    json.dump(result, issueJsonFile, indent=4)
    except Exception as e:
    # Print the errors
    print(_repo, _commit)
    print(result)
    print(e)

    # Notify yourself that an error occured
    duration = 5000 # milliseconds
    freq = 640 # Hz
    winsound.Beep(freq, duration)

    # Notify yourself that it is done
    duration = 1000 # milliseconds
    freq = 440 # Hz
    winsound.Beep(freq, duration)