Repository navigation
Attest hosted Mergify candidates against the trusted workflow base #1853
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 |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| from collections.abc import Callable, Iterator | ||
| from pathlib import Path | ||
| from typing import Any, BinaryIO | ||
| from urllib.parse import quote | ||
|
|
||
| MERGIFY_APP_ID = 10562 | ||
| MERGIFY_APP_OWNER = "Mergifyio" | ||
|
|
@@ -172,6 +173,7 @@ def _repo_identity(repo: JsonObject) -> tuple[object, object]: | |
| def _candidate_snapshot( | ||
| candidate: JsonObject, number: int, event_head: JsonObject, repository: JsonObject, | ||
| expected_identity: tuple[object, object, object, object], | ||
| base_branch: str, | ||
| ) -> None: | ||
| _require(candidate.get("number") == number, "GitHub returned the wrong candidate PR") | ||
| state = candidate.get("state") | ||
|
|
@@ -184,7 +186,7 @@ def _candidate_snapshot( | |
| _require(isinstance(head, dict) and isinstance(base, dict), "Candidate refs are missing") | ||
| _require(head.get("sha") == event_head.get("sha"), "Candidate head changed after the workflow event") | ||
| _require(head.get("ref") == event_head.get("ref"), "Mergify candidate has an unexpected head ref") | ||
| _require(base.get("ref") == "main", "Mergify candidate must target the real main branch") | ||
| _require(base.get("ref") == base_branch, f"Mergify candidate must target the real {base_branch} branch") | ||
| expected_repo = _repo_identity(repository) | ||
| for label, repo in (("head", head.get("repo")), ("base", base.get("repo"))): | ||
| _require(isinstance(repo, dict) and _repo_identity(repo) == expected_repo, | ||
|
|
@@ -225,6 +227,7 @@ def _attest_source( | |
| repository: JsonObject, | ||
| candidate_sha: str, | ||
| github_get: GitHubGet, | ||
| base_branch: str, | ||
| ) -> str: | ||
| _require(source.get("number") == number, f"GitHub returned the wrong source PR for #{number}") | ||
| _require( | ||
|
|
@@ -240,10 +243,10 @@ def _attest_source( | |
| ) | ||
| base_repo = base.get("repo") | ||
| _require( | ||
| base.get("ref") == "main" | ||
| base.get("ref") == base_branch | ||
| and isinstance(base_repo, dict) | ||
| and _repo_identity(base_repo) == _repo_identity(repository), | ||
| f"source PR #{number} does not target this repository's main branch", | ||
| f"source PR #{number} does not target this repository's {base_branch} branch", | ||
| ) | ||
| source_sha = head.get("sha") | ||
| _require( | ||
|
|
@@ -391,6 +394,7 @@ def _verify_tree(repo: str, main: str, candidate: str, sources: list[str]) -> No | |
| def attest_candidate( | ||
| event: JsonObject, | ||
| *, | ||
| base_branch: str = "main", | ||
| github_get: GitHubGet, | ||
| mergify_status: MergifyStatus, | ||
| source_policy: SourcePolicy, | ||
|
|
@@ -405,7 +409,7 @@ def attest_candidate( | |
| for attempt in range(_ATTEST_ATTEMPTS): | ||
| try: | ||
| return _attest_candidate_once( | ||
| event, | ||
| event, base_branch, | ||
| github_get=github_get, | ||
| mergify_status=mergify_status, | ||
| source_policy=source_policy, | ||
|
|
@@ -423,7 +427,7 @@ def attest_candidate( | |
|
|
||
|
|
||
| def _attest_candidate_once( | ||
| event: JsonObject, | ||
| event: JsonObject, base_branch: str, | ||
| *, | ||
| github_get: GitHubGet, | ||
| mergify_status: MergifyStatus, | ||
|
|
@@ -481,15 +485,15 @@ def _attest_candidate_once( | |
| candidate_path = f"/repos/{full_name}/pulls/{candidate_number}" | ||
| def recheck_candidate(message: str = "GitHub candidate lookup returned an invalid payload") -> None: | ||
| candidate = _object(github_get(candidate_path), message) | ||
| _candidate_snapshot(candidate, candidate_number, event_head, repository, expected_identity) | ||
| _candidate_snapshot(candidate, candidate_number, event_head, repository, expected_identity, base_branch) | ||
|
|
||
| recheck_candidate() | ||
|
|
||
| def live_lineage() -> tuple[str, list[int], list[str]]: | ||
| delay = 2 | ||
| while True: | ||
| try: | ||
| return _candidate_lineage(mergify_status(owner, repo, "main"), candidate_number) | ||
| return _candidate_lineage(mergify_status(owner, repo, base_branch), candidate_number) | ||
| except CandidateBatchError: | ||
| recheck_candidate("Invalid candidate lookup") | ||
| remaining = batch_deadline - _retry_clock() | ||
|
|
@@ -504,8 +508,8 @@ def live_lineage() -> tuple[str, list[int], list[str]]: | |
| delay = min(delay * 2, 30) | ||
|
|
||
| first_lineage = live_lineage() | ||
| main_path = f"/repos/{full_name}/git/ref/heads/main" | ||
| main_before = github_get(main_path) if first_lineage[2] else None | ||
| base_path = f"/repos/{full_name}/git/ref/heads/{quote(base_branch)}" | ||
|
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: Renaming the The net source change is about +5 lines, which fits the budget. But renaming |
||
| base_before = github_get(base_path) if first_lineage[2] else None | ||
| source_snapshots: dict[int, tuple[str, tuple[object, ...]]] = {} | ||
| for number in first_lineage[1]: | ||
| source = _lookup_source(github_get, full_name, number) | ||
|
|
@@ -514,7 +518,7 @@ def live_lineage() -> tuple[str, list[int], list[str]]: | |
| number=number, | ||
| repository=repository, | ||
| candidate_sha=candidate_sha, | ||
| github_get=github_get, | ||
| github_get=github_get, base_branch=base_branch, | ||
| ) | ||
| source_snapshots[number] = (source_sha, _source_fingerprint(source)) | ||
| try: | ||
|
|
@@ -531,14 +535,14 @@ def live_lineage() -> tuple[str, list[int], list[str]]: | |
|
|
||
| if first_lineage[2]: | ||
| try: | ||
| main_ref = _object(main_before, "main ref unavailable") | ||
| main_sha = _object(main_ref.get("object"), "main object unavailable").get("sha") | ||
| _require(isinstance(main_sha, str) and _SHA.fullmatch(main_sha) is not None, "invalid main head") | ||
| _verify_tree(full_name, main_sha, candidate_sha, | ||
| base_ref = _object(base_before, f"{base_branch} ref unavailable") | ||
| base_sha = _object(base_ref.get("object"), f"{base_branch} object unavailable").get("sha") | ||
| _require(isinstance(base_sha, str) and _SHA.fullmatch(base_sha) is not None, f"invalid {base_branch} head") | ||
| _verify_tree(full_name, base_sha, candidate_sha, | ||
| [snapshot[0] for snapshot in source_snapshots.values()]) | ||
| _require( | ||
| github_get(main_path) == main_before, | ||
| "main changed during attestation", | ||
| github_get(base_path) == base_before, | ||
| f"{base_branch} changed during attestation", | ||
| CandidateRaceError, | ||
| ) | ||
| except CandidateRaceError: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -264,7 +264,7 @@ def isolated_fetch(argv, **kwargs): | |
| def attest( | ||
| *, head=candidate_sha, main_head=main, policy=lambda _n, _s: None, | ||
| main_after=None, extra_source=None, lineage="vanished", run=attest_candidate, | ||
| held_source=None, | ||
| held_source=None, base_branch="main", | ||
| ): | ||
| candidate_event = event(head) | ||
| values = payloads(candidate_event) | ||
|
|
@@ -286,23 +286,29 @@ def attest( | |
| ) | ||
| for endpoint in ("commits", "files"): | ||
| values[f"/repos/owner/repo/pulls/{number}/{endpoint}?per_page=100&page=1"] = [] | ||
| if base_branch != "main": | ||
| for number in (102, 900): | ||
| values[f"/repos/owner/repo/pulls/{number}"]["base"]["ref"] = base_branch | ||
| reads = cycle([main_head, main_after or main_head]) | ||
|
|
||
| def get(path): | ||
| if not path.endswith("/git/ref/heads/main"): | ||
| if not path.endswith(f"/git/ref/heads/{base_branch}"): | ||
| return deepcopy(values[path]) | ||
| return {"object": {"sha": sha}} if (sha := next(reads)) else {} | ||
|
|
||
| if base_branch != "main": | ||
|
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: The candidate-level trunk test doesn't check which branch the Mergify status is read for
|
||
| run = partial(run, base_branch=base_branch) | ||
| return run_attestation(candidate_event, github_get=get, status_reads=[queue], | ||
| source_policy=policy, run=run) | ||
|
|
||
| return attest, git, commit, base, parent, source, candidate_sha | ||
|
|
||
|
|
||
| def test_vanished_squash_parent_passes_real_tree_attestation(tree_candidate): | ||
| @pytest.mark.parametrize("base_branch", ["main", "trunk"]) | ||
| def test_vanished_squash_parent_passes_real_tree_attestation(tree_candidate, base_branch): | ||
| attest, _, _, _, _, source, _ = tree_candidate | ||
| seen = [] | ||
| assert attest(policy=lambda n, _: seen.append(n)) == expected_sources((102, source)) | ||
| assert attest(base_branch=base_branch, policy=lambda n, _: seen.append(n)) == expected_sources((102, source)) | ||
| assert seen == [102] | ||
|
|
||
|
|
||
|
|
||
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: Default
base_branch="main"lets future callers silently fall back to mainhosted._runis the only production caller, and it passescfg.base_branch. Still, the"main"default onattest_candidatemeans any new caller that forgets the argument will quietly attest againstmaininstead of failing. That cuts against the issue's aim of always using the trusted workflow's base. Smallest fix: makebase_brancha required keyword argument. The tests already route throughrun_attestation/partial, so they can passbase_branch="main"explicitly; that removes theif base_branch != "main"branch in the test fixture.