Files
Ian MacLeod 6824dabe03 Guard prerelease channel and canary updates against older versions (#47597)
## Why

Publishing a hotfix for an older release line or alpha can replace a newer prerelease pointer. Release pointers should advance according to version order.

## What changed

- Share release version validation and numeric comparison across publishing scripts, including alpha hotfix suffixes and alpha, beta, and stable ordering.
- Read the prerelease pointer directly from R2 and update it only for a newer version or missing or invalid version metadata.
- Restrict `latest-alpha-cli` updates to alpha releases with successful R2 and npm publishing, and compare against the branch's Cargo version before advancing it. Allow replacement when the manifest version cannot be parsed or validated.

## Testing

Add 15 passing unit tests covering version validation, older release lines and alphas, numeric hotfix ordering, prerelease precedence, equal versions, and missing or invalid versions.

GitOrigin-RevId: 6bee3906a47f613b662ef2b2224cfcbd1b56650c
2026-09-23 17:35:40 +00:00

39 lines
1.2 KiB
Python

"""Validate and compare the version formats used by Codex releases."""
import re
import sys
_VERSION_RE = re.compile(
r"^[0-9]+\.[0-9]+\.[0-9]+(?:-(?:alpha(?:\.[0-9]+){0,2}"
r"|beta(?:\.[0-9]+)?))?$"
)
def is_valid_release_version(version: str) -> bool:
return _VERSION_RE.fullmatch(version) is not None
def _version_key(version: str) -> tuple[tuple[int, ...], int, tuple[int, ...]]:
base, _, prerelease = version.partition("-")
channel, _, suffix = prerelease.partition(".")
return (
tuple(int(part) for part in base.split(".")),
{"alpha": 0, "beta": 1, "": 2}[channel],
tuple(int(part) for part in suffix.split(".")) if suffix else (),
)
def should_update_version(version: str, current_version: str) -> bool:
"""Compare version strings; replace an unreadable current version."""
if not is_valid_release_version(version):
raise ValueError(f"invalid release version: {version}")
if not is_valid_release_version(current_version):
# Replace an unreadable pointer so publishing can repair it.
return True
return _version_key(version) > _version_key(current_version)
if __name__ == "__main__":
raise SystemExit(0 if is_valid_release_version(sys.argv[1]) else 1)