Skip to content

Commit 3445861

Browse files
authored
fix(runner): push the attested SHA; reuse/open the PR in the pushed remote (#20)
Found rolling GATE-014 out to FastLED/cli (#295, CodeRabbit): - `--push` pushed `HEAD:refs/heads/<branch>`, so anything that moved HEAD between the lanes and the push shipped an unattested commit. It now pushes `<sha>:refs/heads/<branch>` and refuses if HEAD's tree is no longer the tree the lanes ran on. - `gh pr view`/`create` resolved the repository from GH_REPO or gh's default remote, not the remote prooft pushed to. Both now pass `--repo` derived from `git remote get-url <remote>` (github.com and enterprise hosts; a non-URL remote keeps gh's default). 0.3.2. Ci-Attestation: {"at":1795328550,"gate":"python/all/fmt","host":"linux-x86_64","key":"e1c84c71b20b118057d87df87e8e8f2ed88b0d3f2b8f9174b4e4680daedf3f4a","lane":"lint","parents":["627df1683e66b857022e1357ad8997a3b97018f8"],"secs":5,"stamp":"6a5d041e52f31820a68209c82d68838d","tree":"052aacef6081d2688de2dd0b9a14f3ce832efd8f","v":1,"via":"run"} Ci-Attestation: {"at":1795328550,"gate":"python/all/lint","host":"linux-x86_64","key":"e1c84c71b20b118057d87df87e8e8f2ed88b0d3f2b8f9174b4e4680daedf3f4a","lane":"lint","parents":["627df1683e66b857022e1357ad8997a3b97018f8"],"secs":5,"stamp":"9763b7ee255cd668da7f747c265bd2e0","tree":"052aacef6081d2688de2dd0b9a14f3ce832efd8f","v":1,"via":"run"} Ci-Attestation: {"at":1795328550,"gate":"python/all/types","host":"linux-x86_64","key":"e1c84c71b20b118057d87df87e8e8f2ed88b0d3f2b8f9174b4e4680daedf3f4a","lane":"lint","parents":["627df1683e66b857022e1357ad8997a3b97018f8"],"secs":5,"stamp":"60351cd98a1b685ab7df4bed9f8a988c","tree":"052aacef6081d2688de2dd0b9a14f3ce832efd8f","v":1,"via":"run"} Ci-Attestation: {"at":1795328627,"gate":"python/linux-x64/test","host":"linux-x86_64","key":"269f80b55b309b7cb9501f2247ccf564d474e5345266f3383a16fa7b224bd968","lane":"tests","parents":["627df1683e66b857022e1357ad8997a3b97018f8"],"secs":76,"stamp":"5cfbaf911f11213c268dbedafe528aad","tree":"052aacef6081d2688de2dd0b9a14f3ce832efd8f","v":1,"via":"run"}
1 parent 627df16 commit 3445861

5 files changed

Lines changed: 69 additions & 11 deletions

File tree

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "prooft"
3-
version = "0.3.1"
3+
version = "0.3.2"
44
description = "Proof marks for client builds: stamp local lint/test passes into commit trailers so CI and the merge queue skip what is already proven."
55
readme = "README.md"
66
requires-python = ">=3.10"

‎src/prooft/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from prooft.repo import LaneFailed, LaneRun, Repo, open # noqa: A004 - fluent `prooft.open(...)`
1313
from prooft.verify import JobDecision, Plan, State, Verdict, decide, verify
1414

15-
__version__ = "0.3.1"
15+
__version__ = "0.3.2"
1616

1717
__all__ = [
1818
"POLICY_FILE",

‎src/prooft/runner.py‎

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from dataclasses import dataclass
2929
import os
3030
from pathlib import Path
31+
import re
3132
import subprocess
3233
import sys
3334
import time
@@ -180,23 +181,52 @@ def _run_lane(repo: Repo, policy: Policy, step: Step) -> str:
180181
return f"{step.stage} {step.lane}: attested on {sha[:12]}"
181182

182183

183-
def _push(repo: Repo, step: Step) -> None:
184-
code = subprocess.run(list(step.argv), cwd=repo.root, check=False).returncode
184+
def _pinned_push(argv: Sequence[str], sha: str) -> list[str]:
185+
"""Push the attested commit by SHA, never whatever ``HEAD`` names at push time."""
186+
return [f"{sha}:{a[len('HEAD:') :]}" if a.startswith("HEAD:") else a for a in argv]
187+
188+
189+
def _push(repo: Repo, step: Step, tree: str) -> str:
190+
"""Push the commit the lanes attested; refuse if HEAD's bytes moved since lint."""
191+
head = repo.commit()
192+
if head.tree != tree:
193+
raise Refused("HEAD changed after the lanes ran; rerun prooft to attest it")
194+
code = subprocess.run(_pinned_push(step.argv, head.sha), cwd=repo.root, check=False).returncode
185195
if code != 0:
186196
raise LaneFailed("push", code)
197+
return head.sha
198+
199+
200+
def gh_repo(url: str) -> str | None:
201+
"""``OWNER/REPO`` (``HOST/OWNER/REPO`` off github.com) from a remote URL, else None."""
202+
m = re.match(r"^(?:[a-z+]+://)?(?:[^@/]+@)?([^/:]+)[:/]([^/]+)/([^/]+?)(?:\.git)?/?$", url.strip())
203+
if not m:
204+
return None
205+
host, owner, name = m.groups()
206+
return f"{owner}/{name}" if host == "github.com" else f"{host}/{owner}/{name}"
207+
208+
209+
def _remote_repo(repo: Repo, remote: str) -> list[str]:
210+
try:
211+
found = gh_repo(repo.git.run("remote", "get-url", remote))
212+
except GitError:
213+
return []
214+
return ["--repo", found] if found else []
187215

188216

189-
def _pr(repo: Repo, step: Step, branch: str) -> str:
217+
def _pr(repo: Repo, step: Step, branch: str, remote: str) -> str:
218+
"""Reuse or open the PR in the repository the branch was pushed to, not ``GH_REPO``'s."""
219+
where = _remote_repo(repo, remote)
190220
found = subprocess.run(
191-
["gh", "pr", "view", branch, "--json", "url", "-q", ".url"],
221+
["gh", "pr", "view", branch, *where, "--json", "url", "-q", ".url"],
192222
cwd=repo.root,
193223
capture_output=True,
194224
text=True,
195225
check=False,
196226
)
197227
if found.returncode == 0 and found.stdout.strip():
198228
return f"pr: {found.stdout.strip()} (existing)"
199-
code = subprocess.run(list(step.argv), cwd=repo.root, check=False).returncode
229+
code = subprocess.run([*step.argv, *where], cwd=repo.root, check=False).returncode
200230
if code != 0:
201231
raise LaneFailed("pr", code)
202232
return "pr: opened"
@@ -224,10 +254,10 @@ def say(line: str) -> None:
224254
last_lint = max((i for i, s in enumerate(steps) if s.stage == "lint"), default=-1)
225255
for i, step in enumerate(steps):
226256
if step.stage == "push":
227-
_push(repo, step)
228-
say(f"pushed {branch}")
257+
sha = _push(repo, step, tree)
258+
say(f"pushed {sha[:12]} to {branch}")
229259
elif step.stage == "pr":
230-
say(_pr(repo, step, str(branch)))
260+
say(_pr(repo, step, str(branch), opts.remote))
231261
else:
232262
say(_run_lane(repo, policy, step))
233263
if i == last_lint:

‎tests/test_runner.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,31 @@ def test_a_lane_that_commits_new_content_is_refused(repo: Repo) -> None:
265265
_amend_policy(repo, "content")
266266
with pytest.raises(RuntimeError, match="changed HEAD"):
267267
go(repo)
268+
269+
270+
# ── push pins the attested commit; the PR lives where the branch went ────
271+
272+
273+
def test_push_names_the_attested_sha_not_head() -> None:
274+
argv = ("git", "push", "--force-with-lease", "-u", "origin", "HEAD:refs/heads/feature")
275+
assert runner._pinned_push(argv, "abc123")[-1] == "abc123:refs/heads/feature" # pyright: ignore[reportPrivateUsage]
276+
277+
278+
def test_push_refuses_when_head_bytes_moved_after_the_lanes(repo: Repo) -> None:
279+
step = runner.Step("push", "", ("git", "push", "origin", "HEAD:refs/heads/feature"))
280+
with pytest.raises(runner.Refused, match="HEAD changed"):
281+
runner._push(repo, step, "0" * 40) # pyright: ignore[reportPrivateUsage]
282+
283+
284+
@pytest.mark.parametrize(
285+
("url", "want"),
286+
[
287+
("https://github.com/forge-ci/prooft.git", "forge-ci/prooft"),
288+
("[email protected]:FastLED/cli.git", "FastLED/cli"),
289+
("ssh://[email protected]/FastLED/cli", "FastLED/cli"),
290+
("https://ghe.example.com/team/tool.git", "ghe.example.com/team/tool"),
291+
("/srv/git/bare.git", None),
292+
],
293+
)
294+
def test_gh_repo_reads_the_pushed_remote(url: str, want: str | None) -> None:
295+
assert runner.gh_repo(url) == want

‎uv.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)