-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub.py
249 lines (215 loc) · 8.71 KB
/
github.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
import requests
import csv
import time
import threading
import concurrent.futures
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timedelta
from models import Repository
from helpers import *
logger = get_logger()
RATE_LIMIT_LOCK = threading.Lock()
MAX_WORKERS = 5
def add_active_committers(report, repositories, token):
try:
with open(report, "r") as file:
reader = csv.reader(file)
next(reader) # Skip the header row
for row in reader:
user_login, org_repo, *_ = row
org, repo_name = org_repo.split("/")
for repo in repositories:
if repo.name == repo_name and repo.org == org:
repo.add_committer(user_login)
except (Exception, TypeError) as e:
logger.info(f"Fetching active committers from GitHub API...")
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
futures = [
executor.submit(
get_active_committers_in_last_90_days, repo.org, repo.name, token
)
for repo in repositories
]
for i, future in enumerate(
concurrent.futures.as_completed(futures), start=1
):
repo = repositories[i - 1]
logger.info(
f"[{i}/{len(repositories)}] Processing repository: {repo.get_full_name()}"
)
repo.add_active_committers(future.result())
def get_organizations(args, token):
orgs_in_ent = []
if not args.enterprise:
orgs_in_ent.append(args.organization)
else:
orgs_in_ent = get_orgs_in_ent(args.enterprise, token)
return orgs_in_ent
def process_organizations(orgs_in_ent, token):
total_repositories = []
for i, org in enumerate(orgs_in_ent, start=1):
logger.info(f"[{i}/{len(orgs_in_ent)}] Processing organization: {org}")
total_repositories.extend(get_ghas_status_for_repos(org, token))
return total_repositories
def get_ghas_status_for_repos(org, token):
url = f"https://api.github.com/orgs/{org}/repos?per_page=100"
headers = {"Authorization": f"token {token}"}
page = 1
repos = []
while True:
response = requests.get(url, headers=headers, params={"page": page})
handle_rate_limit(response)
data = response.json()
for repo_data in data:
owner, name = repo_data["full_name"].split("/")
ghas_status = (
repo_data.get("security_and_analysis", {})
.get("advanced_security", {})
.get("status", "")
== "enabled"
)
visibility = repo_data["visibility"]
pushed_at = repo_data["pushed_at"]
repo = Repository(name, owner, ghas_status, visibility, pushed_at)
repos.append(repo)
if "next" not in response.links:
break
page += 1
return repos
def get_active_committers_in_last_90_days(org, repo, token):
url = "https://api.github.com/graphql"
headers = {"Authorization": f"token {token}"}
active_committers = set()
end_cursor = None
while True:
query = """
query getCommitters($org: String!, $repo: String!, $since: GitTimestamp!, $after: String) {
repository(owner: $org, name: $repo) {
visibility
refs(refPrefix: "refs/heads/", first: 100) {
nodes {
name
target {
... on Commit {
history(first: 100, since: $since, after: $after) {
nodes {
author {
user {
login
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
"""
hasNextPage = False
since = (datetime.now() - timedelta(days=90)).isoformat()
variables = {"org": org, "repo": repo, "since": since, "after": end_cursor}
payload = {"query": query, "variables": variables}
response = requests.post(url, headers=headers, json=payload)
handle_rate_limit(response)
if response.status_code != 200:
logger.info(f"Response: {response.json()}")
next
if response.status_code == 401:
logger.info(f"Insufficient permissions token provided.")
break
data = response.json()
repository = data["data"]["repository"]
refs = repository.get("refs")
visibility = repository.get("visibility")
# only process if repository is not public
if visibility != "PUBLIC":
if refs:
nodes = refs.get("nodes", [])
for ref in nodes:
target = ref.get("target", {})
history = target.get("history") if target else None
if history:
commits = history.get("nodes", [])
for commit in commits:
user = commit["author"]["user"]
if user:
author = user["login"]
active_committers.add(author)
end_cursor = history.get("pageInfo", {}).get("endCursor")
hasNextPage = history.get("pageInfo", {}).get("hasNextPage")
if not refs or not hasNextPage:
break
handle_rate_limit(response)
# Remove dependabot[bot] from active committers
active_committers.discard("dependabot[bot]")
return list(active_committers)
def get_orgs_in_ent(enterprise_name, token):
url = "https://api.github.com/graphql"
headers = {"Authorization": f"token {token}", "X-Github-Next-Global-ID": "true"}
orgs = []
end_cursor = None
while True:
query = """
query getOrgsInEnterprise($enterprise: String!, $after: String) {
enterprise(slug: $enterprise) {
organizations(first: 100, after: $after) {
nodes {
login
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
"""
variables = {"enterprise": enterprise_name, "after": end_cursor}
payload = {"query": query, "variables": variables}
response = requests.post(url, headers=headers, json=payload)
handle_rate_limit(response)
data = response.json()
orgs += [
org["login"] for org in data["data"]["enterprise"]["organizations"]["nodes"]
]
if data["data"]["enterprise"]["organizations"]["pageInfo"]["hasNextPage"]:
end_cursor = data["data"]["enterprise"]["organizations"]["pageInfo"][
"endCursor"
]
else:
break
handle_rate_limit(response)
return orgs
def handle_rate_limit(response):
# Check the rate limit
remaining = int(response.headers.get("X-RateLimit-Remaining", 0))
if remaining < 50:
logger.info(f"Rate limit close to being exceeded. Slowing down requests...")
time.sleep(5)
if remaining == 0 or response.status_code in [403, 429]:
with RATE_LIMIT_LOCK:
remaining = int(response.headers.get("X-RateLimit-Remaining", 0))
if remaining == 0 or response.status_code in [403, 429]:
logger.info(f"Headers: {response.headers}")
if "Retry-After" in response.headers:
sleep_time = int(response.headers["Retry-After"]) + 2
else:
reset_time = int(response.headers.get("X-RateLimit-Reset", 0))
sleep_time = max(
reset_time - time.time() + 2, 1
) # Ensure sleep time is at least 1 second
logger.info(
f"Rate limit exceeded. Sleeping for {sleep_time} seconds..."
)
time.sleep(sleep_time)
time.sleep(1) # Sleep for 1 second anyways to avoid hitting rate limit