-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql.py
251 lines (230 loc) · 8.01 KB
/
graphql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# %%
import json
import os
import random
import time
from tqdm import tqdm
def save_user_repos(session, username):
user_repos = get_user_repos(session, username)
# tqdm.write(f"Read {len(user_repos):>4} repos from {username}")
return username, user_repos
user_repos_query = """
query($username: String!, $cursor: String, $history: Int) {
user(login: $username) {
repositories(first: $history, isFork: true, after: $cursor) {
edges {
node {
name
parent {
name
owner {
login
}
url
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
"""
def get_user_repos(session, username):
all_repos = []
has_next_page = True
cursor = None
page_num=0
while has_next_page is True:
page_num+=1
variables = {"username": username,"cursor": cursor}
repos = None
sleep_length = 2/1.25
exponential_backoff = 1.5
history = 100
history_divisor = 2
while repos is None:
try:
variables['history'] = history
response = session.post('https://api.github.com/graphql', json={'query': user_repos_query, 'variables': variables}, headers=headers(session))
data = response.json()
repos = data['data']['user']['repositories']['edges']
parent_repos = [repo['node']['parent'] for repo in repos if repo['node'].get('parent')]
parent_full_names = [f"{parent['owner']['login']}/{parent['name']}" for parent in parent_repos]
page_info = data['data']['user']['repositories']['pageInfo']
cursor = page_info['endCursor']
has_next_page = page_info['hasNextPage']
all_repos.extend(parent_full_names)
# tqdm.write(f"total repos: {len(set(all_repos))}")
except Exception as exc:
if "errors" in response.text and "NOT FOUND" in response.text.errors[0].type:
tqdm.write(f"User {username} not found")
return []
sleep_length *= exponential_backoff
history = max(history // history_divisor, 1)
tqdm.write(f"Error fetching repos of {username}: {exc}, waiting for {sleep_length} seconds and trying {history} records...")
tqdm.write("Error message: " + response.text)
time.sleep(sleep_length)
return all_repos
rate_limit_query = """
query {
rateLimit {
limit
cost
remaining
resetAt
}
}
"""
def get_rate_limit(session):
response = session.post('https://api.github.com/graphql', json={'query': rate_limit_query}, headers=headers(session))
rate_limit_info = response.json()
if "data" not in rate_limit_info:
return rate_limit_info
return rate_limit_info['data']['rateLimit']['remaining']
def check_rate_limit(session):
print(f"remaining rate limit: {get_rate_limit(session)}")
parent_repo_query ="""
query {
repository(owner: $owner, name: $repo) {
name
parent {
name
owner {
login
}
isFork
url
}
}
}
"""
def parent_repo(session, owner, repo):
variables = {"owner": owner,"repo": repo}
parent = None
sleep_length = 2/1.25
exponential_backoff = 1.25
while parent is None:
try:
response = session.post('https://api.github.com/graphql', json={'query': parent_repo_query, 'variables': variables}, headers=headers(session))
data = response.json()
parent = data['data']['repository']['parent']
except Exception as exc:
sleep_length *= exponential_backoff
tqdm.write(f"Error fetching parent of {owner}/{repo}: {exc}, waiting for {sleep_length} seconds and trying again...")
tqdm.write(f"Error message: {response.text}")
time.sleep(sleep_length)
return parent
query_default_branch = """
query ($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
defaultBranchRef {
name
}
}
}
"""
def fetch_default_branch_name(session, owner, repo):
variables = {"owner": owner,"repo": repo}
default_branch_name = None
sleep_length = 2/1.25
exponential_backoff = 1.25
while default_branch_name is None:
try:
response = session.post('https://api.github.com/graphql', json={'query': query_default_branch, 'variables': variables}, headers=headers(session))
data = response.json()
default_branch_name = data['data']['repository']['defaultBranchRef']['name']
except Exception as exc:
sleep_length *= exponential_backoff
tqdm.write(f"Error fetching default branch of {owner}/{repo}: {exc}, waiting for {sleep_length} seconds and trying again...")
tqdm.write(f"Error message: {response.text}")
time.sleep(sleep_length)
return default_branch_name
repo_commits_query = """
query ($owner: String!, $repo: String!, $branch: String!, $cursor: String, $history: Int!) {
repository(owner: $owner, name: $repo) {
ref(qualifiedName: $branch) {
target {
... on Commit {
history(first: $history, after: $cursor) {
pageInfo {
endCursor
hasNextPage
}
edges {
node {
oid
messageHeadline
author {
name
email
user {
login
}
}
additions
deletions
}
}
}
}
}
}
}
}
"""
def fetch_commits(session, owner, repo, branch):
all_commits = []
has_next_page = True
cursor = None
page_num=0
while has_next_page is True:
page_num+=1
variables = {"owner": owner,"repo": repo,"branch": branch,"cursor": cursor}
commits = None
sleep_length = 2/1.25
exponential_backoff = 1.5
history = 100
history_divisor = 2
while commits is None:
try:
variables['history'] = history
tqdm.write(f"{variables=}")
response = session.post('https://api.github.com/graphql', json={'query': repo_commits_query, 'variables': variables}, headers=headers(session))
data = response.json()
commits = data['data']['repository']['ref']['target']['history']['edges']
except Exception as exc:
sleep_length *= exponential_backoff
history = max(history // history_divisor, 1)
tqdm.write(f"Error fetching page {page_num} of {owner}/{repo}: {exc}, waiting for {sleep_length} seconds and trying {history} records...")
tqdm.write("Error message: " + response.text)
time.sleep(sleep_length)
all_commits.extend(commits)
page_info = data['data']['repository']['ref']['target']['history']['pageInfo']
cursor = page_info['endCursor']
has_next_page = page_info['hasNextPage']
return all_commits
def parse_repo(session, repo):
owner, repo = repo.split('/')
branch = fetch_default_branch_name(session, owner, repo)
return fetch_commits(session, owner, repo, branch)
def save_repo(session, repo):
filepath = f"repos/{repo.replace('/', '_')}.json"
if os.path.exists(filepath):
print(f"{filepath} already exists, skipping...")
return
start_time = time.time()
parsed_repo = parse_repo(session, repo)
with open(filepath, "w", encoding="utf-8") as f:
json.dump(parsed_repo, f)
tqdm.write(f"Saved {repo} to {filepath} in {time.time() - start_time:.2f} seconds")
# Headers for the HTTP request
headers_template = {
"Authorization": "Bearer {key}",
"Content-Type": "application/json"
}
def headers(session):
random_key = random.choice(session.keys)
return {key: value.format(key=random_key) for key, value in headers_template.items()}