Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/loopzero/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The error message says what's wrong but not what to do next

The acceptance criteria ask for the workflow source, the setting and an operator instruction. The message gives the source and the setting. Its only instruction is 'must list the intended publisher logins', which doesn't say where to make the change. Consider adding a concrete next step, such as adding review_publishers = ["<login>"] under [delivery] and merging that into the base branch. That matches the more actionable hints in config.py (e.g. lines 113 and 162).

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(
Expand Down
37 changes: 28 additions & 9 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -68,6 +69,7 @@
[delivery]
merge = "squash"
reviewers = ["claude", "codex"]
{PUBLISHERS}
"""


Expand Down Expand Up @@ -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]"}}})
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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}"),
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
)
Expand Down
Loading