|
| 1 | +"""Guard package version changes so they only happen in release PRs.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +import re |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +import tomllib |
| 10 | +from dataclasses import dataclass |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +PACKAGE_PYPROJECTS: dict[str, Path] = { |
| 15 | + "openhands-sdk": Path("openhands-sdk/pyproject.toml"), |
| 16 | + "openhands-tools": Path("openhands-tools/pyproject.toml"), |
| 17 | + "openhands-workspace": Path("openhands-workspace/pyproject.toml"), |
| 18 | + "openhands-agent-server": Path("openhands-agent-server/pyproject.toml"), |
| 19 | +} |
| 20 | + |
| 21 | +_VERSION_PATTERN = r"\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.]+)?" |
| 22 | +_RELEASE_TITLE_RE = re.compile(rf"^Release v(?P<version>{_VERSION_PATTERN})$") |
| 23 | +_RELEASE_BRANCH_RE = re.compile(rf"^rel-(?P<version>{_VERSION_PATTERN})$") |
| 24 | + |
| 25 | + |
| 26 | +@dataclass(frozen=True) |
| 27 | +class VersionChange: |
| 28 | + package: str |
| 29 | + path: Path |
| 30 | + previous_version: str |
| 31 | + current_version: str |
| 32 | + |
| 33 | + |
| 34 | +def _read_version_from_pyproject_text(text: str, source: str) -> str: |
| 35 | + data = tomllib.loads(text) |
| 36 | + version = data.get("project", {}).get("version") |
| 37 | + if not isinstance(version, str): |
| 38 | + raise SystemExit(f"Unable to determine project.version from {source}") |
| 39 | + return version |
| 40 | + |
| 41 | + |
| 42 | +def _read_current_version(repo_root: Path, pyproject: Path) -> str: |
| 43 | + return _read_version_from_pyproject_text( |
| 44 | + (repo_root / pyproject).read_text(), |
| 45 | + str(pyproject), |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def _read_version_from_git_ref(repo_root: Path, git_ref: str, pyproject: Path) -> str: |
| 50 | + result = subprocess.run( |
| 51 | + ["git", "show", f"{git_ref}:{pyproject.as_posix()}"], |
| 52 | + cwd=repo_root, |
| 53 | + check=False, |
| 54 | + capture_output=True, |
| 55 | + text=True, |
| 56 | + ) |
| 57 | + if result.returncode != 0: |
| 58 | + message = result.stderr.strip() or result.stdout.strip() or "unknown git error" |
| 59 | + raise SystemExit( |
| 60 | + f"Unable to read {pyproject} from git ref {git_ref}: {message}" |
| 61 | + ) |
| 62 | + return _read_version_from_pyproject_text(result.stdout, f"{git_ref}:{pyproject}") |
| 63 | + |
| 64 | + |
| 65 | +def _base_ref_candidates(base_ref: str) -> list[str]: |
| 66 | + if base_ref.startswith("origin/"): |
| 67 | + return [base_ref, base_ref.removeprefix("origin/")] |
| 68 | + return [f"origin/{base_ref}", base_ref] |
| 69 | + |
| 70 | + |
| 71 | +def find_version_changes(repo_root: Path, base_ref: str) -> list[VersionChange]: |
| 72 | + changes: list[VersionChange] = [] |
| 73 | + candidates = _base_ref_candidates(base_ref) |
| 74 | + |
| 75 | + for package, pyproject in PACKAGE_PYPROJECTS.items(): |
| 76 | + current_version = _read_current_version(repo_root, pyproject) |
| 77 | + previous_error: SystemExit | None = None |
| 78 | + previous_version: str | None = None |
| 79 | + |
| 80 | + for candidate in candidates: |
| 81 | + try: |
| 82 | + previous_version = _read_version_from_git_ref( |
| 83 | + repo_root, candidate, pyproject |
| 84 | + ) |
| 85 | + break |
| 86 | + except SystemExit as exc: |
| 87 | + previous_error = exc |
| 88 | + |
| 89 | + if previous_version is None: |
| 90 | + assert previous_error is not None |
| 91 | + raise previous_error |
| 92 | + |
| 93 | + if previous_version != current_version: |
| 94 | + changes.append( |
| 95 | + VersionChange( |
| 96 | + package=package, |
| 97 | + path=pyproject, |
| 98 | + previous_version=previous_version, |
| 99 | + current_version=current_version, |
| 100 | + ) |
| 101 | + ) |
| 102 | + |
| 103 | + return changes |
| 104 | + |
| 105 | + |
| 106 | +def get_release_pr_version( |
| 107 | + pr_title: str, pr_head_ref: str |
| 108 | +) -> tuple[str | None, list[str]]: |
| 109 | + title_match = _RELEASE_TITLE_RE.fullmatch(pr_title.strip()) |
| 110 | + branch_match = _RELEASE_BRANCH_RE.fullmatch(pr_head_ref.strip()) |
| 111 | + title_version = title_match.group("version") if title_match else None |
| 112 | + branch_version = branch_match.group("version") if branch_match else None |
| 113 | + |
| 114 | + if title_version and branch_version and title_version != branch_version: |
| 115 | + return None, [ |
| 116 | + "Release PR markers disagree: title requests " |
| 117 | + f"v{title_version} but branch is rel-{branch_version}." |
| 118 | + ] |
| 119 | + |
| 120 | + return title_version or branch_version, [] |
| 121 | + |
| 122 | + |
| 123 | +def validate_version_changes( |
| 124 | + changes: list[VersionChange], |
| 125 | + pr_title: str, |
| 126 | + pr_head_ref: str, |
| 127 | +) -> list[str]: |
| 128 | + if not changes: |
| 129 | + return [] |
| 130 | + |
| 131 | + release_version, errors = get_release_pr_version(pr_title, pr_head_ref) |
| 132 | + if errors: |
| 133 | + return errors |
| 134 | + |
| 135 | + formatted_changes = ", ".join( |
| 136 | + f"{change.package} ({change.previous_version} -> {change.current_version})" |
| 137 | + for change in changes |
| 138 | + ) |
| 139 | + |
| 140 | + if release_version is None: |
| 141 | + return [ |
| 142 | + "Package version changes are only allowed in release PRs. " |
| 143 | + f"Detected changes: {formatted_changes}. " |
| 144 | + "Use the Prepare Release workflow so the PR title is 'Release vX.Y.Z' " |
| 145 | + "or the branch is 'rel-X.Y.Z'." |
| 146 | + ] |
| 147 | + |
| 148 | + mismatched = [ |
| 149 | + change for change in changes if change.current_version != release_version |
| 150 | + ] |
| 151 | + if mismatched: |
| 152 | + mismatch_details = ", ".join( |
| 153 | + f"{change.package} ({change.current_version})" for change in mismatched |
| 154 | + ) |
| 155 | + return [ |
| 156 | + f"Release PR version v{release_version} does not match changed package " |
| 157 | + f"versions: {mismatch_details}." |
| 158 | + ] |
| 159 | + |
| 160 | + return [] |
| 161 | + |
| 162 | + |
| 163 | +def main() -> int: |
| 164 | + repo_root = Path(__file__).resolve().parents[2] |
| 165 | + base_ref = os.environ.get("VERSION_BUMP_BASE_REF") or os.environ.get( |
| 166 | + "GITHUB_BASE_REF" |
| 167 | + ) |
| 168 | + if not base_ref: |
| 169 | + print("::warning title=Version bump guard::No base ref found; skipping check.") |
| 170 | + return 0 |
| 171 | + |
| 172 | + pr_title = os.environ.get("PR_TITLE", "") |
| 173 | + pr_head_ref = os.environ.get("PR_HEAD_REF", "") |
| 174 | + |
| 175 | + changes = find_version_changes(repo_root, base_ref) |
| 176 | + errors = validate_version_changes(changes, pr_title, pr_head_ref) |
| 177 | + |
| 178 | + if errors: |
| 179 | + for error in errors: |
| 180 | + print(f"::error title=Version bump guard::{error}") |
| 181 | + return 1 |
| 182 | + |
| 183 | + if changes: |
| 184 | + changed_packages = ", ".join(change.package for change in changes) |
| 185 | + print( |
| 186 | + "::notice title=Version bump guard::" |
| 187 | + f"Release PR version changes validated for {changed_packages}." |
| 188 | + ) |
| 189 | + else: |
| 190 | + print("::notice title=Version bump guard::No package version changes detected.") |
| 191 | + |
| 192 | + return 0 |
| 193 | + |
| 194 | + |
| 195 | +if __name__ == "__main__": |
| 196 | + sys.exit(main()) |
0 commit comments