Repository navigation
Refresh trusted review publishers before readiness #1733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,12 +108,21 @@ 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, *, refresh: bool) -> tuple[Config, str]: | ||
| source = f"origin/{branch}:workflow.toml" | ||
| hint = "Fetch the trusted base workflow, or supply --config for bootstrap or an override." | ||
| ref = f"refs/remotes/origin/{branch}" | ||
| if refresh: | ||
| fetched = _proc.run( | ||
| ["git", "fetch", "--quiet", "--no-tags", "--no-recurse-submodules", | ||
| "--no-write-fetch-head", "origin", f"+refs/heads/{branch}:{ref}"], | ||
| cwd=worktree, env_allowlist=worktree_mod.GIT_ENV, timeout=60, | ||
| ) | ||
| if fetched.exit_code != 0: | ||
| raise ConfigError(f"could not refresh origin/{branch}. {hint}") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Refresh failure drops git's diagnostic If |
||
| directory_args = ["--path-format=absolute", "--git-common-dir"] if previous is not None else [] | ||
| resolved = _proc.run( | ||
| ["git", "rev-parse", *directory_args, "--verify", f"refs/remotes/origin/{branch}^{{commit}}"], | ||
| ["git", "rev-parse", *directory_args, "--verify", f"{ref}^{{commit}}"], | ||
| cwd=worktree, env_allowlist=worktree_mod.GIT_ENV, timeout=60, | ||
| ) | ||
| if resolved.exit_code != 0: | ||
|
|
@@ -140,7 +149,7 @@ def _remote_workflow(worktree: Path, branch: str, previous: Config | None) -> tu | |
| raise ConfigError(f"{source}: {exc}. {hint}") from None | ||
|
|
||
|
|
||
| def load_trusted(worktree: Path, *, previous: Config | None = None) -> Config: | ||
| def load_trusted(worktree: Path, *, previous: Config | None = None, refresh: bool = False) -> Config: | ||
| """Select delivery policy using Git's origin default and its trusted workflow.""" | ||
| prefix = "refs/remotes/origin/" | ||
| default = _proc.run( | ||
|
|
@@ -154,10 +163,10 @@ 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, refresh=refresh) | ||
| 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, refresh=refresh) | ||
| 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) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1445,13 +1445,16 @@ def test_review_runtime_paths_use_only_verified_base( | |
| def test_runtime_pinning_requires_trusted_workflow( | ||
| wt: Path, gh: FakeGh, fake_bin: Path, capsys, command: str, missing: str, | ||
| ) -> None: | ||
| origin = Path(git(wt, "remote", "get-url", "origin").strip()) | ||
| if missing == "default": | ||
| git(wt, "symbolic-ref", "--delete", "refs/remotes/origin/HEAD") | ||
| elif missing == "remote": | ||
| git(wt, "update-ref", "-d", "refs/remotes/origin/main") | ||
| git(origin, "update-ref", "-d", "refs/heads/main") | ||
| else: | ||
| git(wt, "update-ref", "refs/remotes/origin/main", | ||
| git(wt, "rev-list", "--max-parents=0", "HEAD").strip()) | ||
| initial = git(wt, "rev-list", "--max-parents=0", "HEAD").strip() | ||
| git(wt, "update-ref", "refs/remotes/origin/main", initial) | ||
| git(origin, "update-ref", "refs/heads/main", initial) | ||
| prepare_review(wt, gh, fake_bin) | ||
| code, out, err = run(capsys, command) | ||
| assert "reviewer paths" not in out + err | ||
|
|
@@ -2151,7 +2154,7 @@ def test_ready_after_reviewed_head( | |
| popen = subprocess.Popen | ||
|
|
||
| def isolated_fetch(argv, **kwargs): | ||
| if "fetch" in argv: | ||
| if "fetch" in argv and f"https://github.com/{REPO}.git" in argv: | ||
| if history == "missing history": | ||
| raise cli.eligibility.candidate.CandidateError("history unavailable") | ||
| argv = [wt.as_uri() if arg == f"https://github.com/{REPO}.git" else arg for arg in argv] | ||
|
|
@@ -2192,7 +2195,7 @@ def test_ready_marks_draft_ready(wt: Path, gh: FakeGh, capsys, head, branch, git | |
| gh.respond("pr ready", "") | ||
| git_calls.clear() | ||
| code, out, err = run(capsys, "ready") | ||
| assert_git_cost(git_calls, 7, 1) | ||
| assert_git_cost(git_calls, 8, 1) | ||
| assert (code, err) == (0, "") and out.endswith(f"ready: {URL}\n") | ||
| assert ["pr", "ready", "7", "--repo", REPO] in [c["argv"] for c in gh.calls] | ||
|
|
||
|
|
@@ -2467,7 +2470,7 @@ def after_sleep(): | |
|
|
||
| @pytest.mark.parametrize("command", ["ready", "merge"]) | ||
| @pytest.mark.parametrize("settled", ["CONFLICTING", "MERGEABLE", "UNKNOWN"]) | ||
| def test_wait_for_github_mergeability(wt, gh, capsys, monkeypatch, command, settled): | ||
| def test_wait_for_github_mergeability(wt, gh, capsys, monkeypatch, command, settled, git_calls): | ||
| head = commit_mergify_configuration(wt) | ||
| arm_approved_ready_pr(gh, head, wt, mergeable="UNKNOWN", isDraft=False) | ||
| gh.respond_pr_view(pr_json(headRefOid=head, mergeable=settled, isDraft=False, | ||
|
|
@@ -2478,18 +2481,69 @@ def test_wait_for_github_mergeability(wt, gh, capsys, monkeypatch, command, sett | |
| monkeypatch.setattr(cli, "_wait_for_mergify", lambda *_: None) | ||
|
|
||
| poll_clock(monkeypatch) | ||
| git_calls.clear() | ||
| code, out, err = run(capsys, command, "--wait=1") | ||
|
|
||
| assert sum(call[0] == "fetch" for call in git_calls) == 1 | ||
| if settled == "CONFLICTING": | ||
| assert code == 1 and "has merge conflicts" in out + err | ||
| elif settled == "UNKNOWN": | ||
| assert code == 3 and out.endswith(f"timed out waiting for GitHub to compute mergeability on {head[:12]}. Run `loopzero {command} --wait` to resume on this head.\n") | ||
| assert code == 3 and out.endswith(f"timed out waiting for GitHub to compute mergeability on {head[:12]}. Run `loopzero {command} --wait` to resume on this head.\n"), (code, out, err) | ||
| else: | ||
| assert code == (0 if command == "ready" else 3) | ||
| assert requests == ([(REPO, 7, head, "main")] | ||
| if command == "merge" and settled == "MERGEABLE" else []) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("publisher,expected", [ | ||
| ("retired-publisher", "pending"), ("replacement-publisher", "success"), | ||
| ]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Publisher rotation test only covers Both commands refresh, but the rotation test only runs |
||
| def test_readiness_and_hosted_use_current_base_publishers(wt, gh, head, tmp_path, monkeypatch, capsys, | ||
| publisher, expected): | ||
| git(tmp_path, "clone", "-q", str(tmp_path / "origin.git"), "trusted") | ||
| trusted = tmp_path / "trusted" | ||
| workflow = trusted / "workflow.toml" | ||
| retired_workflow = (WORKFLOW.replace('required_ci = ["checks"]', | ||
| 'required_ci = ["checks", "Loop-zero Eligibility"]') | ||
| + 'review_publishers = ["retired-publisher"]\n') | ||
| workflow.write_text(retired_workflow) | ||
| git(trusted, "commit", "-qam", "trust old review publisher") | ||
| git(trusted, "push", "-q", "origin", "main") | ||
| git(wt, "fetch", "-q", "origin", "main") | ||
| stale = git(wt, "rev-parse", "origin/main") | ||
|
|
||
| arm_approved_pr_with_check_result(gh, head, wt, "success", isDraft=False, review_login=publisher) | ||
| gh.respond(f"api repos/{REPO}/commits/{head}/statuses", | ||
| [{"context": "Loop-zero Eligibility", "state": "success"}]) | ||
| workflow.write_text(retired_workflow.replace("retired-publisher", "replacement-publisher")) | ||
| git(trusted, "commit", "-qam", "rotate review publisher") | ||
| git(trusted, "push", "-q", "origin", "main") | ||
| assert git(wt, "rev-parse", "origin/main") == stale | ||
| assert git(trusted, "rev-parse", "HEAD") != stale | ||
|
|
||
| code, out, _ = run(capsys, "ready") | ||
| assert code == (0 if expected == "success" else 1) | ||
| assert (github.MISSING_REVIEW_REASON in out) == (expected == "pending") | ||
| if expected == "pending": | ||
| git(wt, "remote", "set-url", "origin", str(tmp_path / "unavailable.git")) | ||
| code, _, err = run(capsys, "ready") | ||
| assert code == 1 and "could not refresh origin/main" in err | ||
| explicit = tmp_path / "explicit.toml" | ||
| explicit.write_text(retired_workflow) | ||
| assert run(capsys, "--config", str(explicit), "ready")[0] == 0 | ||
|
|
||
| event = trusted / "event.json" | ||
| event.write_text(json.dumps({"inputs": {"pr": "7"}})) | ||
| monkeypatch.chdir(trusted) | ||
| monkeypatch.setenv("GITHUB_REPOSITORY", REPO) | ||
| monkeypatch.setenv("GITHUB_EVENT_PATH", str(event)) | ||
| gh.respond(f"api repos/{REPO}/statuses/{head}", {}) | ||
| assert hosted.main() == 0 | ||
| payload = json.loads(gh.calls[-1]["--input"]) | ||
| assert gh.calls[-1]["argv"][1] == f"repos/{REPO}/statuses/{head}" | ||
| assert payload["state"] == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("command,check,hold_at_admission", [ | ||
| ("ready", "success", False), ("ready", "skipped", False), | ||
| ("merge", "success", False), ("merge", "success", True), | ||
|
|
@@ -3116,6 +3170,7 @@ def test_merged_bare_primary_cleans_only_clean_linked_worktree( | |
| repo: Path, gh: FakeGh, capsys, monkeypatch, state | ||
| ) -> None: | ||
| primary = repo.parent / "origin.git" | ||
| git(primary, "remote", "add", "origin", str(primary)) | ||
| linked = repo.parent / "linked task" | ||
| git(primary, "worktree", "add", "-q", "-b", "lz/bare", str(linked), "main") | ||
| monkeypatch.chdir(linked) | ||
|
|
@@ -3645,7 +3700,7 @@ def test_commands_refuse_base_branch(wt: Path, gh: FakeGh, capsys, base) -> None | |
| git(wt, "checkout", "-q", "--ignore-other-worktrees", base) | ||
| else: | ||
| publish_workflow(wt, workflow.read_text()) | ||
| git(wt, "update-ref", f"refs/remotes/origin/{base}", "refs/remotes/origin/main") | ||
| git(wt, "push", "-q", "origin", f"origin/main:refs/heads/{base}") | ||
| git(wt, "checkout", "-q", "-b", base) | ||
| for command in ("pr", "review", "ready", "merge"): | ||
| code, out, err = run(capsys, command) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion:
merge --cancelnow needs network access to origin_contextnow refreshes for everymergeinvocation, includingmerge --cancel.--cancelonly usesconfig.repoandmerge_strategyto dequeue from Mergify. As a result, an unreachable origin, or credentials that can't fetch, now block cancelling a queued PR, even though the publisher policy plays no part in a dequeue. If that wasn't intended, skip the refresh whenargs.cancelis set, e.g.refresh=args.command == "ready" or (args.command == "merge" and not args.cancel).