-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_repos.py
403 lines (304 loc) · 12.8 KB
/
update_repos.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import base64
import urllib.parse
import feedparser
import httpx
import json
import gzip
import pathlib
import re
import os
from urllib.request import Request, urlopen
root = pathlib.Path(__file__).parent.resolve()
github_api_base_url = "https://api.github.com"
GITHUB_USER = "credfeto"
GITHUB_TOKEN = os.environ.get("SOURCE_PUSH_TOKEN", "")
TEAMCITY_TOKEN = os.environ.get("TEAMCITY_READ_API_KEY", "")
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36 Edg/83.0.478.56"
def base_main_branch_protection_settings():
return {
"required_status_checks": {
"strict": True,
"checks": [
{
"context": "does-not-contain-secrets",
"app_id": 15368
},
{
"context": "has-no-merge-conflicts",
"app_id": 15368
},
{
"context": "include-changelog-entry",
"app_id": 15368
},
{
"context": "no-ignored-files",
"app_id": 15368
},
{
"context": "has-no-file-or-folder-case-sensitivity-issues",
"app_id": 15368
},
{
"context": "change-log-entry-is-in-unreleased",
"app_id": 15368
},
{
"context": "lint-code",
"app_id": 15368
},
{
"context": "dependency-review",
"app_id": 15368
}
]
},
"required_pull_request_reviews": {
"dismiss_stale_reviews": True,
"require_code_owner_reviews": True,
"required_approving_review_count": 1
},
"restrictions": None,
"required_signatures": False,
"enforce_admins": False,
"required_linear_history": False,
"allow_force_pushes": False,
"allow_deletions": False,
"required_conversation_resolution": True
}
def have_branch_protection_settings_changed(existing_settings, new_settings):
if "required_status_checks" not in existing_settings:
return True
# Branch Must Be Up-to date
if existing_settings["required_status_checks"]["strict"] != new_settings["required_status_checks"]["strict"]:
print("required_status_checks:strict different")
return True
# checks [ check each context ]
existing_checks = []
if "checks" in existing_settings["required_status_checks"]:
existing_checks = existing_settings["required_status_checks"]["checks"]
new_checks = new_settings["required_status_checks"]["checks"]
for new_check in new_checks:
existing_check = None
for candidate in existing_checks:
if candidate["context"] == new_check["context"]:
existing_check = candidate
break
if not existing_check:
print("required_status_checks:checks:" + new_check["context"] + " different (missing)")
return True
existing_app_id = existing_check["app_id"]
new_app_id = new_check["app_id"]
if new_app_id == -1:
if existing_app_id is not None:
print("required_status_checks:checks:" + new_check["context"] + " different (app_id: target)")
return True
else:
if existing_app_id is None:
print("required_status_checks:checks:" + new_check["context"] + " different (app_id: source)")
return True
if existing_app_id != new_app_id:
print("required_status_checks:checks:" + new_check["context"] + " different (app_id: different ids)")
return True
if existing_settings["required_status_checks"]["checks"] != new_settings["required_status_checks"]["checks"]:
print("required_status_checks:checks different")
return True
# required_pull_request_reviews : dismiss_stale_reviews
if existing_settings["required_pull_request_reviews"]["dismiss_stale_reviews"] != new_settings["required_pull_request_reviews"]["dismiss_stale_reviews"]:
print("required_pull_request_reviews:dismiss_stale_reviews different")
return True
# required_pull_request_reviews : require_code_owner_reviews
if existing_settings["required_pull_request_reviews"]["require_code_owner_reviews"] != new_settings["required_pull_request_reviews"]["require_code_owner_reviews"]:
print("required_pull_request_reviews:require_code_owner_reviews different")
return True
# required_pull_request_reviews : required_approving_review_count
if existing_settings["required_pull_request_reviews"]["required_approving_review_count"] != new_settings["required_pull_request_reviews"]["required_approving_review_count"]:
print("required_pull_request_reviews:required_approving_review_count different")
return True
# required_signatures
if existing_settings["required_signatures"]["enabled"] != new_settings["required_signatures"]:
print("required_signatures different")
return True
# enforce_admins
if existing_settings["enforce_admins"]["enabled"] != new_settings["enforce_admins"]:
print("enforce_admins different")
return True
# required_linear_history
if existing_settings["required_linear_history"]["enabled"] != new_settings["required_linear_history"]:
return True
# allow_force_pushes
if existing_settings["allow_force_pushes"]["enabled"] != new_settings["allow_force_pushes"]:
return True
# allow_deletions
if existing_settings["allow_deletions"]["enabled"] != new_settings["allow_deletions"]:
return True
# required_conversation_resolution
if existing_settings["required_conversation_resolution"]["enabled"] != new_settings["required_conversation_resolution"]:
return True
return False
def get_branch_protection_settings(owner, repo, branch):
try:
settings = get_github("/repos/" + owner + "/" + repo + "/branches/"+branch+"/protection")
except Exception:
return None
return settings
def set_branch_protection_settings(owner, repo, branch, settings):
try:
settings = put_github("/repos/" + owner + "/" + repo + "/branches/"+branch+"/protection", settings)
except Exception as e:
print(e)
print("############################################################")
return False
print(settings)
return True
def basic_auth(user, password):
message = user + ":" + password
message_bytes = message.encode('ascii')
base64_bytes = base64.b64encode(message_bytes)
base64_message = base64_bytes.decode('ascii')
return base64_message
def get_github(path):
url = github_api_base_url + path
print("Connect to GET " + url)
# print("Token: " + TEAMCITY_TOKEN)
token = basic_auth(GITHUB_USER, GITHUB_TOKEN)
request = Request(url)
request.add_header('Accept', 'application/json')
request.add_header('Accept-Encoding', 'deflate, gzip')
request.add_header('Authorization', 'BASIC ' + token)
request.add_header('User-Agent', USER_AGENT)
response = urlopen(request)
content = response.read()
try:
content = gzip.decompress(content)
except gzip.BadGzipFile as e:
return json.loads(content)
return json.loads(content)
def put_github(path, data):
url = github_api_base_url + path
print("Connect to PUT " + url)
# print("Token: " + TEAMCITY_TOKEN)
token = basic_auth(GITHUB_USER, GITHUB_TOKEN)
json_data = json.dumps(data)
print(json_data)
post_data = json_data.encode()
request = Request(url=url, data=post_data, method='PUT')
request.add_header('Accept', 'application/json')
request.add_header('Accept-Encoding', 'deflate, gzip')
request.add_header('Content-Type', 'application/json')
request.add_header('Authorization', 'BASIC ' + token)
request.add_header('User-Agent', USER_AGENT)
print(request.get_method())
response = urlopen(request)
content = response.read()
try:
content = gzip.decompress(content)
except gzip.BadGzipFile as e:
return json.loads(content)
return json.loads(content)
def patch_github(path, data):
url = github_api_base_url + path
print("Connect to PATCH " + url)
# print("Token: " + TEAMCITY_TOKEN)
token = basic_auth(GITHUB_USER, GITHUB_TOKEN)
json_data = json.dumps(data)
print(json_data)
post_data = json_data.encode()
request = Request(url=url, data=post_data, method='PATCH')
request.add_header('Accept', 'application/json')
request.add_header('Accept-Encoding', 'deflate, gzip')
request.add_header('Content-Type', 'application/json')
request.add_header('Authorization', 'BASIC ' + token)
request.add_header('User-Agent', USER_AGENT)
print(request.get_method())
response = urlopen(request)
content = response.read()
try:
content = gzip.decompress(content)
except gzip.BadGzipFile as e:
return json.loads(content)
return json.loads(content)
def repo_url_to_owner_and_name(repo_url):
# git@github.com:funfair-tech/funfair-games.git
# https://github.com/funfair-tech/funfair-labs-tictactoe-server
if repo_url.startswith("git@github.com:"):
split = repo_url[15:-4].split("/")
return {
"owner": split[0],
"repo": split[1]
}
if repo_url.startswith("https://github.com/"):
split = repo_url[8:].split("/")
return {
"owner": split[1],
"repo": split[2]
}
return None
def add_teamcity_build(settings, project_name):
settings["required_status_checks"]["checks"].append({"context": project_name, "app_id": -1})
def add_github_build(settings, project_name):
settings["required_status_checks"]["checks"].append({"context": project_name, "app_id": 15368})
def has_existing_check(existing_settings, name):
if "required_status_checks" not in existing_settings:
return False
if "checks" not in existing_settings["required_status_checks"]:
return False
checks = existing_settings["required_status_checks"]["checks"]
for check in checks:
if check["context"] == name:
return True
return False
def update_repo_settings(owner, name):
repo_settings = {
"has_issues": True,
"has_projects": False,
"has_wiki": False,
"allow_squash_merge": False,
"allow_merge_commit": True,
"allow_rebase_merge": False,
"allow_auto_merge": True,
"delete_branch_on_merge": True,
"allow_update_branch": True,
"archive-program-opt-out-feature": True,
"merge_commit_title": "PR_TITLE",
"merge_commit_message": "PR_BODY"
}
patch_github("/repos/" + owner + "/" + name, repo_settings)
def add_existing_github_check(existing_settings, new_settings, build_name):
if has_existing_check(existing_settings, build_name):
print("****** Found Matching Build: " + build_name)
add_github_build(new_settings, build_name)
print(new_settings)
def update():
if GITHUB_TOKEN == "":
raise "Invalid Github token"
repository_list_file = root / "personal/repos.lst"
print(repository_list_file)
repos = repository_list_file.open("r").read().splitlines()
print(repos)
for repo in repos:
print("*****************************************************************************")
print(repo)
repo_parts = repo_url_to_owner_and_name(repo)
if repo_parts:
update_repo_settings(repo_parts["owner"], repo_parts["repo"])
if repo == 'git@github.com:credfeto/auto-update-config.git':
continue
if repo == 'git@github.com:credfeto/credfeto.git':
continue
new_settings = base_main_branch_protection_settings()
print(new_settings)
existing_settings = get_branch_protection_settings(repo_parts["owner"], repo_parts["repo"], 'main')
print(existing_settings)
if existing_settings:
add_existing_github_check(existing_settings, new_settings, "build-contracts")
print("**** CHECK UPDATE")
changed = have_branch_protection_settings_changed(existing_settings, new_settings)
if changed:
set_branch_protection_settings(repo_parts["owner"], repo_parts["repo"], 'main', new_settings)
else:
set_branch_protection_settings(repo_parts["owner"], repo_parts["repo"], 'main', new_settings)
# ABORT AT FIRST ONE FOR NOW
# break
if __name__ == "__main__":
update()