-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadd_codeowners.py
More file actions
553 lines (469 loc) · 17.7 KB
/
Copy pathadd_codeowners.py
File metadata and controls
553 lines (469 loc) · 17.7 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
"""
Adds a codeowners file to a repository via pull request.
At a high level, this helper:
- Creates a feature branch for a repo on pcdshub
- Generates a CODEOWNERS file given the requested settings
- Commits CODEOWNERS file directly to branch
- Opens a pull request to the default branch
Some notes:
- All of this is done via the github API to avoid local file manipulation.
- A personal access token (PAT) is needed for API access. It should have access
to the organization's resources, and provide the following scopes:
- get repo: "Metadata" (read)
- get branch (create ref): "Contents" (write)
- create/update contents: "Contents" (write)
- create pull request: "Pull requests" (write)
- Because it seems impossible to scope a PAT to fork an internal / private repo
into a personal github accounts, everything is performed on the organization
- The token would need access to both the org and user's resources, currently
you can only choose one
- To do this on pcdshub currently you need bypass powers
"""
import argparse
import base64
import csv
from dataclasses import dataclass, fields
from enum import Enum
from pathlib import Path
from typing import Dict, List, Optional
from urllib.error import HTTPError
from ghapi.all import GhApi
from helpers import DryRunner
class CodeownerGroup(str, Enum):
ADMIN = "software-admin"
class LanguageOwners(str, Enum):
PYTHON = "python-reviewers"
TWINCAT = "twincat-reviewers"
EPICS = "epics-reviewers"
SHELL = "shell-reviewers"
C = "c-reviewers"
class SMEOwners(str, Enum):
VACUUM_SME = "vacuum-sme"
LASER_SME = "laser-sme"
MOTION_SME = "motion-sme"
OPTICS_SME = "optics-sme"
PMPS_SME = "pmps-sme"
UI_SME = "ui-sme"
LCLS_NAMING = "lcls-naming-council"
class AreaOwners(str, Enum):
CXI = "cxi-members"
LAS = "las-members"
MEC = "mec-members"
MFX = "mfx-members"
RIX = "rix-members"
TMO = "tmo-members"
TXI = "txi-members"
UED = "ued-members"
XCS = "xcs-members"
XPP = "xpp-members"
@dataclass
class RepoOwnerSettings:
owner: str
repo_name: str
sme_owners: List[SMEOwners]
lang_owners: List[LanguageOwners]
area_owners: List[AreaOwners]
@classmethod
def from_dict(cls, source: dict):
settings = cls(
owner=source["owner"],
repo_name=source["repo_name"],
sme_owners=[SMEOwners(value.strip()) for value in
source["sme_owners"].split(',') if value],
area_owners=[AreaOwners(value.strip()) for value in
source["area_owners"].split(',') if value],
lang_owners=[LanguageOwners(value.strip()) for value in
source["lang_owners"].split(',') if value],
)
settings.sme_owners.sort()
settings.area_owners.sort()
settings.lang_owners.sort()
return settings
GROUP_TO_EXT: Dict[LanguageOwners, list[str]] = {
LanguageOwners.PYTHON: ['*.py', '*.pyi', '*.pyc', '*.pyw', '*.pyx', '*.pyd'],
LanguageOwners.TWINCAT: ['*.tsproj', '*.plcproj', '*.tmc', '*.tpr', '*.xti',
'*.TcTTO', '*.TcPOU', '*.TcDUT', '*.TcGVL', '*.TcVis',
'*.TcVMO', '*.TcGTLO'],
LanguageOwners.C: ['*.c', '*.cpp', '*.cc', '*.h', '*.h++', '*.hh', '*.hpp'],
LanguageOwners.SHELL: ['*.sh', '*.zsh', '*.csh', '*.bash'],
LanguageOwners.EPICS: ['*.archive', '*.autosave', '*.cmd', '*.db', '*.dbd',
'*.edl', '*.ioc', '*.proto', '*.req',
'*.sub-arch', '*.sub-req', '*.substitutions',
'*.tpl-arch', '*.tpl-req', 'RELEASE_SITE',
'/configure/RELEASE'],
}
def create_codeowners_file(settings: RepoOwnerSettings) -> str:
"""
Create codeowners file based on RepoOwnerSettings
See https://confluence.slac.stanford.edu/x/u5Q9Hw for details.
By default:
- "default" = sme_owners + area_owners
- .github owned by admins only
- Default (*): default
- language specific files: default + lang_owners
Returns content encoded as base64 utf-8, in order to ensure the
payload is appropriate for github's REST API
"""
lines = []
# gather groups
base_default_owners = list(set(settings.sme_owners + settings.area_owners))
if base_default_owners:
default_owners = base_default_owners
elif not base_default_owners and settings.lang_owners:
default_owners = settings.lang_owners
else:
default_owners = [CodeownerGroup.ADMIN]
default_groups = ["@pcdshub/" + grp for grp in default_owners]
# default group
lines.append("# default group")
lines.append(f"* {' '.join(default_groups)}")
lines.append("")
# Language specific groups
if settings.lang_owners:
lines.append("# language-specific group(s)")
for group in settings.lang_owners:
lines.append(f"## {group.name} files")
specific_lang_group = ' '.join([f"@pcdshub/{grp}" for grp
in set([group] + base_default_owners)])
for rule in GROUP_TO_EXT[group]:
lines.append(f"{rule} {specific_lang_group}")
lines.append("")
# .github admin group
lines.append("# github folder holds administrative files")
lines.append(f".github/** @pcdshub/{CodeownerGroup.ADMIN}")
lines.append("")
base_file = "\n".join(lines)
# ensure file contents can be base64 encoded for submission to github API
encoded_content = base64.b64encode(base_file.encode('utf-8'))
return encoded_content.decode("utf-8")
def parse_repo_list(repo_data_path: str) -> List[RepoOwnerSettings]:
"""
Read and parse a repository data list from ``repo_data_path``
Expects a basic csv format (comma-delimited, first row are column headers)
Expects the following header names
- 'owner': repository owner, organization
- 'repo_name': repository name
- 'sme_owners': subject matter experts
- 'area_owners': hutch/area owners
- 'lang_owners': programming language experts
Parameters
----------
repo_data_path : str
path to the repo-data csv
Returns
-------
List[ProtectionGroup]
A list of ProtectionGroup's, holding settings for protection groups
"""
data_path = Path(repo_data_path)
if not data_path.exists:
print('repo data file does not exist')
return
repo_data = []
# Deal with extra columns
with open(data_path, 'r') as csvfile:
csv_reader = csv.DictReader(csvfile)
for row in csv_reader:
data_row = RepoOwnerSettings.from_dict(row)
repo_data.append(data_row)
return repo_data
@DryRunner()
def create_fork(
repo_name: str,
original_owner: str = "pcdshub",
api: Optional[GhApi] = None,
) -> None:
"""
Creates a fork of `original_owner`/`repo_name`on whatever account the
GITHUB_TOKEN has access to.
(The github api does not let you specify the fork destination, as making a
fork only makes sense on your account, not someone else's)
"""
if api is None:
api = GhApi() # requires a token for access
print(f" > Creating fork of {original_owner}/{repo_name}")
api.repos.create_fork(original_owner, repo_name, default_branch_only=True)
@DryRunner()
def create_branch(
owner: str,
repo_name: str,
branch_name: str,
default_branch_name: str = "master",
api: Optional[GhApi] = None,
) -> None:
"""
Create branch (`branch_name`) from `default_branch_name` on `owner`/`repo_name`
To do this we must identify the sha of the default branch, so that we can
specify the new branch be created with that sha as its head. This requires
a few extra calls to the github API
This will also first delete the branch named `branch_name` if it exists
before creating a new one.
"""
if api is None:
api = GhApi() # requires a token for access
print(f" > Create branch: {branch_name}")
# Create branch from most recent sha
try:
api.repos.get_branch(owner, repo_name, branch_name)
print(" >> found existing branch, deleting branch...")
api.git.delete_ref(owner, repo_name, f"heads/{branch_name}")
except HTTPError:
# branch does not exist, continue to create
pass
head_sha = api.repos.get_branch(
owner, repo_name, default_branch_name
)['commit']['sha']
print(f" >> Creating ref: {owner}/{repo_name}:{branch_name} from sha: {head_sha}")
api.git.create_ref(
owner,
repo_name,
f'refs/heads/{branch_name}',
head_sha
)
@DryRunner()
def add_codeowners_file_to_branch(
owner: str,
repo_name: str,
branch_name: str,
codeowner_data: str,
commit_message: str = "MNT: Adding CODEOWNERS file",
api: Optional[GhApi] = None,
) -> None:
"""
Add a CODEOWNERS file defined by `codeowner_data` to `branch_name`.
NOTE: This expects `codeowner_data` to be base64 encoded.
This could probably be generalized but I don't care enough right now.
"""
if api is None:
api = GhApi() # requires a token for access
print(f" > adding CODEOWNERS file to branch: {branch_name}")
try:
# if file exists, grab its sha so we can edit it
current_file_sha = api.repos.get_content(
owner,
repo_name,
".github/CODEOWNERS",
branch_name,
).sha
print(" >> File exists, will modify existing CODEOWNERS")
except HTTPError:
# file not found, create a new file
current_file_sha = None
print(" >> Adding new codeowners data...")
api.repos.create_or_update_file_contents(
owner=owner,
repo=repo_name,
path=".github/CODEOWNERS",
message=commit_message,
content=codeowner_data,
sha=current_file_sha,
branch=branch_name,
)
@DryRunner()
def create_pull_request(
repo_name: str,
contributor: str,
branch_name: str,
reviewers: List[str],
team_reviewers: List[str],
upstream: str = "pcdshub",
base_branch_name: str = "master",
title: str = "AUTOGENERATED: Pull Request Title",
body: str = "(autogenerated) Pull Request body",
api: Optional[GhApi] = None,
) -> None:
"""
Create a pull request to merge:
`contributor`/`branch_name` --> `upstream`/`base_branch_name`
"""
if api is None:
api = GhApi() # requires a token for access
# Create pull request
print(f" > Creating pull request: {title}")
resp = api.pulls.create(
owner=contributor,
repo=repo_name,
title=title,
head=f"{upstream}:{branch_name}", # This could also point to contributor
base=base_branch_name,
body=body,
maintainer_can_modify=True
)
print(f" > requesting reviews from individuals: {reviewers}, teams: {team_reviewers}")
api.pulls.request_reviewers(
owner=upstream,
repo=repo_name,
pull_number=resp['number'],
reviewers=reviewers,
team_reviewers=team_reviewers,
)
def add_codeowners_from_setting(
settings: RepoOwnerSettings,
user: str,
branch_name: str,
reviewers: List[str],
team_reviewers: List[str],
commit_message: str,
pr_title: str,
pr_body: str,
api: Optional[GhApi],
):
"""
Add a CODEOWNERS file from the specified `RepoOwnerSettings` object.
Also requires information about the actor (`user`), and the contribution
description metadata (branch_name, commit_message, pr_title, pr_body)
"""
if api is None:
api = GhApi() # requires a token for access
if reviewers is None:
reviewers = []
try:
repo_info = api.repos.get(settings.owner, settings.repo_name)
except HTTPError:
print(f" > Unable to access {settings.owner}/{settings.repo_name}")
return
if repo_info['archived']:
print(f" > skipping archived repo: {settings.repo_name}")
return
default_branch_name = repo_info["default_branch"]
create_branch(
owner=settings.owner,
repo_name=settings.repo_name,
branch_name=branch_name,
default_branch_name=default_branch_name,
api=api,
)
codeowner_file = create_codeowners_file(settings)
print("-------")
print(base64.b64decode(codeowner_file).decode('utf-8'))
print("-------")
add_codeowners_file_to_branch(
owner=settings.owner,
repo_name=settings.repo_name,
branch_name=branch_name,
codeowner_data=codeowner_file,
commit_message=commit_message,
api=api,
)
create_pull_request(
repo_name=settings.repo_name,
contributor=user,
branch_name=branch_name,
reviewers=reviewers,
team_reviewers=team_reviewers,
upstream=settings.owner,
base_branch_name=default_branch_name,
title=pr_title,
body=pr_body,
api=api,
)
def main(
owner: str = "pcdshub",
user: str = "pcdshub",
repo_name: str = "",
branch_name: str = "mnt_add_codeowners",
reviewers: Optional[List[str]] = None,
team_reviewers: Optional[List[str]] = None,
commit_message: str = "MNT: Adding CODEOWNERS file",
title="MNT: Adding CODEOWNERS file",
body="(autogenerated) adding codeowners file",
sme: Optional[List[str]] = None,
area: Optional[List[str]] = None,
lang: Optional[List[str]] = None,
repo_data_path: str = "",
write: bool = False
):
"""
Create codeowners files and submit them via pull request
"""
# Set dry-run option
DryRunner.set(run=write)
api = GhApi()
print(owner, user, repo_name, branch_name, reviewers)
# parse csv with settings
if repo_data_path:
repo_data = parse_repo_list(repo_data_path)
for settings in repo_data:
print(f"===== working on {settings.repo_name}... =====")
add_codeowners_from_setting(
settings, user, branch_name, reviewers, team_reviewers,
commit_message, title, body,
api=api
)
print("====================")
return
if not repo_name:
print("Neither repo name nor repo data path provided, nothing to do")
return
# apply on a per-repo basis
sme_owners = [SMEOwners(grp) for grp in sme or []]
area_owners = [AreaOwners(grp) for grp in area or []]
lang_owners = [LanguageOwners(grp) for grp in lang or []]
print(sme_owners, area_owners, lang_owners)
settings = RepoOwnerSettings(
owner=owner,
repo_name=repo_name,
sme_owners=sme_owners,
area_owners=area_owners,
lang_owners=lang_owners,
)
print(f"===== working on {settings.repo_name}... =====")
add_codeowners_from_setting(
settings, user, branch_name, reviewers, team_reviewers,
commit_message, title, body,
api=api
)
print("====================")
def _create_argparser() -> argparse.ArgumentParser:
"""
Create an ArgumentParser for add_codeowners
If we ever reuse this we should probably let users specify their commit
message, pr title, etc. But this is already an incredibly long cli call.
Returns
-------
argparse.ArgumentParser
"""
parser = argparse.ArgumentParser()
# specify an owner, repo, and settings
parser.add_argument("owner", type=str, default='pcdshub', nargs='?',
help='Organization or owner of the repository, "pcdshub"'
'by default')
parser.add_argument("user", type=str, default='pcdshub', nargs='?',
help="Name of account to contribute from, the fork will "
"created on this github account if required.")
parser.add_argument("branch_name", type=str, default="mnt_add_codeowners", nargs='?',
help="Name of branch to create and submit to upstream")
# manually specify settings
parser.add_argument("repo_name", type=str, default='', nargs='?',
help='Name of the repository')
parser.add_argument("--sme", type=str, nargs='*',
choices=[g.value for g in SMEOwners],
help='Subject Matter Experts for the repository.')
parser.add_argument("--area", type=str, nargs='*',
choices=[g.value for g in AreaOwners],
help='Area (Hutch) owners for the repository.')
parser.add_argument("--lang", type=str, nargs='*',
choices=[g.value for g in LanguageOwners],
help='Language experts for the repository.')
# Optionally specify everything at once
parser.add_argument("--repo-data-path", type=str, dest='repo_data_path',
help='Path to repo data csv. Expects columns for: '
f'{[f.name for f in fields(RepoOwnerSettings)]}')
# reviewers
parser.add_argument("--reviewers", type=str, nargs='*',
help="Reviewers to be added to the generated Pull Request")
parser.add_argument("--team-reviewers", type=str, nargs='*',
help="Team reviewers to be added to the generated Pull Request."
"e.g.: 'python-reviewers' (no @pcdshub/ necessary)")
# dry run?
parser.add_argument("--write", action="store_true", dest="write",
help="(by default False) Apply the changes. "
"If not specified, this will dry run and display the "
"proposed CODEOWNER file.")
return parser
def _main(args=None):
"""CLI entrypoint."""
parser = _create_argparser()
return main(**vars(parser.parse_args(args=args)))
if __name__ == "__main__":
_main()