From e47815b56f9ff6f4be9261284f9f55d41c952aa1 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 10:52:43 +0100 Subject: [PATCH] Require ready and merge workflows to declare required CI Architecture-Review: unchanged; the existing workflow key still supplies readiness policy, and an explicit empty list denotes no required contexts. Co-Authored-By: Codex --- src/loopzero/cli.py | 5 +++-- src/loopzero/config.py | 33 +++++++++++++++++++++++++-------- tests/test_cli.py | 21 +++++++++++++++++++++ 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index f545068d..ada2286c 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -131,10 +131,11 @@ def _load_config( path = args.config or root / "workflow.toml" if not pin_checks: return config_mod.load(path) + require_ci = args.command in {"ready", "merge"} and not getattr(args, "cancel", False) if args.config: print("checks are unpinned because --config was provided") - return config_mod.load(path) - config = config_mod.load_trusted(root, previous=previous) + return config_mod.load(path, require_ci=require_ci) + config = config_mod.load_trusted(root, previous=previous, require_ci=require_ci) if args.command == "check": if not config.checks: raise config_mod.ConfigError( diff --git a/src/loopzero/config.py b/src/loopzero/config.py index 81388cfa..abd6e0f1 100644 --- a/src/loopzero/config.py +++ b/src/loopzero/config.py @@ -52,7 +52,7 @@ class ConfigError(LoopZeroError): """The configuration file is missing, unreadable, or has invalid content.""" -def load(path: Path | str) -> Config: +def load(path: Path | str, *, require_ci: bool = False) -> Config: """Load and validate a workflow file.""" path = Path(path) try: @@ -70,7 +70,10 @@ def load(path: Path | str) -> Config: except tomllib.TOMLDecodeError as exc: raise ConfigError(f"{path}: invalid TOML: {exc}") from exc try: - return _build(data) + policy = _build(data) + if require_ci: + _require_ci(data.get("checks", {})) + return policy except ConfigError as exc: raise ConfigError(f"{path}: {exc}") from None @@ -108,7 +111,9 @@ def _has_replacement_refs(common_dir: Path) -> bool: return b" refs/replace/" in packed -def _remote_workflow(worktree: Path, branch: str, previous: Config | None) -> tuple[Config, str]: +def _remote_workflow( + worktree: Path, branch: str, previous: Config | None, *, require_ci: bool, +) -> tuple[Config, str]: source = f"origin/{branch}:workflow.toml" hint = "Fetch the trusted base workflow, or supply --config for bootstrap or an override." directory_args = ["--path-format=absolute", "--git-common-dir"] if previous is not None else [] @@ -135,13 +140,18 @@ def _remote_workflow(worktree: Path, branch: str, previous: Config | None) -> tu except tomllib.TOMLDecodeError as exc: raise ConfigError(f"{source}: invalid TOML: {exc}. {hint}") from None try: - return _build(data), revision + policy = _build(data) + if require_ci and policy.base_branch == branch: + _require_ci(data.get("checks", {})) + return policy, revision except ConfigError as exc: raise ConfigError(f"{source}: {exc}. {hint}") from None -def load_trusted(worktree: Path, *, previous: Config | None = None) -> Config: - """Select delivery policy using Git's origin default and its trusted workflow.""" +def load_trusted( + worktree: Path, *, previous: Config | None = None, require_ci: bool = False, +) -> Config: + """Select the trusted origin workflow, reusing a prior load with the same validation.""" prefix = "refs/remotes/origin/" default = _proc.run( ["git", "symbolic-ref", "--quiet", f"{prefix}HEAD"], @@ -154,15 +164,22 @@ def load_trusted(worktree: Path, *, previous: Config | None = None) -> Config: "or supply --config for bootstrap or an override." ) branch = ref.removeprefix(prefix) - policy, revision = _remote_workflow(worktree, branch, previous) + policy, revision = _remote_workflow(worktree, branch, previous, require_ci=require_ci) if policy.base_branch == branch: return replace(policy, trusted_base_revision=revision) - base, revision = _remote_workflow(worktree, policy.base_branch, previous) + base, revision = _remote_workflow(worktree, policy.base_branch, previous, require_ci=require_ci) if base.base_branch != policy.base_branch: raise ConfigError("trusted origin workflows disagree on repo.base; align them or supply --config") return replace(base, trusted_base_revision=revision) +def _require_ci(checks: dict[str, Any]) -> None: + if "required_ci" not in checks: + raise ConfigError( + "checks.required_ci must list the required contexts or explicitly set required_ci = [] if none are required" + ) + + def _build(data: dict[str, Any]) -> Config: _reject_unknown("top level", data, _SECTIONS) for section, keys in _SECTIONS.items(): diff --git a/tests/test_cli.py b/tests/test_cli.py index da4e686d..4616ac35 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2363,6 +2363,27 @@ def test_ready_not_ready_lists_reasons(wt: Path, gh: FakeGh, capsys, head) -> No assert not any("actions/runs" in " ".join(c["argv"]) for c in gh.calls) +@pytest.mark.parametrize("command,required,code", [ + ("ready", "", 1), ("merge", "", 1), + ("ready", "required_ci = []", 0), ("ready", 'required_ci = ["checks"]', 1), +]) +def test_delivery_requires_explicit_ci_policy(wt, gh, capsys, head, command, required, code): + if not required: + arm_approved_pr_with_check_result(gh, head, wt, "failure", isDraft=False) + publish_workflow(wt, WORKFLOW.replace('required_ci = ["checks"]', required) + "\n") + if required: + arm_approved_pr_with_check_result(gh, head, wt, "failure", isDraft=False) + actual, out, err = run(capsys, command) + assert actual == code, (out, err) + if not required: + assert "origin/main:workflow.toml: checks.required_ci must list the required contexts" in err + assert "required_ci = []" in err and not gh.calls + elif code: + assert "required check 'checks' is failure" in out + else: + assert not any("/check-runs" in " ".join(call["argv"]) for call in gh.calls) + + @pytest.mark.parametrize("retargeted,reason", [ (True, "PR targets release, configured base is main"), (False, f"required check 'checks' is failure{FAILED_CHECK_HINT}"),