Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(ci): mark anchored Perception release PRs as tagged (#4222)

Francesco Bonacci99a12b876251
diff --git a/.github/scripts/mark_perception_release_prs_tagged.py b/.github/scripts/mark_perception_release_prs_tagged.py
new file mode 100755
index 0000000000..34014671b0
--- /dev/null
+++ b/.github/scripts/mark_perception_release_prs_tagged.py
@@ -0,0 +1,112 @@
+#!/usr/bin/env python3
+"""Mark merged Cua Perception release pull requests as tagged.
+
+Release Please refuses to open or update any release pull request while a
+merged release pull request still carries ``autorelease: pending``. It swaps
+that label for ``autorelease: tagged`` only when it creates a GitHub release
+itself. Cua Perception sets ``skip-github-release`` and is tagged by
+``anchor_perception_release_tag.sh`` instead, so its merged release pull
+request keeps the pending label forever and blocks every other component.
+
+This script closes that gap. For each merged Perception release pull request
+that is still pending, it reads the version the merge commit set and relabels
+the pull request only when ``cua-perception-v<version>`` is a lightweight tag
+at that exact merge commit. Anything else is left pending, so Release Please
+keeps failing closed until the tag is correct.
+
+Inputs (environment): GITHUB_REPOSITORY, GH_TOKEN (used by gh).
+"""
+
+from __future__ import annotations
+
+import json
+import os
+import re
+import subprocess
+import sys
+
+BRANCH = "release-please--branches--main--components--cua-perception"
+VERSION_PATH = "libs/cua-driver/rust/crates/cua-perception/VERSION"
+PENDING = "autorelease: pending"
+TAGGED = "autorelease: tagged"
+SEMVER = re.compile(r"^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$")
+
+
+def run(*args: str, check: bool = True) -> subprocess.CompletedProcess[str]:
+ return subprocess.run(list(args), check=check, capture_output=True, text=True)
+
+
+def pending_pull_requests(repository: str) -> list[dict]:
+ result = run(
+ "gh", "pr", "list",
+ "--repo", repository,
+ "--state", "merged",
+ "--base", "main",
+ "--label", PENDING,
+ "--search", f"head:{BRANCH}",
+ "--limit", "100",
+ "--json", "number,headRefName,mergeCommit",
+ )
+ pulls = json.loads(result.stdout or "[]")
+ # Search matches head-branch prefixes, so require the exact bot branch.
+ return [pull for pull in pulls if pull.get("headRefName") == BRANCH]
+
+
+def merged_version(sha: str) -> str | None:
+ result = run("git", "show", f"{sha}:{VERSION_PATH}", check=False)
+ if result.returncode != 0:
+ return None
+ version = result.stdout.strip()
+ return version if SEMVER.fullmatch(version) else None
+
+
+def remote_tag(repository: str, tag: str) -> tuple[str, str] | None:
+ result = run("gh", "api", f"repos/{repository}/git/ref/tags/{tag}", check=False)
+ try:
+ body = json.loads(result.stdout or "{}")
+ except json.JSONDecodeError:
+ body = {}
+ if result.returncode == 0:
+ return body["object"]["type"], body["object"]["sha"]
+ if body.get("status") == "404" or body.get("message") == "Not Found":
+ return None
+ raise SystemExit(f"::error::Could not read {tag} (gh exit {result.returncode})")
+
+
+def main() -> int:
+ repository = os.environ["GITHUB_REPOSITORY"]
+ pulls = pending_pull_requests(repository)
+ if not pulls:
+ print("No merged Perception release pull request is pending")
+ return 0
+
+ for pull in pulls:
+ number = pull["number"]
+ sha = (pull.get("mergeCommit") or {}).get("oid")
+ if not sha:
+ print(f"::warning::#{number} has no merge commit; leaving it pending")
+ continue
+ version = merged_version(sha)
+ if version is None:
+ print(f"::warning::#{number} merge {sha} sets no valid {VERSION_PATH}; leaving it pending")
+ continue
+ tag = f"cua-perception-v{version}"
+ record = remote_tag(repository, tag)
+ if record != ("commit", sha):
+ print(
+ f"::warning::{tag} is not a lightweight tag at #{number}'s merge "
+ f"commit {sha} (found {record}); leaving it pending"
+ )
+ continue
+ run(
+ "gh", "pr", "edit", str(number),
+ "--repo", repository,
+ "--remove-label", PENDING,
+ "--add-label", TAGGED,
+ )
+ print(f"Marked #{number} as tagged at {tag}")
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/.github/scripts/tests/test_mark_perception_release_prs_tagged.py b/.github/scripts/tests/test_mark_perception_release_prs_tagged.py
new file mode 100644
index 0000000000..4b17f94088
--- /dev/null
+++ b/.github/scripts/tests/test_mark_perception_release_prs_tagged.py
@@ -0,0 +1,162 @@
+"""Behavioral tests for mark_perception_release_prs_tagged.py with a fake gh."""
+
+from __future__ import annotations
+
+import json
+import os
+import subprocess
+import sys
+import textwrap
+from pathlib import Path
+
+import pytest
+import yaml
+
+ROOT = Path(__file__).resolve().parents[3]
+SCRIPT = ROOT / ".github/scripts/mark_perception_release_prs_tagged.py"
+WORKFLOW = ROOT / ".github/workflows/release-please.yml"
+VERSION_PATH = "libs/cua-driver/rust/crates/cua-perception/VERSION"
+BRANCH = "release-please--branches--main--components--cua-perception"
+
+# The fake gh serves pull requests and tags from a JSON state file and logs
+# every call. Like the real gh, a missing ref prints the 404 body on stdout.
+FAKE_GH = textwrap.dedent(
+ """\
+ #!{python}
+ import json, os, pathlib, sys
+
+ state = json.loads(pathlib.Path(os.environ["FAKE_GH_STATE"]).read_text())
+ args = sys.argv[1:]
+ with open(os.environ["FAKE_GH_LOG"], "a") as log:
+ log.write(json.dumps(args) + "\\n")
+ if args[:2] == ["pr", "list"]:
+ print(json.dumps(state["pulls"]))
+ raise SystemExit(0)
+ if args[:2] == ["pr", "edit"]:
+ raise SystemExit(0)
+ if args[0] == "api":
+ if state.get("mode") == "server-error":
+ print(json.dumps({{"message": "Server Error", "status": "500"}}))
+ raise SystemExit(1)
+ name = args[1].split("/git/ref/tags/", 1)[1]
+ if name not in state["tags"]:
+ print(json.dumps({{"message": "Not Found", "status": "404"}}))
+ raise SystemExit(1)
+ print(json.dumps({{"ref": "refs/tags/" + name, "object": state["tags"][name]}}))
+ raise SystemExit(0)
+ raise SystemExit(2)
+ """
+)
+
+
+def git(repo: Path, *args: str) -> str:
+ return subprocess.run(
+ ["git", *args], cwd=repo, check=True, capture_output=True, text=True
+ ).stdout.strip()
+
+
+def env(tmp_path: Path) -> dict[str, object]:
+ repo = tmp_path / "repo"
+ subprocess.run(["git", "init", "-q", "-b", "main", str(repo)], check=True)
+ git(repo, "config", "user.email", "[email protected]")
+ git(repo, "config", "user.name", "Test")
+ version = repo / VERSION_PATH
+ version.parent.mkdir(parents=True)
+ version.write_text("0.2.1\n")
+ git(repo, "add", "-A")
+ git(repo, "commit", "-q", "-m", "chore(main): release cua-perception 0.2.1")
+ bin_dir = tmp_path / "bin"
+ bin_dir.mkdir()
+ fake = bin_dir / "gh"
+ fake.write_text(FAKE_GH.format(python=sys.executable))
+ fake.chmod(0o755)
+ return {
+ "repo": repo,
+ "sha": git(repo, "rev-parse", "HEAD"),
+ "bin": bin_dir,
+ "state": tmp_path / "state.json",
+ "log": tmp_path / "gh.log",
+ }
+
+
+def run(env: dict[str, object], pulls: list[dict], tags: dict, **extra) -> tuple[subprocess.CompletedProcess[str], list[list[str]]]:
+ env["state"].write_text(json.dumps({"pulls": pulls, "tags": tags, **extra}))
+ environment = dict(os.environ)
+ environment.update({
+ "PATH": f"{env['bin']}{os.pathsep}{environment['PATH']}",
+ "GITHUB_REPOSITORY": "trycua/cua",
+ "FAKE_GH_STATE": str(env["state"]),
+ "FAKE_GH_LOG": str(env["log"]),
+ })
+ result = subprocess.run(
+ [sys.executable, str(SCRIPT)], cwd=env["repo"], env=environment,
+ capture_output=True, text=True,
+ )
+ log = env["log"]
+ calls = [json.loads(line) for line in log.read_text().splitlines()] if log.exists() else []
+ return result, calls
+
+
+def edits(calls: list[list[str]]) -> list[list[str]]:
+ return [call for call in calls if call[:2] == ["pr", "edit"]]
+
+
+def pull(env, number: int = 4075, branch: str = BRANCH) -> dict:
+ return {"number": number, "headRefName": branch, "mergeCommit": {"oid": env["sha"]}}
+
+
+def test_pending_pull_request_with_anchored_tag_is_marked_tagged(env) -> None:
+ tags = {"cua-perception-v0.2.1": {"type": "commit", "sha": env["sha"]}}
+ result, calls = run(env, [pull(env)], tags)
+ assert result.returncode == 0, result.stdout + result.stderr
+ assert edits(calls) == [[
+ "pr", "edit", "4075", "--repo", "trycua/cua",
+ "--remove-label", "autorelease: pending",
+ "--add-label", "autorelease: tagged",
+ ]]
+
+
+def test_missing_tag_leaves_the_pull_request_pending(env) -> None:
+ result, calls = run(env, [pull(env)], {})
+ assert result.returncode == 0, result.stdout + result.stderr
+ assert edits(calls) == []
+ assert "leaving it pending" in result.stdout
+
+
[email protected]("record", [
+ {"type": "commit", "sha": "0" * 40},
+ {"type": "tag", "sha": "SHA"},
+])
+def test_tag_elsewhere_or_annotated_leaves_the_pull_request_pending(env, record) -> None:
+ record = {**record, "sha": env["sha"] if record["sha"] == "SHA" else record["sha"]}
+ result, calls = run(env, [pull(env)], {"cua-perception-v0.2.1": record})
+ assert result.returncode == 0, result.stdout + result.stderr
+ assert edits(calls) == []
+
+
+def test_other_branches_are_ignored(env) -> None:
+ tags = {"cua-perception-v0.2.1": {"type": "commit", "sha": env["sha"]}}
+ other = pull(env, 1, "release-please--branches--main--components--cua-perception-extra")
+ result, calls = run(env, [other], tags)
+ assert result.returncode == 0, result.stdout + result.stderr
+ assert edits(calls) == []
+
+
+def test_tag_lookup_errors_fail_closed(env) -> None:
+ result, calls = run(env, [pull(env)], {}, mode="server-error")
+ assert result.returncode != 0
+ assert edits(calls) == []
+
+
+def test_runs_after_the_anchor_and_before_release_please() -> None:
+ steps = yaml.safe_load(WORKFLOW.read_text())["jobs"]["release-please"]["steps"]
+ names = [step.get("name") for step in steps]
+ mark = names.index("Mark tagged Perception release pull requests")
+ assert names.index("Anchor a merged Perception version with a lightweight tag") < mark
+ assert mark < names.index("Open or update automatic release pull requests")
+ assert mark < names.index("Open or update the targeted release pull request")
+ step = steps[mark]
+ assert "if" not in step
+ assert step["env"]["GH_TOKEN"] == "${{ steps.app-token.outputs.token }}"
+ assert step["run"] == "python3 .github/scripts/mark_perception_release_prs_tagged.py"
diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml
index 87a2106852..3278bf2913 100644
--- a/.github/workflows/release-please.yml
+++ b/.github/workflows/release-please.yml
@@ -90,6 +90,14 @@ jobs:
# release. A missed tag heals on the next main push.
run: bash .github/scripts/anchor_perception_release_tag.sh
+ # Release Please aborts every release PR while a merged release PR is
+ # still labeled `autorelease: pending`. Perception skips GitHub releases,
+ # so Release Please never relabels it; do that once its tag is anchored.
+ - name: Mark tagged Perception release pull requests
+ env:
+ GH_TOKEN: ${{ steps.app-token.outputs.token }}
+ run: python3 .github/scripts/mark_perception_release_prs_tagged.py
+
# release-please-action v4.4.1, embedding release-please 17.3.0.
- name: Open or update automatic release pull requests
id: release-auto