-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
376 lines (351 loc) · 11.9 KB
/
utils.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import base64
import json
import os
import re
import sys
from gql import Client, gql
from gql.transport.requests import RequestsHTTPTransport
from gql.transport.exceptions import TransportQueryError
BEARER_TOKEN = os.environ.get("GITHUB_TOKEN")
GITHUB_GQL_ENDPOINT = os.environ.get("GITHUB_GQL_ENDPOINT", "https://api.github.com/graphql")
LOGIN = os.environ.get('GITHUB_LOGIN')
headers = {
"Authorization": f"Bearer {BEARER_TOKEN}",
"Content-Type": "application/json",
}
transport = RequestsHTTPTransport(
url=GITHUB_GQL_ENDPOINT,
headers=headers,
use_json=True,
retries=3,
)
client = Client(
transport=transport,
fetch_schema_from_transport=False,
)
class ForbiddenAccessError(Exception):
pass
def run_query(query):
try:
query = gql(query)
return client.execute(query)
except TransportQueryError as e:
raise ForbiddenAccessError()
def simplify(name):
name = name.lower()
result = ""
for idx, char in enumerate(name):
if idx == 0:
if ord(char) >= 48 and ord(char) <= 57:
continue
if ord(char) >= 97 and ord(char) <= 122:
result += f"{char}"
if ord(char) >= 48 and ord(char) <= 57:
result += f"{char}"
if char == "_" or char == " " or char == "-" or char == "/":
result += "_"
if char == "*":
result += "wildcard"
return re.sub(r'_+', '_', result)
def unique(in_data):
data = set()
for row in in_data:
data.add(base64.b64encode(json.dumps(row).encode('utf-8')))
return [json.loads(base64.b64decode(row)) for row in data]
def get_all_teams(teams_filter = "first: 100"):
query = f"""
{{
organization(login: "{LOGIN}") {{
id
name,
teams({teams_filter}) {{
pageInfo {{
endCursor,
}},
totalCount,
nodes {{
id
databaseId,
name,
description,
privacy,
parentTeam {{
id,
name,
}},
members(membership:IMMEDIATE) {{
edges {{
role,
node {{
login,
}},
}},
}},
}},
}},
}},
}}
"""
data = run_query(query)
teams = data['organization']['teams']
hasNextPage = teams.get('hasNextPage')
output = teams.get('nodes')
endCursor = teams.get('pageInfo').get('endCursor')
if hasNextPage:
teams_filter = f"first: 100, after: '{endCursor}'"
output += get_all_teams(teams_filter)
return sorted(unique(output), key = lambda t: simplify(t['name']))
def get_team_repositories(team_name, repository_filter = "first: 100"):
query = f"""
{{
organization(login: "{LOGIN}") {{
teams(query: "{team_name}", first: 1) {{
nodes {{
id
repositories({repository_filter}) {{
edges {{
permission,
node {{
name
}}
}}
pageInfo {{
endCursor
hasNextPage
}}
}}
}}
}}
}}
}}
"""
data = run_query(query)
team = data.get('organization').get('teams').get('nodes')[0]
repos = team.get('repositories').get('edges')
hasNextPage = team.get('repositories').get('pageInfo').get('hasNextPage')
endCursor = team.get('repositories').get('pageInfo').get('endCursor')
if hasNextPage:
repos += get_team_repositories(team_name, repository_filter=f'first: 100, after: "{endCursor}"')
return sorted(unique(repos), key = lambda t: simplify(t['node']['name']))
def get_private_repositories(query):
data = run_query(query)
repos = data.get('organization').get('repositories')
count = len(repos.get('edges'))
total_count = repos.get('totalCount')
output = repos.get('edges')
while True:
repo_filters = f"isFork: false, first: 100, after: '{repos.get('pageInfo').get('endCursor')}'"
repos = run_query(query).get('organization').get('repositories')
count += len(repos.get('edges'))
output += repos.get('edges')
if count > total_count:
break
return sorted(unique(output), key = lambda t: simplify(t['node']['name']))
def get_branch_protection_rules(
owner,
repository_name,
branch_protection_filter = "first: 100"
):
review_dismissal_allowances_filter = "first: 100"
push_allowances_filter = "first: 100"
query = f"""
{{
repository(owner: "{owner}", name: "{repository_name}") {{
branchProtectionRules({branch_protection_filter}) {{
nodes {{
allowsDeletions,
allowsForcePushes,
pattern,
isAdminEnforced,
requiredApprovingReviewCount,
requiredStatusCheckContexts,
requiresApprovingReviews,
requiresCodeOwnerReviews,
requiresCommitSignatures,
requiresConversationResolution,
requiresLinearHistory,
requiresStatusChecks,
requiresStatusChecks,
requiresStrictStatusChecks,
restrictsPushes,
restrictsReviewDismissals,
dismissesStaleReviews,
pushAllowances({push_allowances_filter}) {{
nodes {{
actor {{
... on App {{
__typename
name
}}
... on Team {{
__typename
name
}}
... on User {{
__typename
login
}}
}}
}}
pageInfo {{
endCursor,
hasNextPage,
}},
totalCount,
}},
reviewDismissalAllowances({review_dismissal_allowances_filter}) {{
nodes {{
actor {{
... on Team {{
__typename
name
}}
... on User {{
__typename
name
}}
}}
}}
pageInfo {{
endCursor,
hasNextPage
}},
totalCount
}},
}},
pageInfo {{
endCursor
hasNextPage
}},
totalCount
}}
}}
}}
"""
data = run_query(query)
repo = data.get('repository')
output = repo.get('branchProtectionRules').get('nodes')
hasNextPage = repo.get('branchProtectionRules').get('hasNextPage')
if hasNextPage:
end_cursor = repo.get('branchProtectionRules').get('pageInfo').get('endCursor')
fltr = f"first: 100, after: {end_cursor}"
output += get_branch_protection_rules(owner, repository_name, fltr)
return output
def get_repositories(repository_filter="first: 100"):
query = f"""
{{
organization(login: "{LOGIN}") {{
repositories({repository_filter}) {{
totalCount,
nodes {{
name,
description,
homepageUrl,
isPrivate,
isArchived,
visibility,
hasIssuesEnabled,
hasProjectsEnabled,
hasWikiEnabled,
mergeCommitAllowed,
squashMergeAllowed,
rebaseMergeAllowed,
deleteBranchOnMerge,
}}
pageInfo {{
endCursor
hasNextPage
}}
}}
}}
}}
"""
data = run_query(query)
repos = data.get('organization').get('repositories')
output = repos.get('nodes')
hasNextPage = repos.get('pageInfo').get('hasNextPage')
if hasNextPage:
end_cursor = repos.get('pageInfo').get('endCursor')
repository_filter = f'first: 100, after: "{end_cursor}"'
output += get_repositories(repository_filter)
return sorted(output, key = lambda t: simplify(t['name']))
def get_collaborators_for_repository(repo_name, affiliation="ALL",
collaborators_filter="first: 100", owner=LOGIN):
collaborators_filter += f", affiliation: {affiliation}"
query = f"""
{{
repository(owner: "{owner}", name: "{repo_name}") {{
collaborators({collaborators_filter}) {{
pageInfo {{
endCursor
hasNextPage
}}
edges {{
permission,
node {{
login
}}
}}
}}
}}
}}
"""
data = run_query(query)
collaborators = data['repository']['collaborators']
hasNextPage = collaborators.get('pageInfo').get('hasNextPage')
end_cursor = collaborators.get('pageInfo').get('endCursor')
output = collaborators.get('edges')
if hasNextPage:
collaborators_filter = f"first: 100, after: {end_cursor}"
output += get_collaborators_for_repository(
repo_name,
affiliation=affiliation,
collaborators_filter=collaborators_filter,
owner=owner
)
return output
def get_all_users(query):
data = run_query(query)
users = data.get('organization').get('membersWithRole')
count = len(users.get('edges'))
total_count = users.get('totalCount')
output = users.get('edges')
while True:
users_filters = f"first: 100, after: '{users.get('pageInfo').get('endCursor')}'"
users = run_query(query).get('organization').get('membersWithRole')
count += len(users.get('edges'))
output += users.get('edges')
if count > total_count:
break
return sorted(unique(output), key = lambda t: simplify(t['node']['login']))
def bool_to_str(boolean):
if boolean:
return "true"
else:
return "false"
def tf_context_for_contexts(contexts):
output = []
count = len(contexts)
for idx, context in enumerate(contexts):
ctx = f'"{context}"'
if idx < count - 1:
ctx += ","
output.append(ctx)
return output
def tf_resource_for_actor(actor):
typename = actor.get('__typename')
output = ""
if typename == "User":
output = f"github_user.{simplify(actor.get('login'))}.node_id"
elif typename == "Team" or typename == "App":
output = f"github_team.{simplify(actor.get('name'))}.node_id"
return output
def tf_resources_for_actors(actors):
output = []
count = len(actors)
for idx, actor in enumerate(actors):
resource = tf_resource_for_actor(actor)
if idx < count - 1:
resource += ","
output.append(resource)
return output