diff --git a/src/loopzero/cli.py b/src/loopzero/cli.py index cd841b4d..15c2b0ab 100644 --- a/src/loopzero/cli.py +++ b/src/loopzero/cli.py @@ -136,6 +136,12 @@ def _load_config( print("checks are unpinned because --config was provided") return config_mod.load(path) config = config_mod.load_trusted(root, previous=previous, refresh=refresh) + if (args.command in {"ready", "merge"} and not getattr(args, "cancel", False) + and not config.review_publishers): + raise config_mod.ConfigError( + f"origin/{config.base_branch}:workflow.toml: delivery.review_publishers must list " + "the intended publisher logins" + ) if args.command == "check": if not config.checks: raise config_mod.ConfigError( diff --git a/tests/test_cli.py b/tests/test_cli.py index b016c7ff..f3c864f9 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -56,6 +56,7 @@ LOOKUP_CALLS = [BRANCH_READ, PR_LIST_READ, ["api", f"repos/{REPO}/pulls/7"]] HISTORY_CALLS = [BRANCH_READ, branch_history_key().split()] LOGIN = "lz-bot" +PUBLISHERS = f'review_publishers = ["{LOGIN}"]' WORKFLOW = f""" [repo] name = "{REPO}" @@ -68,6 +69,7 @@ [delivery] merge = "squash" reviewers = ["claude", "codex"] +{PUBLISHERS} """ @@ -1969,8 +1971,8 @@ def test_review_publisher_check_uses_app_identity( publisher: str, allowed: bool, ) -> None: app_token_helper() - (wt / "workflow.toml").write_text(WORKFLOW + f'\nreview_publishers = ["{publisher}"]\n') - git(wt, "commit", "-qam", "configure publisher") + (wt / "workflow.toml").write_text(WORKFLOW.replace(PUBLISHERS, f'review_publishers = ["{publisher}"]')) + git(wt, "commit", "--allow-empty", "-qam", "configure publisher") git(wt, "update-ref", "refs/remotes/origin/main", "HEAD") prepare_review(wt, gh, fake_bin, post_id=3) gh.respond("api graphql viewer", {"data": {"viewer": {"login": "app[bot]"}}}) @@ -1990,7 +1992,7 @@ def test_review_reminted_app_token_keeps_publisher( boot, run_review = [1_000_000.0], cli._run_review monkeypatch.setattr(github, "_app_token_clock", lambda: boot[0]) monkeypatch.setattr(cli, "_run_review", lambda *a, **k: (run_review(*a, **k), boot.append(boot.pop() + 3601))[0]) - (wt / "workflow.toml").write_text(WORKFLOW + '\nreview_publishers = ["app[bot]"]\n') + (wt / "workflow.toml").write_text(WORKFLOW.replace(PUBLISHERS, 'review_publishers = ["app[bot]"]')) git(wt, "commit", "-qam", "configure publisher") git(wt, "update-ref", "refs/remotes/origin/main", "HEAD") head = prepare_review(wt, gh, fake_bin, post_id=3) @@ -2010,7 +2012,7 @@ def test_review_posts_as_checked_identity(wt, gh, fake_bin, monkeypatch, capsys, monkeypatch.setattr(github, "app_token", lambda: tokens[0]) # flips after the publisher check monkeypatch.setattr(github, "login", lambda token=None, f=github.login: ( f(token), len(tokens) > 1 and tokens.pop(0))[0]) - (wt / "workflow.toml").write_text(WORKFLOW + f'\nreview_publishers = ["{publisher}"]\n') + (wt / "workflow.toml").write_text(WORKFLOW.replace(PUBLISHERS, f'review_publishers = ["{publisher}"]')) git(wt, "commit", "-qam", "configure publisher") git(wt, "update-ref", "refs/remotes/origin/main", "HEAD") prepare_review(wt, gh, fake_bin, post_id=3) @@ -2026,7 +2028,7 @@ def test_review_posts_as_checked_identity(wt, gh, fake_bin, monkeypatch, capsys, @pytest.mark.parametrize("publisher", [LOGIN, "OTHER"]) def test_review_refuses_same_head_twice(wt: Path, gh: FakeGh, capsys, publisher) -> None: - commit_file(wt, "workflow.toml", WORKFLOW + '\nreview_publishers = ["lz-bot", "other"]\n', "configure trusted publishers") + commit_file(wt, "workflow.toml", WORKFLOW.replace(PUBLISHERS, 'review_publishers = ["lz-bot", "other"]'), "configure trusted publishers") git(wt, "update-ref", "refs/remotes/origin/main", "HEAD") head = head_of(wt) arm_pr(gh, head) @@ -2364,6 +2366,23 @@ 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,publishers", [ + ("ready", ""), ("merge", ""), ("ready", "review_publishers = []"), + ("merge", "review_publishers = []"), ("ready", PUBLISHERS), +]) +def test_delivery_requires_trusted_review_publishers(wt, gh, capsys, head, command, publishers): + if publishers != PUBLISHERS: + arm_approved_ready_pr(gh, head, wt, isDraft=False) + publish_workflow(wt, WORKFLOW.replace(PUBLISHERS, publishers) + "\n") + if publishers == PUBLISHERS: + arm_approved_ready_pr(gh, head, wt, isDraft=False) + expect_cli(capsys, 0, command, stderr="") + else: + code, _, err = run(capsys, command) + assert code == 1 and not gh.calls + assert "origin/main:workflow.toml: delivery.review_publishers must list the intended publisher logins" in err + + @pytest.mark.parametrize("retargeted,reason", [ (True, "PR targets release, configured base is main"), (False, f"required check 'checks' is failure{FAILED_CHECK_HINT}"), @@ -2616,7 +2635,7 @@ def test_hold_blocks_ready_and_merge(wt, gh, capsys, monkeypatch, command, check def test_hold_label_refresh_matches_local_readiness(wt, gh, head, tmp_path, monkeypatch, capsys): trusted = tmp_path / "trusted" trusted.mkdir() - (trusted / "workflow.toml").write_text(WORKFLOW + f'review_publishers = ["{LOGIN}"]\n') + (trusted / "workflow.toml").write_text(WORKFLOW) event = trusted / "event.json" monkeypatch.setenv("GITHUB_REPOSITORY", REPO) monkeypatch.setenv("GITHUB_EVENT_PATH", str(event)) @@ -4236,10 +4255,10 @@ def test_hosted_eligibility_uses_publisher_not_token(wt, gh, state, verdict, exp @pytest.mark.parametrize("publisher", ["stranger", "lz-bot"]) def test_ready_pins_review_publishers_to_base(wt, repo, gh, capsys, monkeypatch, publisher, head): policy = WORKFLOW.replace('required_ci = ["checks"]', 'required_ci = ["checks", "Loop-zero Eligibility"]') - publish_workflow(wt, policy + '\nreview_publishers = ["stranger"]\n') - commit_file(repo, "workflow.toml", policy + f'\nreview_publishers = ["{LOGIN}"]\n', "rotate publisher") + publish_workflow(wt, policy.replace(PUBLISHERS, 'review_publishers = ["stranger"]')) + commit_file(repo, "workflow.toml", policy, "rotate publisher") git(repo, "push", str(repo.parent / "origin.git"), "main") # preserve the retired local publisher - (wt / "workflow.toml").write_text(policy + f'\nreview_publishers = ["{publisher}"]\n') + (wt / "workflow.toml").write_text(policy.replace(PUBLISHERS, f'review_publishers = ["{publisher}"]')) arm_approved_pr_with_check_result( gh, head, wt, "success", review_login=publisher, isDraft=False )