-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreusable_ci_scope.py
More file actions
385 lines (322 loc) · 12.8 KB
/
reusable_ci_scope.py
File metadata and controls
385 lines (322 loc) · 12.8 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
#!/usr/bin/env python3
"""Select reusable CI scenarios from changed repository inputs.
Scenario entries can opt into scoped selection by adding any of these fields:
- ``scope: {"paths": ["tests/**"], "reason": "tests changed"}``
- ``scope_paths: ["tests/**"]``
- ``paths: ["tests/**"]``
If no scenario has scope metadata, selection falls back to the full matrix.
That keeps callers conservative until they explicitly annotate a matrix.
"""
from __future__ import annotations
import argparse
import fnmatch
import json
import os
import sys
from dataclasses import dataclass, field
from pathlib import PurePosixPath
from typing import Any
JsonObject = dict[str, Any]
MatrixInput = list[JsonObject] | JsonObject
@dataclass(frozen=True)
class SelectionOptions:
force_full: bool = False
strip_scope: bool = True
@dataclass(frozen=True)
class SelectedMatrix:
workflow_name: str
matrix: MatrixInput
scenarios: list[JsonObject]
selected_count: int
total_count: int
reason: str
force_full: bool = False
scope_found: bool = False
matched_patterns: tuple[str, ...] = field(default_factory=tuple)
def _options_from(value: Any) -> SelectionOptions:
if isinstance(value, SelectionOptions):
return value
if value is None:
return SelectionOptions()
if isinstance(value, dict):
return SelectionOptions(
force_full=bool(value.get("force_full", False)),
strip_scope=bool(value.get("strip_scope", True)),
)
return SelectionOptions(
force_full=bool(getattr(value, "force_full", False)),
strip_scope=bool(getattr(value, "strip_scope", True)),
)
def _normalize_path(path: str) -> str:
normalized = str(PurePosixPath(path.replace("\\", "/")))
while normalized.startswith("./"):
normalized = normalized[2:]
return normalized
def _normalize_matrix(full_matrix: MatrixInput) -> tuple[list[JsonObject], str]:
if isinstance(full_matrix, list):
return list(full_matrix), "list"
if isinstance(full_matrix, dict):
include = full_matrix.get("include")
if isinstance(include, list):
return list(include), "include"
raise TypeError("full_matrix must be a list of scenarios or a matrix object with include")
def _restore_matrix(
selected: list[JsonObject], full_matrix: MatrixInput, shape: str
) -> MatrixInput:
if shape == "list":
return selected
if not isinstance(full_matrix, dict):
raise TypeError("matrix object shape requires a dictionary input")
restored = dict(full_matrix)
restored["include"] = selected
return restored
def _scope_for(scenario: JsonObject) -> tuple[list[str], str | None]:
scope = scenario.get("scope")
reason: str | None = None
paths: Any = None
if isinstance(scope, dict):
paths = (
scope.get("paths")
or scope.get("path_globs")
or scope.get("changed_paths")
or scope.get("changed-files")
)
raw_reason = scope.get("reason")
if isinstance(raw_reason, str) and raw_reason.strip():
reason = raw_reason.strip()
elif isinstance(scope, (list, str)):
paths = scope
if paths is None:
paths = (
scenario.get("scope_paths")
or scenario.get("scope-paths")
or scenario.get("paths")
or scenario.get("changed_paths")
or scenario.get("changed-paths")
)
if isinstance(paths, str):
scope_paths = [paths]
elif isinstance(paths, list):
scope_paths = [str(path) for path in paths if str(path).strip()]
else:
scope_paths = []
return scope_paths, reason
def _strip_scope_metadata(scenario: JsonObject) -> JsonObject:
stripped = dict(scenario)
for key in (
"scope",
"scope_paths",
"scope-paths",
"paths",
"changed_paths",
"changed-paths",
):
stripped.pop(key, None)
return stripped
def _path_matches(path: str, pattern: str) -> bool:
normalized_path = _normalize_path(path)
normalized_pattern = _normalize_path(pattern)
if normalized_pattern.endswith("/"):
return normalized_path.startswith(normalized_pattern)
has_glob = any(char in normalized_pattern for char in "*?[]")
if not has_glob:
return normalized_path == normalized_pattern or normalized_path.startswith(
f"{normalized_pattern}/"
)
if fnmatch.fnmatchcase(normalized_path, normalized_pattern):
return True
if normalized_pattern.startswith("**/"):
return fnmatch.fnmatchcase(normalized_path, normalized_pattern[3:])
return False
def _changed_roots(changed_files: list[str]) -> str:
roots = sorted(
{f"{path.split('/', 1)[0]}/" if "/" in path else path for path in changed_files if path}
)
if not roots:
return "changed files were unavailable"
if len(roots) == 1:
return f"only `{roots[0]}` changed"
if len(roots) <= 3:
return "changed inputs touched " + ", ".join(f"`{root}`" for root in roots)
return f"changed inputs touched {len(roots)} top-level paths"
def _scenario_name(scenario: JsonObject) -> str:
for key in ("name", "scenario", "id"):
value = scenario.get(key)
if value:
return str(value)
return "unnamed"
def select_scenarios(
workflow_name: str,
changed_files: list[str],
full_matrix: MatrixInput,
options: SelectionOptions | JsonObject | Any | None = None,
) -> SelectedMatrix:
"""Return the reduced matrix for the calling workflow."""
parsed_options = _options_from(options)
scenarios, shape = _normalize_matrix(full_matrix)
normalized_changes = [_normalize_path(path) for path in changed_files if str(path).strip()]
total = len(scenarios)
if parsed_options.force_full:
selected = [
_strip_scope_metadata(scenario) if parsed_options.strip_scope else scenario
for scenario in scenarios
]
matrix = _restore_matrix(selected, full_matrix, shape)
return SelectedMatrix(
workflow_name=workflow_name,
matrix=matrix,
scenarios=selected,
selected_count=len(selected),
total_count=total,
reason="force_full requested",
force_full=True,
scope_found=any(bool(_scope_for(scenario)[0]) for scenario in scenarios),
)
scoped_entries = [(scenario, _scope_for(scenario)) for scenario in scenarios]
scope_found = any(bool(scope_paths) for _, (scope_paths, _) in scoped_entries)
if not scope_found:
selected = list(scenarios)
matrix = _restore_matrix(selected, full_matrix, shape)
return SelectedMatrix(
workflow_name=workflow_name,
matrix=matrix,
scenarios=selected,
selected_count=len(selected),
total_count=total,
reason="no reusable CI scope metadata was provided",
scope_found=False,
)
if not normalized_changes:
selected = [
_strip_scope_metadata(scenario) if parsed_options.strip_scope else scenario
for scenario in scenarios
]
matrix = _restore_matrix(selected, full_matrix, shape)
return SelectedMatrix(
workflow_name=workflow_name,
matrix=matrix,
scenarios=selected,
selected_count=len(selected),
total_count=total,
reason="changed files were unavailable",
scope_found=True,
)
selected_scenarios: list[JsonObject] = []
matched_patterns: list[str] = []
matched_reasons: list[str] = []
for scenario, (scope_paths, scope_reason) in scoped_entries:
if not scope_paths:
selected_scenarios.append(
_strip_scope_metadata(scenario) if parsed_options.strip_scope else scenario
)
matched_reasons.append(f"`{_scenario_name(scenario)}` is unscoped")
continue
matches = [
pattern
for pattern in scope_paths
if any(_path_matches(path, pattern) for path in normalized_changes)
]
if matches:
selected_scenarios.append(
_strip_scope_metadata(scenario) if parsed_options.strip_scope else scenario
)
matched_patterns.extend(matches)
if scope_reason:
matched_reasons.append(scope_reason)
reason = _changed_roots(normalized_changes)
if matched_reasons:
reason = matched_reasons[0]
matrix = _restore_matrix(selected_scenarios, full_matrix, shape)
return SelectedMatrix(
workflow_name=workflow_name,
matrix=matrix,
scenarios=selected_scenarios,
selected_count=len(selected_scenarios),
total_count=total,
reason=reason,
scope_found=True,
matched_patterns=tuple(dict.fromkeys(matched_patterns)),
)
def describe_selection(selected: SelectedMatrix, full: MatrixInput) -> str:
"""Return a one-line human-readable rationale for a selected matrix."""
full_scenarios, _shape = _normalize_matrix(full)
total = len(full_scenarios)
selected_count = selected.selected_count
if selected.force_full:
return f"running {total}/{total} scenarios because force_full was requested"
return f"running {selected_count}/{total} scenarios because {selected.reason}"
def _load_json(raw: str, label: str) -> Any:
try:
return json.loads(raw)
except json.JSONDecodeError as exc:
raise SystemExit(f"{label} must be valid JSON: {exc}") from exc
def _parse_changed_files(args: argparse.Namespace) -> list[str]:
if args.changed_files_json:
parsed = _load_json(args.changed_files_json, "--changed-files-json")
if not isinstance(parsed, list):
raise SystemExit("--changed-files-json must be a JSON array")
return [str(path) for path in parsed]
if args.changed_files_file:
with open(args.changed_files_file, encoding="utf-8") as handle:
return [line.strip() for line in handle if line.strip()]
env_value = os.environ.get("CHANGED_FILES_JSON", "")
if env_value:
parsed = _load_json(env_value, "CHANGED_FILES_JSON")
if not isinstance(parsed, list):
raise SystemExit("CHANGED_FILES_JSON must be a JSON array")
return [str(path) for path in parsed]
return []
def _parse_matrix(args: argparse.Namespace) -> MatrixInput:
if args.matrix_json:
parsed = _load_json(args.matrix_json, "--matrix-json")
elif args.matrix_file:
with open(args.matrix_file, encoding="utf-8") as handle:
parsed = _load_json(handle.read(), "--matrix-file")
else:
raise SystemExit("one of --matrix-json or --matrix-file is required")
if not isinstance(parsed, (list, dict)):
raise SystemExit("matrix must be a JSON list or object")
return parsed
def _write_github_outputs(path: str, outputs: dict[str, str]) -> None:
with open(path, "a", encoding="utf-8") as handle:
for key, value in outputs.items():
delimiter = f"__REUSABLE_CI_SCOPE_{key.upper()}__"
while delimiter in value:
delimiter = f"_{delimiter}_"
handle.write(f"{key}<<{delimiter}\n")
handle.write(f"{value}\n")
handle.write(f"{delimiter}\n")
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--workflow-name", required=True)
parser.add_argument("--changed-files-json")
parser.add_argument("--changed-files-file")
parser.add_argument("--matrix-json")
parser.add_argument("--matrix-file")
parser.add_argument("--force-full", action="store_true")
parser.add_argument("--keep-scope-metadata", action="store_true")
parser.add_argument("--github-output", default=os.environ.get("GITHUB_OUTPUT", ""))
return parser
def main(argv: list[str] | None = None) -> int:
args = build_parser().parse_args(argv)
full_matrix = _parse_matrix(args)
selected = select_scenarios(
args.workflow_name,
_parse_changed_files(args),
full_matrix,
SelectionOptions(force_full=args.force_full, strip_scope=not args.keep_scope_metadata),
)
rationale = describe_selection(selected, full_matrix)
matrix_json = json.dumps(selected.matrix, separators=(",", ":"), sort_keys=True)
outputs = {
"matrix": matrix_json,
"selected_count": str(selected.selected_count),
"total_count": str(selected.total_count),
"rationale": rationale,
}
if args.github_output:
_write_github_outputs(args.github_output, outputs)
print(json.dumps({**outputs, "matrix": selected.matrix}, sort_keys=True))
return 0
if __name__ == "__main__": # pragma: no cover - CLI entry point
raise SystemExit(main(sys.argv[1:]))