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.

ci(release): rebase release branches only when main changed their component (#5535)

Francesco Bonaccida6fd06f142c
diff --git a/.github/scripts/release_branch_needs_rebase.py b/.github/scripts/release_branch_needs_rebase.py
new file mode 100644
index 0000000000..2364352b9a
--- /dev/null
+++ b/.github/scripts/release_branch_needs_rebase.py
@@ -0,0 +1,82 @@
+#!/usr/bin/env python3
+"""Decide whether a Release Please branch must be rebased onto main.
+
+Every rebase force-pushes the branch, and every force-push restarts the
+release PR's whole check suite. Rebase only when it is needed:
+
+* the branch no longer merges cleanly into main, or
+* commits that touch this component's paths (or the shared release tooling)
+ landed on main after the branch's merge base, so the changelog and
+ generated files could be stale.
+
+Prints ``rebase <reason>`` or ``keep <reason>`` and always exits 0 on a
+decision. Release Please itself still regenerates the branch whenever a
+relevant commit lands, so the changelog stays complete.
+"""
+
+from __future__ import annotations
+
+import argparse
+import json
+import subprocess
+import sys
+
+COMPONENT_PREFIX = "release-please--branches--main--components--"
+# Shared inputs that change how every release branch is generated or checked.
+SHARED_PATHS = (
+ ".github/scripts",
+ "release-please-config.json",
+)
+
+
+def git(*args: str, check: bool = True) -> subprocess.CompletedProcess[str]:
+ return subprocess.run(
+ ["git", *args], check=check, capture_output=True, text=True
+ )
+
+
+def component_paths(config: dict, branch: str) -> list[str]:
+ if not branch.startswith(COMPONENT_PREFIX):
+ return []
+ component = branch[len(COMPONENT_PREFIX) :]
+ return [
+ path
+ for path, package in config.get("packages", {}).items()
+ if package.get("component") == component
+ ]
+
+
+def decide(config: dict, branch: str, base: str, head: str) -> tuple[str, str]:
+ paths = component_paths(config, branch)
+ if not paths:
+ return "rebase", "unknown component; keep the previous behavior"
+
+ merge_tree = git("merge-tree", "--write-tree", base, head, check=False)
+ if merge_tree.returncode != 0:
+ return "rebase", "branch conflicts with main"
+
+ merge_base = git("merge-base", base, head).stdout.strip()
+ changed = git(
+ "diff", "--name-only", merge_base, base, "--", *paths, *SHARED_PATHS
+ ).stdout.split()
+ if changed:
+ return "rebase", f"main changed {len(changed)} relevant file(s), e.g. {changed[0]}"
+ return "keep", "no relevant commits landed on main since the branch base"
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("--branch", required=True)
+ parser.add_argument("--base", default="origin/main")
+ parser.add_argument("--head", default="HEAD")
+ parser.add_argument("--config", default="release-please-config.json")
+ args = parser.parse_args()
+ with open(args.config, encoding="utf-8") as handle:
+ config = json.load(handle)
+ action, reason = decide(config, args.branch, args.base, args.head)
+ print(f"{action} {reason}")
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/.github/scripts/tests/test_release_branch_needs_rebase.py b/.github/scripts/tests/test_release_branch_needs_rebase.py
new file mode 100644
index 0000000000..3873fa60f8
--- /dev/null
+++ b/.github/scripts/tests/test_release_branch_needs_rebase.py
@@ -0,0 +1,80 @@
+"""Dry-run checks for the release branch rebase decision."""
+
+from __future__ import annotations
+
+import importlib.util
+import subprocess
+from pathlib import Path
+
+SCRIPT = Path(__file__).resolve().parents[1] / "release_branch_needs_rebase.py"
+spec = importlib.util.spec_from_file_location("release_branch_needs_rebase", SCRIPT)
+mod = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(mod)
+
+BRANCH = mod.COMPONENT_PREFIX + "lume"
+CONFIG = {
+ "packages": {
+ "libs/lume": {"component": "lume"},
+ "libs/cua-driver": {"component": "cua-driver-rs"},
+ }
+}
+
+
+def run(repo: Path, *args: str) -> None:
+ subprocess.run(["git", "-C", str(repo), *args], check=True, capture_output=True)
+
+
+def commit(repo: Path, path: str, text: str) -> None:
+ target = repo / path
+ target.parent.mkdir(parents=True, exist_ok=True)
+ target.write_text(text)
+ run(repo, "add", path)
+ run(repo, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-m", f"edit {path}")
+
+
+def make_repo(tmp_path: Path) -> Path:
+ run(tmp_path, "init", "-q", "-b", "main")
+ commit(tmp_path, "libs/lume/a.txt", "base\n")
+ run(tmp_path, "checkout", "-q", "-b", "release")
+ commit(tmp_path, "libs/lume/VERSION", "1.1.0\n")
+ run(tmp_path, "checkout", "-q", "main")
+ return tmp_path
+
+
+def decide(repo: Path, monkeypatch) -> str:
+ monkeypatch.chdir(repo)
+ return mod.decide(CONFIG, BRANCH, "main", "release")[0]
+
+
+def test_unrelated_main_push_keeps_branch(tmp_path, monkeypatch):
+ repo = make_repo(tmp_path)
+ commit(repo, "docs/readme.md", "unrelated\n")
+ assert decide(repo, monkeypatch) == "keep"
+
+
+def test_other_component_push_keeps_branch(tmp_path, monkeypatch):
+ repo = make_repo(tmp_path)
+ commit(repo, "libs/cua-driver/x.rs", "other\n")
+ assert decide(repo, monkeypatch) == "keep"
+
+
+def test_component_commit_triggers_rebase(tmp_path, monkeypatch):
+ repo = make_repo(tmp_path)
+ commit(repo, "libs/lume/b.txt", "new feature\n")
+ assert decide(repo, monkeypatch) == "rebase"
+
+
+def test_shared_release_tooling_triggers_rebase(tmp_path, monkeypatch):
+ repo = make_repo(tmp_path)
+ commit(repo, ".github/scripts/validate_release_versions.py", "x\n")
+ assert decide(repo, monkeypatch) == "rebase"
+
+
+def test_conflict_triggers_rebase(tmp_path, monkeypatch):
+ repo = make_repo(tmp_path)
+ commit(repo, "libs/lume/VERSION", "9.9.9\n")
+ assert decide(repo, monkeypatch) == "rebase"
+
+
+def test_unknown_branch_keeps_old_behavior():
+ assert mod.decide(CONFIG, "feature/x", "main", "HEAD")[0] == "rebase"
diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml
index 14938eec60..a7f570e799 100644
--- a/.github/workflows/release-please.yml
+++ b/.github/workflows/release-please.yml
@@ -192,14 +192,24 @@ jobs:
# Release Please can update an existing branch that predates the
# current workflow. Refresh it, then replay only this trusted bot
- # branch onto main so validation uses the current release tooling.
+ # branch onto main when main changed this component or the release
+ # tooling, or when the branch conflicts, so validation uses the
+ # current release tooling. Otherwise keep the branch untouched.
git fetch origin "+refs/heads/main:refs/remotes/origin/main"
git fetch origin "+refs/heads/$BRANCH:refs/remotes/origin/$BRANCH"
REMOTE_HEAD=$(git rev-parse "origin/$BRANCH")
+ # Every force-push restarts the release PR's checks, so replay
+ # the branch only when it must move. Decide from main's copy of
+ # the script, before the checkout swaps in the branch's tree.
+ DECISION=$(python3 .github/scripts/release_branch_needs_rebase.py \
+ --branch "$BRANCH" --head "origin/$BRANCH")
+ echo "Release PR #$number: $DECISION"
git checkout -B "$BRANCH" "origin/$BRANCH"
git config user.name "trycua-release[bot]"
git config user.email "trycua-release[bot]@users.noreply.github.com"
- if ! git rebase origin/main; then
+ if [[ "$DECISION" == keep* ]]; then
+ echo "Leaving the release branch and its checks alone"
+ elif ! git rebase origin/main; then
mapfile -t CONFLICTS < <(git diff --name-only --diff-filter=U)
if [[ "${#CONFLICTS[@]}" -ne 1 || \
"${CONFLICTS[0]}" != ".release-please-manifest.json" ]]; then