11import os
22import requests
3+ import sys
34import json
45import click
56
6- token = os .environ ["GRAPH_API_KEY" ]
7+ try :
8+ token = os .environ ["GRAPH_API_KEY" ]
9+ except KeyError :
10+ print ("You need to set GRAPH_API_KEY" )
11+ print ("But you shouldn't use this yet." )
12+ sys .exit ()
13+
714endpoint = r"https://api.github.com/graphql"
8- headers = {"Authorization" : "bearer {}" . format ( token ) }
15+ headers = {"Authorization" : f "bearer { token } " }
916
1017
1118def load_query_from_file (fname , repo_owner = "numpy" , repo_name = "numpy" ):
1219 """
1320 Load an 'issue' query from file and set the target repository, where
1421 the target repository has the format:
15-
22+
1623 https://github.com/<repo_owner>/<repo_name>
17-
24+
1825 Parameters
1926 ----------
2027 fname : str
21- Path to a text file containing a valid issue query according to the
28+ Path to a text file containing a valid issue query according to the
2229 GitHub GraphQL schema.
2330 repo_owner : str
2431 Owner of target repository on GitHub. Default is 'numpy'.
2532 repo_name : str
2633 Name of target repository on GitHub. Default is 'numpy'.
27-
34+
2835 Returns
2936 -------
3037 query : str
@@ -36,7 +43,7 @@ def load_query_from_file(fname, repo_owner="numpy", repo_name="numpy"):
3643 for general GitHub GraphQL queries. See ``examples/`` for some valid
3744 templated issue queries.
3845 """
39- with open (fname , "r" ) as fh :
46+ with open (fname ) as fh :
4047 query = fh .read ()
4148 # Set target repo from template
4249 query = query .replace ("_REPO_OWNER_" , repo_owner )
@@ -85,10 +92,11 @@ def send_query(query, query_type, cursor=None):
8592 cursor_ind = query .find (cursor_insertion_key ) + len (cursor_insertion_key )
8693 query = query [:cursor_ind ] + f'after:"{ cursor } ", ' + query [cursor_ind :]
8794 # Build request payload
88- payload = {' query' : '' .join (query .split (' \n ' ))}
95+ payload = {" query" : "" .join (query .split (" \n " ))}
8996 response = requests .post (endpoint , json = payload , headers = headers )
9097 return json .loads (response .content )
9198
99+
92100def get_all_responses (query , query_type ):
93101 """
94102 Helper function to bypass GitHub GraphQL API node limit.
@@ -106,20 +114,21 @@ def get_all_responses(query, query_type):
106114 print ("Done." )
107115 return data
108116
117+
109118def parse_single_query (data , query_type ):
110119 """
111120 Parse the data returned by `send_query`
112121
113122 .. warning::
114-
123+
115124 Like `send_query`, the logic here depends on the specific structure
116125 of the query (e.g. it must be an issue or PR query, and must have a
117126 total count).
118127 """
119128 try :
120- total_count = data [' data' ][ ' repository' ][query_type ][' totalCount' ]
121- data = data [' data' ][ ' repository' ][query_type ][' edges' ]
122- last_cursor = data [- 1 ][' cursor' ]
129+ total_count = data [" data" ][ " repository" ][query_type ][" totalCount" ]
130+ data = data [" data" ][ " repository" ][query_type ][" edges" ]
131+ last_cursor = data [- 1 ][" cursor" ]
123132 except KeyError as e :
124133 print (data )
125134 raise e
@@ -182,30 +191,29 @@ def dump(self, outfile):
182191
183192
184193@click .command ()
185- @click .argument (' repo_owner' )
186- @click .argument (' repo_name' )
194+ @click .argument (" repo_owner" )
195+ @click .argument (" repo_name" )
187196def main (repo_owner , repo_name ):
188197 """Download and save issue and pr data for `repo_owner`/`repo_name`."""
189198 # Download issue data
190199 issues = GithubGrabber (
191- ' query_examples/issue_activity_since_date.gql' ,
192- ' issues' ,
200+ " query_examples/issue_activity_since_date.gql" ,
201+ " issues" ,
193202 repo_owner = repo_owner ,
194203 repo_name = repo_name ,
195204 )
196205 issues .get ()
197206 issues .dump (f"{ repo_name } _issues.json" )
198207 # Download PR data
199208 prs = GithubGrabber (
200- ' query_examples/pr_data_query.gql' ,
201- ' pullRequests' ,
209+ " query_examples/pr_data_query.gql" ,
210+ " pullRequests" ,
202211 repo_owner = repo_owner ,
203212 repo_name = repo_name ,
204213 )
205214 prs .get ()
206215 prs .dump (f"{ repo_name } _prs.json" )
207216
208217
209-
210218if __name__ == "__main__" :
211219 main ()
0 commit comments