Repository navigation
ci(push): delegate ./push to the prooft runner (GATE-014) #295
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,214 +1,14 @@ | ||
| #!/usr/bin/env -S uv run --no-project --script | ||
| # /// script | ||
| # requires-python = ">=3.11" | ||
| # dependencies = [] | ||
| # /// | ||
| """Attest Linux x86_64 lint and unit tests, push HEAD, and open a PR. | ||
| #!/usr/bin/env python | ||
| """Publish this branch through the prooft runner (forge-ci/ci.yml GATE-014). | ||
|
|
||
| Exit codes: 0 success, 1 step failed, 2 usage, 3 refused. | ||
| Lint first, `--test` adds the test lane, then a lease push and the PR are opened or reused. | ||
| """ | ||
|
|
||
| import argparse | ||
| import json | ||
| import platform | ||
| import subprocess | ||
| import os | ||
| import sys | ||
| from typing import NamedTuple | ||
|
|
||
| from ci.prooft_verify import ROOT, prooft | ||
| from ci.prooft_verify import PROOFT_SOURCE, require_attesting_host | ||
|
|
||
| require_attesting_host() | ||
|
|
||
| class Lane(NamedTuple): | ||
| name: str | ||
| gates: set[str] | ||
| command: tuple[str, ...] | ||
|
|
||
|
|
||
| LANES = ( | ||
| Lane( | ||
| "lint", | ||
| { | ||
| "general/linux-x64/lint", | ||
| "rust/all/fmt", | ||
| "rust/x86_64-unknown-linux-gnu/clippy", | ||
| "rust/x86_64-unknown-linux-gnu/dylint", | ||
| }, | ||
| ("./lint",), | ||
| ), | ||
| Lane( | ||
| "test", | ||
| {"rust/x86_64-unknown-linux-gnu/test", "python/linux-x64/test"}, | ||
| ("uv", "run", "--no-project", "python", "ci/attest_test.py"), | ||
| ), | ||
| ) | ||
| # A worktree-local ref keeps the latest proof available across commits. | ||
| CACHE = "refs/worktree/fastled-prooft" | ||
|
|
||
|
|
||
| def git(*args: str) -> str: | ||
| return subprocess.check_output(["git", *args], cwd=ROOT, text=True).strip() | ||
|
|
||
|
|
||
| def step(argv: list[str], *, input_text: str | None = None) -> int: | ||
| print(f"./push: $ {' '.join(argv)}", flush=True) | ||
| return subprocess.run( | ||
| argv, cwd=ROOT, input=input_text, text=True, check=False | ||
| ).returncode | ||
|
|
||
|
|
||
| def proof_states(rev: str = "HEAD") -> tuple[int, dict[str, str]]: | ||
| result = subprocess.run( | ||
| prooft("verify", rev, "--json"), | ||
| cwd=ROOT, | ||
| stdout=subprocess.PIPE, | ||
| text=True, | ||
| check=False, | ||
| ) | ||
| rows = json.loads(result.stdout) | ||
| return result.returncode, {row["gate"]: row["state"] for row in rows} | ||
|
|
||
|
|
||
| def reuse_cached_proof(states: dict[str, str]) -> bool: | ||
| """Restore previously verified gates if prooft accepts them for this HEAD.""" | ||
| cached = subprocess.run( | ||
| ["git", "rev-parse", "--verify", "--quiet", CACHE], | ||
| cwd=ROOT, | ||
| stdout=subprocess.PIPE, | ||
| text=True, | ||
| check=False, | ||
| ).stdout.strip() | ||
| if not cached or cached == git("rev-parse", "HEAD"): | ||
| return False | ||
| bundle = json.loads(subprocess.check_output(prooft("emit", cached), text=True)) | ||
| bundle["attestations"] = [ | ||
| proof | ||
| for proof in bundle["attestations"] | ||
| if states.get(proof["gate"]) != "valid" | ||
| ] | ||
| if bundle["attestations"]: | ||
| # Exit 1 also means the cached inputs no longer match. The next verify | ||
| # determines which lanes still need a new run. | ||
| return step(prooft("apply", "-"), input_text=json.dumps(bundle)) == 0 | ||
| return False | ||
|
|
||
|
|
||
| def attest() -> str: | ||
| _, states = proof_states() | ||
| required = set().union(*(lane.gates for lane in LANES)) | ||
| if not required.issubset(states): | ||
| raise RuntimeError("the policy is missing required gates") | ||
| if any(states[gate] != "valid" for gate in required) and reuse_cached_proof(states): | ||
| _, states = proof_states() | ||
| for lane in LANES: | ||
| if all(states.get(gate) == "valid" for gate in lane.gates): | ||
| print(f"./push: reusing {lane.name} proof", flush=True) | ||
| continue | ||
| if step(prooft("run", lane.name, "--", *lane.command)) != 0: | ||
| raise RuntimeError(f"the {lane.name} lane failed") | ||
| git("update-ref", CACHE, "HEAD") | ||
| head = git("rev-parse", "HEAD") | ||
| code, states = proof_states(head) | ||
| if ( | ||
| code | ||
| or not required.issubset(states) | ||
| or any(state != "valid" for state in states.values()) | ||
| ): | ||
| raise RuntimeError(f"HEAD did not verify: {states}") | ||
| git("update-ref", CACHE, head) | ||
| return head | ||
|
|
||
|
|
||
| def refusal(branch: str, remote: str) -> str | None: | ||
| remote_head = subprocess.run( | ||
| ["git", "symbolic-ref", "--quiet", "--short", f"refs/remotes/{remote}/HEAD"], | ||
| cwd=ROOT, | ||
| stdout=subprocess.PIPE, | ||
| text=True, | ||
| check=False, | ||
| ).stdout.strip() | ||
| if branch in {"HEAD", "main"} or remote_head == f"{remote}/{branch}": | ||
| return "work on a feature branch" | ||
| if git("status", "--porcelain", "--untracked-files=no"): | ||
| return "tracked files have uncommitted changes" | ||
| return None | ||
|
|
||
|
|
||
| def gh_json(*args: str): | ||
| return json.loads(subprocess.check_output(["gh", *args], cwd=ROOT, text=True)) | ||
|
|
||
|
|
||
| def open_pr(remote: str, branch: str, labels: list[str]) -> int: | ||
| url = git("remote", "get-url", "--push", remote) | ||
| repo = gh_json("repo", "view", url, "--json", "nameWithOwner,url") | ||
| url = repo["url"] | ||
| prs = gh_json( | ||
| "pr", | ||
| "list", | ||
| "--repo", | ||
| url, | ||
| "--head", | ||
| branch, | ||
| "--state", | ||
| "open", | ||
| "--limit", | ||
| "100", | ||
| "--json", | ||
| "headRepository,headRefName", | ||
| ) | ||
| if any( | ||
| pr["headRefName"] == branch | ||
| and (pr.get("headRepository") or {}).get("nameWithOwner") | ||
| == repo["nameWithOwner"] | ||
| for pr in prs | ||
| ): | ||
| return 0 | ||
| create = ["gh", "pr", "create", "--repo", url, "--fill", "--head", branch] | ||
| for label in labels: | ||
| create.extend(("--label", label)) | ||
| return int(step(create) != 0) | ||
|
|
||
|
|
||
| def main() -> int: | ||
| parser = argparse.ArgumentParser(description=__doc__, allow_abbrev=False) | ||
| parser.add_argument( | ||
| "--prove-only", action="store_true", help="attest without pushing" | ||
| ) | ||
| parser.add_argument( | ||
| "--no-pr", action="store_true", help="push without opening a PR" | ||
| ) | ||
| parser.add_argument("--remote", default="origin", help="git remote to push to") | ||
| parser.add_argument("--label", action="append", default=[], help="label a new PR") | ||
| args = parser.parse_args() | ||
|
|
||
| try: | ||
| branch = git("rev-parse", "--abbrev-ref", "HEAD") | ||
| if reason := refusal(branch, args.remote): | ||
| print(f"./push: refusing: {reason}", file=sys.stderr) | ||
| return 3 | ||
| if ( | ||
| platform.system() != "Linux" | ||
| or platform.machine() != "x86_64" | ||
| or platform.libc_ver()[0] != "glibc" | ||
| ): | ||
| raise RuntimeError("the attestation lanes require Linux x86_64 with glibc") | ||
| head = attest() | ||
| if args.prove_only: | ||
| return 0 | ||
| if ( | ||
| git("rev-parse", "HEAD") != head | ||
| or git("rev-parse", "--abbrev-ref", "HEAD") != branch | ||
| ): | ||
| raise RuntimeError("HEAD or the branch changed after verification") | ||
| target = f"{head}:refs/heads/{branch}" | ||
| if step(["git", "push", "--force-with-lease", "-u", args.remote, target]): | ||
| return 1 | ||
| # Git only configures -u automatically when the source is a branch ref. | ||
| git("branch", "--set-upstream-to", f"{args.remote}/{branch}", branch) | ||
| return 0 if args.no_pr else open_pr(args.remote, branch, args.label) | ||
| except (OSError, ValueError, RuntimeError, subprocess.CalledProcessError) as error: | ||
| print(f"./push: {error}", file=sys.stderr) | ||
| return 1 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
| os.execvp("uvx", ["uvx", "--from", PROOFT_SOURCE, "prooft", "--push", *sys.argv[1:]]) | ||
|
zackees marked this conversation as resolved.
Resolved
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.