-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_codeowners.py
executable file
·66 lines (52 loc) · 1.78 KB
/
generate_codeowners.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
#!/usr/bin/env python
import sys
from collections import defaultdict
from pathlib import Path
from typing import Dict
import yaml
from yamale import YamaleError, make_data, make_schema, validate
from yamale.validators import DefaultValidators
OWNERS = Path("owners.yaml")
OWNERS_SCHEMA = Path("owners_schema.yaml")
def check_owners_file():
for f in sys.argv[1:]:
if f == str(OWNERS):
return False
return True
def validate_schema():
validators = DefaultValidators.copy()
schema = make_schema(OWNERS_SCHEMA, validators=validators)
config_data = make_data(OWNERS)
try:
validate(schema=schema, data=config_data, strict=True)
except YamaleError as e:
exit(
f"Error validating config from {str(OWNERS)}. Errors: {','.join([str(result) for result in e.results])}"
)
def main():
if check_owners_file():
return
validate_schema()
with OWNERS.open("r") as ows:
try:
owners_file = yaml.safe_load(ows)
except yaml.YAMLError as exc:
exit(str(exc))
codeowners_root = ""
codeowners = ""
for team in owners_file["teams"]:
group_name = team.get("group_name", "root")
joined_owners = " ".join([f"@{o}" for o in team.get("members", [])])
paths = ""
for path in team.get("owned_code_paths", []):
paths += f"{path} {joined_owners}\n"
if group_name != "root":
codeowners += f"\n[{group_name}]\n"
codeowners += paths
else:
codeowners_root += paths
with Path("CODEOWNERS").open("w") as owners:
owners.write(codeowners_root + "\n")
owners.write(codeowners)
if __name__ == "__main__":
main()