feat(ci): run the molecule scenarios of the roles that use a changed role
The scenario picker assumed the roles were independent and a test enforced it. Wave 2 roles call other Collection roles (traefik includes ufw and the internal _binary_update), so a change to the called role must also run the callers' scenarios. A role uses another when a file under its directory names it as mortis.kamino.<role>; the relation is followed transitively and read from the tree under test. A role the pull request deletes or renames still counts as used by the roles that name it, so their scenarios run and show the break. The independence test becomes a test that fails a bare or templated role name in an include, import (also in the k=v form), play roles: entry or meta dependency, and task includes that reach outside the role through '..'. It walks .yml and .yaml files and has its own cases for each form. This lands before the first such role because the workflow runs the picker from the base branch's copy. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
30ce518f00
commit
fcb51da065
3 files changed
+271
-25
No files matched your search
@@ -11,15 +11,21 @@ workflow runs `molecule test` in each.
|
||||
Only a pull request is narrowed, and only when <base-ref> is given and EVENT_NAME is
|
||||
`pull_request`. Then the diff of <base-ref>...HEAD decides:
|
||||
|
||||
* a file under roles/<role>/ selects that role's scenarios, and nothing else;
|
||||
* a file under roles/<role>/ selects that role's scenarios, and those of every role that
|
||||
uses it (below);
|
||||
* a file on the IGNORED list (documentation, changelog, repository tooling that no
|
||||
scenario reads) selects nothing;
|
||||
* any other file selects every scenario.
|
||||
|
||||
The roles do not depend on one another, so a change inside one role cannot alter another
|
||||
role's result. If one ever does (a role calling another, a shared plugin), the shared file
|
||||
sits outside roles/<role>/ and so already selects everything; only an explicit role-to-role
|
||||
dependency would need a rule here.
|
||||
A role uses another when any file under its directory names it as `mortis.kamino.<other>`
|
||||
(an include_role, a scenario's prepare play, a comment). The relation is transitive: when
|
||||
`a` names `b` and `b` names `c`, a change to `c` selects all three. The roles call each other
|
||||
by that full name only (a test checks includes, imports, play `roles:` entries and meta
|
||||
dependencies for a bare or templated role name), so the text search finds every use. It reads the
|
||||
tree under test, not the base: a pull request that drops a use also changes the using role,
|
||||
which then selects itself, and a role the pull request deletes or renames still counts as used
|
||||
by the roles that name it. A shared file outside roles/<role>/ (a plugin) already selects
|
||||
everything.
|
||||
|
||||
Every other case runs everything: a push, a manual dispatch, a scheduled run, no <base-ref>, a git
|
||||
failure.
|
||||
@@ -42,6 +48,7 @@ control characters in paths escaped so a crafted file name cannot inject a log c
|
||||
|
||||
import fnmatch
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
@@ -123,6 +130,48 @@ def discover(root="."):
|
||||
return found
|
||||
|
||||
|
||||
ROLE_REFERENCE = re.compile(r"mortis\.kamino\.([A-Za-z0-9_]+)")
|
||||
|
||||
|
||||
def users(root=".", gone=()):
|
||||
"""Map each role to the roles whose files name it as mortis.kamino.<role>.
|
||||
|
||||
Self-references (a scenario converging its own role) are left out, and so are names that
|
||||
are neither a role directory nor in `gone`: the changed roles, so that a role deleted or
|
||||
renamed by the pull request still selects the roles that call it.
|
||||
"""
|
||||
base = os.path.join(root, "roles")
|
||||
if not os.path.isdir(base):
|
||||
return {}
|
||||
roles = {r for r in os.listdir(base) if os.path.isdir(os.path.join(base, r))}
|
||||
known = roles | set(gone)
|
||||
used_by = {}
|
||||
for role in roles:
|
||||
for dirpath, _dirs, files in os.walk(os.path.join(base, role)):
|
||||
for name in files:
|
||||
try:
|
||||
with open(os.path.join(dirpath, name), "rb") as f:
|
||||
text = f.read().decode("utf-8", "ignore")
|
||||
except OSError:
|
||||
continue
|
||||
for other in ROLE_REFERENCE.findall(text):
|
||||
if other != role and other in known:
|
||||
used_by.setdefault(other, set()).add(role)
|
||||
return used_by
|
||||
|
||||
|
||||
def with_users(roles, used_by):
|
||||
"""`roles` plus every role that uses one of them, directly or through another."""
|
||||
found = set(roles)
|
||||
todo = list(roles)
|
||||
while todo:
|
||||
for user in used_by.get(todo.pop(), ()):
|
||||
if user not in found:
|
||||
found.add(user)
|
||||
todo.append(user)
|
||||
return found
|
||||
|
||||
|
||||
def is_ignored(path):
|
||||
return any(fnmatch.fnmatchcase(path, pattern) for pattern in IGNORED)
|
||||
|
||||
@@ -141,6 +190,8 @@ def select(all_scenarios, changed, root="."):
|
||||
roles.add(parts[1])
|
||||
continue
|
||||
return list(all_scenarios), f"{printable(path)} can affect every scenario"
|
||||
changed_roles = roles
|
||||
roles = with_users(changed_roles, users(root, changed_roles))
|
||||
chosen = [
|
||||
s for s in all_scenarios if s.startswith("roles/") and s.split("/")[1] in roles
|
||||
]
|
||||
@@ -150,11 +201,17 @@ def select(all_scenarios, changed, root="."):
|
||||
)
|
||||
if bare:
|
||||
raise ValueError(
|
||||
"changed role without a molecule scenario: "
|
||||
"role without a molecule scenario (changed, or a user of a changed role): "
|
||||
+ ", ".join(printable(r) for r in bare)
|
||||
)
|
||||
if roles:
|
||||
reason = "only roles changed: " + ", ".join(printable(r) for r in sorted(roles))
|
||||
reason = "only roles changed: " + ", ".join(
|
||||
printable(r) for r in sorted(changed_roles)
|
||||
)
|
||||
if roles - changed_roles:
|
||||
reason += "; their users: " + ", ".join(
|
||||
printable(r) for r in sorted(roles - changed_roles)
|
||||
)
|
||||
else:
|
||||
reason = "no change that a scenario reads"
|
||||
return chosen, reason
|
||||
|
||||
Reference in new issue
Block a user