-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathvalidate-workstreams.py
More file actions
209 lines (158 loc) · 6.71 KB
/
validate-workstreams.py
File metadata and controls
209 lines (158 loc) · 6.71 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
#!/usr/bin/env python3
"""
Validates workstreams.yml.
Usage:
python scripts/validate-workstreams.py [--install]
Pass --install to auto-install dependencies (pyyaml, jsonschema) before running.
People validation (gcLiaison/tcSponsor/specSponsor membership checks) requires
people.yml, which is checked into the repository. Validation fails if it is missing.
"""
import subprocess
import sys
from pathlib import Path
if (len(sys.argv) > 1) and (sys.argv[1] == "--install"):
subprocess.check_call([sys.executable, "-m", "pip", "install", "pyyaml", "jsonschema"])
import yaml
import jsonschema
REPO_ROOT = Path(__file__).parent.parent
SCRIPTS_DIR = REPO_ROOT / "scripts"
WORKSTREAMS_FILE = REPO_ROOT / "workstreams.yml"
WORKSTREAMS_SCHEMA = SCRIPTS_DIR / "schema" / "workstreams.schema.yml"
PEOPLE_FILE = REPO_ROOT / "people.yml"
TBD = "tbd"
NONE = "none"
KIND_REQUIRED_ROLES = {
"sig": {"gcLiaison", "tcSponsor"},
"initiative": {"lead"},
}
KIND_FORBIDDEN_ROLES = {
"initiative": {"gcLiaison", "tcSponsor"},
}
VALID_PARENT_KINDS = {
"sig": {"sig"},
"initiative": {"sig"},
}
KINDS_REQUIRING_PARENT = {"initiative"}
MEMBERSHIP_REQUIRED_ROLES = {"gcLiaison", "tcSponsor", "specSponsor"}
def load_schema(path: Path) -> dict:
with open(path) as f:
return yaml.safe_load(f)
def load_yaml(path: Path) -> object:
with open(path) as f:
return yaml.safe_load(f)
def validate_against_schema(data: object, schema: dict, label: str) -> list[str]:
errors = []
validator = jsonschema.Draft202012Validator(schema)
for err in sorted(validator.iter_errors(data), key=lambda e: list(e.absolute_path)):
path = " -> ".join(str(p) for p in err.absolute_path) or "(root)"
errors.append(f"[{label}] {path}: {err.message}")
return errors
def _entry_role_and_username(entry: dict) -> tuple[str, str]:
"""Return (role, username) from a single-key people entry."""
role = next(iter(entry))
val = entry[role]
if role == "tcSponsor":
username = val.get("username", "") if isinstance(val, dict) else ""
else:
username = val if isinstance(val, str) else ""
return role, username
def validate_workstreams_semantics(workstreams: list[dict], people_data: dict) -> list[str]:
errors: list[str] = []
seen_ids: set[str] = set()
for w in workstreams:
wid = w.get("id", "")
if wid in seen_ids:
errors.append(f"[{wid}] Duplicate workstream id")
seen_ids.add(wid)
id_to_workstream = {w["id"]: w for w in workstreams if "id" in w}
for w in workstreams:
wid = w.get("id", "(unknown)")
kind = w.get("kind", "")
parent_id = w.get("parent")
if parent_id is None or parent_id == NONE:
if kind in KINDS_REQUIRING_PARENT:
errors.append(f"[{wid}] kind '{kind}' requires a parent")
continue
if parent_id == wid:
errors.append(f"[{wid}] parent references itself")
continue
if parent_id not in id_to_workstream:
errors.append(f"[{wid}] parent '{parent_id}' does not exist")
continue
parent_kind = id_to_workstream[parent_id].get("kind", "")
if parent_kind not in VALID_PARENT_KINDS.get(kind, set()):
errors.append(
f"[{wid}] kind '{kind}' cannot have a parent of kind '{parent_kind}'"
)
for w in workstreams:
wid = w.get("id", "(unknown)")
visited = set()
current = wid
while current is not None and current != NONE:
if current in visited:
errors.append(f"[{wid}] parent chain contains a cycle")
break
visited.add(current)
current = id_to_workstream.get(current, {}).get("parent")
for w in workstreams:
wid = w.get("id", "(unknown)")
kind = w.get("kind", "")
if "sigCategory" in w and kind != "sig":
errors.append(
f"[{wid}] sigCategory is only valid on kind 'sig'"
)
person_roles = {next(iter(pr)) for pr in w.get("people", [])}
for required_role in KIND_REQUIRED_ROLES.get(kind, set()):
if required_role not in person_roles:
errors.append(
f"[{wid}] kind '{kind}' requires at least one '{required_role}'"
)
for forbidden_role in KIND_FORBIDDEN_ROLES.get(kind, set()) & person_roles:
errors.append(
f"[{wid}] kind '{kind}' must not specify '{forbidden_role}' "
"(inherited from parent SIG)"
)
teams = people_data.get("teams", {})
gc_members = {u.lower() for u in teams.get("governance-committee", [])}
tc_members = {u.lower() for u in teams.get("technical-committee", [])}
spec_sponsors = {u.lower() for u in teams.get("spec-sponsors", [])} | tc_members
for pr in w.get("people", []):
role, username = _entry_role_and_username(pr)
if username == TBD or role not in MEMBERSHIP_REQUIRED_ROLES:
continue
if role == "gcLiaison" and username.lower() not in gc_members:
errors.append(
f"[{wid}] '{username}' is assigned gcLiaison "
"but is not in the governance-committee team"
)
elif role == "tcSponsor" and username.lower() not in tc_members:
errors.append(
f"[{wid}] '{username}' is assigned tcSponsor "
"but is not in the technical-committee team"
)
elif role == "specSponsor" and username.lower() not in spec_sponsors:
errors.append(
f"[{wid}] '{username}' is assigned specSponsor "
"but is not in the spec-sponsors or technical-committee team"
)
return errors
def main() -> None:
all_errors: list[str] = []
workstreams_schema = load_schema(WORKSTREAMS_SCHEMA)
workstreams_data = load_yaml(WORKSTREAMS_FILE)
all_errors += validate_against_schema(workstreams_data, workstreams_schema, "workstreams.yml")
if not PEOPLE_FILE.exists():
print(f"Error: {PEOPLE_FILE.name} not found.", file=sys.stderr)
sys.exit(1)
with open(PEOPLE_FILE) as f:
people_data = yaml.safe_load(f)
if not all_errors:
all_errors += validate_workstreams_semantics(workstreams_data, people_data)
if all_errors:
for err in all_errors:
print(err, file=sys.stderr)
sys.exit(1)
count = len(workstreams_data)
print(f"OK — {count} workstream(s) validated (membership checks enabled).")
if __name__ == "__main__":
main()