Repository navigation
Expand file tree
/
Copy pathchanged_paths.py
More file actions
197 lines (161 loc) ยท 6.55 KB
/
Copy pathchanged_paths.py
File metadata and controls
197 lines (161 loc) ยท 6.55 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
"""Evaluate GitHub Actions path filters outside of a workflow trigger.
A workflow that ``on.paths``/``on.paths-ignore`` filters out never starts, so its
checks are never reported and a branch rule requiring them blocks the pull
request forever. Leaving the trigger unfiltered and skipping the jobs instead
reports those checks as skipped, which counts as a pass. This script evaluates
the filter that used to sit on the trigger, so the jobs skip on exactly the pull
requests the trigger used to drop.
Reads the pull request's changed files from stdin, one JSON object per line
carrying the ``filename`` and ``previous_filename`` fields of the pulls files
API, and writes ``true`` or ``false`` to stdout. The filter comes from whichever
of ``FILTER_PATHS`` or ``FILTER_PATHS_IGNORE`` is set, as a newline-separated
pattern list using GitHub's filter pattern syntax.
"""
from __future__ import annotations
import json
import os
import re
import sys
from collections.abc import Iterable, Sequence
# GitHub allows only alphanumerics and ranges inside a filter pattern's `[]`, so
# the contents pass through to the regex unescaped once they look like that.
_RANGE_BODY = re.compile(r"[A-Za-z0-9-]+")
def translate(pattern: str) -> re.Pattern[str]:
"""Compile one GitHub filter pattern into a regular expression.
Args:
pattern: A filter pattern, without any leading ``!``.
Returns:
A regex matching the repo-relative paths the pattern selects.
Raises:
ValueError: If the pattern contains an unsupported character range.
"""
out: list[str] = []
index = 0
while index < len(pattern):
char = pattern[index]
# A `**/` that starts a segment spans zero or more whole directories,
# which is what makes `**/*.md` cover a top-level README.md as well as
# docs/guide.md. Embedded, as in `foo**/bar`, it is `**` then a slash.
if pattern.startswith("**/", index) and (
index == 0 or pattern[index - 1] == "/"
):
out.append("(?:.*/)?")
index += 3
continue
if pattern.startswith("**", index):
out.append(".*")
index += 2
continue
if char == "[":
end = pattern.find("]", index + 1)
body = pattern[index + 1 : end] if end != -1 else ""
if not _RANGE_BODY.fullmatch(body):
msg = f"unsupported character range in filter pattern: {pattern!r}"
raise ValueError(msg)
out.append(f"[{body}]")
index = end + 1
continue
if char == "\\" and index + 1 < len(pattern):
out.append(re.escape(pattern[index + 1]))
index += 2
continue
if char == "*":
out.append("[^/]*")
elif char in "?+":
# GitHub reads these as quantifiers on the preceding character.
out.append(char)
else:
out.append(re.escape(char))
index += 1
return re.compile("".join(out))
def compile_filters(patterns: Iterable[str]) -> list[tuple[bool, re.Pattern[str]]]:
"""Compile an ordered filter pattern list once, for reuse across paths.
Args:
patterns: Filter patterns, each optionally prefixed with ``!``.
Returns:
``(negated, regex)`` pairs in the order the patterns were given.
"""
return [
(pattern.startswith("!"), translate(pattern.removeprefix("!")))
for pattern in patterns
]
def selects(filters: Sequence[tuple[bool, re.Pattern[str]]], path: str) -> bool:
"""Return whether a compiled filter list selects one path.
Args:
filters: Compiled filters, in pattern order.
path: A repo-relative changed path.
Returns:
True when the last pattern matching the path is a positive one.
"""
selected = False
for negated, regex in filters:
if regex.fullmatch(path):
selected = not negated
return selected
def changed_files(entries: Iterable[str]) -> list[str]:
"""Collect every path a pull request's changed files touch.
A rename touches two paths, but the API names only the new one in
``filename``. Without ``previous_filename`` a code file renamed to ``*.md``
would pass for a Markdown-only change and skip every job it should run.
Args:
entries: One JSON object per changed file, as the pulls files API
returns it.
Returns:
The changed paths, with both names of a renamed file.
"""
paths: list[str] = []
for line in entries:
entry = json.loads(line)
paths.append(entry["filename"])
if previous := entry.get("previous_filename"):
paths.append(previous)
return paths
def triggers(
changed: Sequence[str],
paths: Sequence[str] = (),
paths_ignore: Sequence[str] = (),
) -> bool:
"""Return whether a path filter would have started a run for these changes.
Args:
changed: The repo-relative paths the pull request changes.
paths: ``on.paths`` patterns; a run starts when a changed path matches.
paths_ignore: ``on.paths-ignore`` patterns; a run starts when a changed
path matches none of them.
Returns:
True when the filter selects the change set. A change set with no paths
at all also returns True, so an unreadable diff runs the jobs rather
than silently skipping them.
"""
if not changed:
return True
if paths:
filters = compile_filters(paths)
return any(selects(filters, path) for path in changed)
filters = compile_filters(paths_ignore)
return any(not selects(filters, path) for path in changed)
def lines(text: str) -> list[str]:
"""Split a newline-separated input into stripped, non-empty entries.
Args:
text: The raw input.
Returns:
The non-empty lines, stripped of surrounding whitespace.
"""
return [stripped for line in text.splitlines() if (stripped := line.strip())]
def main() -> int:
"""Evaluate the configured filter against the changed files on stdin.
Returns:
The process exit code.
"""
paths = lines(os.environ.get("FILTER_PATHS", ""))
paths_ignore = lines(os.environ.get("FILTER_PATHS_IGNORE", ""))
if bool(paths) == bool(paths_ignore):
print(
"set exactly one of FILTER_PATHS or FILTER_PATHS_IGNORE",
file=sys.stderr,
)
return 2
changed = changed_files(lines(sys.stdin.read()))
print("true" if triggers(changed, paths, paths_ignore) else "false")
return 0
if __name__ == "__main__":
sys.exit(main())