diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-09-29 15:50:58 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-09-29 15:50:58 +0200 |
| commit | cc70af2a5d8850218fa336e6acb2f46ee8207f40 (patch) | |
| tree | 4157f1864623005e829d2181697a1b136389cc85 /scripts | |
| parent | c50e5e10c0d8df6cf54b94194ffb514db390ed4a (diff) | |
| download | ele-cc70af2a5d8850218fa336e6acb2f46ee8207f40.tar.gz ele-cc70af2a5d8850218fa336e6acb2f46ee8207f40.zip | |
Automate tested branch releases for Linux and macOS
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/release.py | 155 | ||||
| -rw-r--r-- | scripts/test_release.py | 163 |
2 files changed, 318 insertions, 0 deletions
diff --git a/scripts/release.py b/scripts/release.py new file mode 100644 index 0000000..512feab --- /dev/null +++ b/scripts/release.py @@ -0,0 +1,155 @@ +"""Prepare versioned source bundles and publish tested Git references.""" + +import argparse +import copy +from pathlib import Path +import re +import subprocess +import tomllib + +VERSION = r"(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)" + + +def run(*args): + return subprocess.check_output(args, text=True).strip() + + +def branch_major(branch): + match = re.fullmatch(r"v(0|[1-9][0-9]*)\.x", branch) + if not match: + raise ValueError("release branch must be named vX.x, for example v0.x") + return int(match[1]) + + +def version_tuple(version): + match = re.fullmatch(VERSION, version) + if not match: + raise ValueError(f"expected a stable major.minor.patch version: {version}") + return tuple(map(int, match.groups())) + + +def next_version(branch, impact, tags): + major = branch_major(branch) + if impact not in ("patch", "minor"): + raise ValueError("impact must be patch or minor") + versions = [ + version_tuple(tag[1:]) for tag in tags + if re.fullmatch("v" + VERSION, tag) and version_tuple(tag[1:])[0] == major + ] + if not versions: + return f"{major}.{1 if major == 0 else 0}.0" + _, minor, patch = max(versions) + return f"{major}.{minor + 1}.0" if impact == "minor" else f"{major}.{minor}.{patch + 1}" + + +def require_ancestor(ancestor, commit): + subprocess.run(["git", "merge-base", "--is-ancestor", ancestor, commit], check=True) + + +def set_version(version): + manifest = Path("Cargo.toml") + text = manifest.read_text() + package = tomllib.loads(text)["package"] + if package["name"] != "aip_filter": + raise ValueError("expected the aip_filter package") + current = package["version"] + if version_tuple(current) > version_tuple(version): + raise ValueError(f"manifest version {current} exceeds the planned release {version}") + if current == version: + return + section = re.search(r"(?ms)^\[package\]\n.*?(?=^\[|\Z)", text) + if not section: + raise ValueError("expected an explicit [package] section") + body, count = re.subn(r'(?m)^version\s*=\s*"[^"]+"', f'version = "{version}"', section[0]) + if count != 1: + raise ValueError("expected one explicit package version") + expected_lock = copy.deepcopy(tomllib.loads(Path("Cargo.lock").read_text())) + packages = [p for p in expected_lock["package"] if p["name"] == "aip_filter" and "source" not in p] + if len(packages) != 1 or packages[0]["version"] != current: + raise ValueError("manifest and lockfile versions must agree") + packages[0]["version"] = version + manifest.write_text(text[:section.start()] + body + text[section.end():]) + subprocess.run(["cargo", "update", "--workspace"], check=True) + if tomllib.loads(Path("Cargo.lock").read_text()) != expected_lock: + raise ValueError("version update changed dependencies; refusing to prepare a release") + + +def prepare(branch, impact, bundle): + branch_major(branch) + if run("git", "status", "--porcelain"): + raise ValueError("release preparation requires a clean working tree") + base = run("git", "rev-parse", "HEAD") + if any(re.fullmatch("v" + VERSION, tag) for tag in run("git", "tag", "--points-at", base).splitlines()): + raise ValueError("this commit already has a release tag; rerun a failed publish job to resume it") + tags = run("git", "tag", "--list").splitlines() + version = next_version(branch, impact, tags) + for tag in tags: + if re.fullmatch("v" + VERSION, tag) and version_tuple(tag[1:])[0] == branch_major(branch): + require_ancestor(tag, base) + set_version(version) + subprocess.run(["git", "add", "Cargo.toml", "Cargo.lock"], check=True) + if run("git", "diff", "--cached", "--name-only"): + subprocess.run([ + "git", "-c", "user.name=github-actions[bot]", + "-c", "user.email=41898282+github-actions[bot]@users.noreply.github.com", + "-c", "commit.gpgsign=false", "commit", "-m", f"Release v{version}", + ], check=True) + commit = run("git", "rev-parse", "HEAD") + bundle.parent.mkdir(parents=True, exist_ok=True) + subprocess.run(["git", "bundle", "create", str(bundle), "HEAD"], check=True) + return {"version": version, "tag": f"v{version}", "commit": commit, "base": base} + + +def push_refs(branch, version, base, commit): + if branch_major(branch) != version_tuple(version)[0]: + raise ValueError("release version does not belong to this branch") + require_ancestor(base, commit) + if run("git", "rev-parse", "HEAD") != commit: + raise ValueError("checkout does not match the tested release commit") + if tomllib.loads(Path("Cargo.toml").read_text())["package"]["version"] != version: + raise ValueError("manifest does not match the release version") + if run("git", "status", "--porcelain"): + raise ValueError("publishing requires a clean working tree") + tag = f"v{version}" + remote_tag = run("git", "ls-remote", "--tags", "origin", f"refs/tags/{tag}") + if remote_tag: + subprocess.run(["git", "fetch", "origin", f"refs/tags/{tag}"], check=True) + if run("git", "rev-parse", "FETCH_HEAD^{commit}") != commit: + raise ValueError("the remote tag belongs to a different commit") + return + remote_branch = run("git", "ls-remote", "--heads", "origin", f"refs/heads/{branch}") + if not remote_branch or remote_branch.split()[0] != base: + raise ValueError("release branch has moved; start a new workflow run from its current head") + subprocess.run(["git", "tag", tag, commit], check=True) + subprocess.run([ + "git", "push", "--atomic", "origin", + f"{commit}:refs/heads/{branch}", f"refs/tags/{tag}:refs/tags/{tag}", + ], check=True) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + commands = parser.add_subparsers(dest="command", required=True) + prepare_parser = commands.add_parser("prepare") + prepare_parser.add_argument("--branch", required=True) + prepare_parser.add_argument("--impact", choices=("patch", "minor"), required=True) + prepare_parser.add_argument("--bundle", type=Path, required=True) + prepare_parser.add_argument("--output", type=Path, required=True) + push_parser = commands.add_parser("push") + for name in ("branch", "version", "base", "commit"): + push_parser.add_argument(f"--{name}", required=True) + args = parser.parse_args() + try: + if args.command == "prepare": + outputs = prepare(args.branch, args.impact, args.bundle) + with args.output.open("a") as output: + for name, value in outputs.items(): + print(f"{name}={value}", file=output) + else: + push_refs(args.branch, args.version, args.base, args.commit) + except (ValueError, subprocess.CalledProcessError) as error: + parser.exit(1, f"release: {error}\n") + + +if __name__ == "__main__": + main() diff --git a/scripts/test_release.py b/scripts/test_release.py new file mode 100644 index 0000000..1986ad5 --- /dev/null +++ b/scripts/test_release.py @@ -0,0 +1,163 @@ +import os +from pathlib import Path +import subprocess +import tempfile +import tomllib +import unittest + +import release + +ROOT = Path(__file__).resolve().parent.parent + + +class VersionTests(unittest.TestCase): + def test_initial_versions_and_impacts(self): + self.assertEqual(release.next_version("v0.x", "patch", []), "0.1.0") + self.assertEqual(release.next_version("v1.x", "minor", []), "1.0.0") + tags = ["v0.9.9", "v0.10.2", "v0.10.10", "v1.3.0", "v0.11.0-rc.1", "other"] + self.assertEqual(release.next_version("v0.x", "patch", tags), "0.10.11") + self.assertEqual(release.next_version("v0.x", "minor", tags), "0.11.0") + self.assertEqual(release.next_version("v1.x", "patch", tags), "1.3.1") + + def test_invalid_inputs(self): + for branch in ["master", "v01.x", "v1.2.x", "v1.x\n", "v1.x;echo bad"]: + with self.subTest(branch=branch), self.assertRaises(ValueError): + release.next_version(branch, "patch", []) + with self.assertRaises(ValueError): + release.next_version("v0.x", "major", []) + for version in ["01.2.3", "1.2", "1.2.3-rc.1", "1.2.3+build"]: + with self.subTest(version=version), self.assertRaises(ValueError): + release.version_tuple(version) + + +class RepositoryTests(unittest.TestCase): + def setUp(self): + directory = ROOT / "target" / "release-script-tests" + directory.mkdir(parents=True, exist_ok=True) + self.temporary = tempfile.TemporaryDirectory(dir=directory) + self.addCleanup(self.temporary.cleanup) + self.previous = Path.cwd() + self.addCleanup(os.chdir, self.previous) + self.root = Path(self.temporary.name) + self.repo = self.root / "repo" + self.repo.mkdir() + os.chdir(self.repo) + release.run("git", "init", "-b", "v0.x") + release.run("git", "config", "user.name", "Release tests") + release.run("git", "config", "user.email", "release-tests@example.invalid") + release.run("git", "config", "commit.gpgsign", "false") + Path(".gitignore").write_text("target/\n") + Path("Cargo.toml").write_text( + '[package]\nname = "aip_filter"\nversion = "0.1.0"\nedition = "2024"\n' + ) + Path("src").mkdir() + Path("src/lib.rs").write_text("pub fn example() {}\n") + release.run("cargo", "generate-lockfile", "--offline") + self.commit("Initial package") + self.remote = self.root / "origin.git" + release.run("git", "init", "--bare", "-b", "v0.x", str(self.remote)) + release.run("git", "remote", "add", "origin", str(self.remote)) + release.run("git", "push", "origin", "HEAD:refs/heads/v0.x") + + def commit(self, message): + release.run("git", "add", ".") + release.run("git", "commit", "-m", message) + return release.run("git", "rev-parse", "HEAD") + + def prepare(self, impact="patch"): + return release.prepare("v0.x", impact, Path("target/source.bundle")) + + def publish(self, result): + release.push_refs("v0.x", result["version"], result["base"], result["commit"]) + + def patch_source(self): + release.run("git", "tag", "v0.1.0") + release.run("git", "push", "origin", "v0.1.0") + Path("src/lib.rs").write_text("pub fn example() -> bool { true }\n") + self.commit("Change package") + release.run("git", "push", "origin", "HEAD:refs/heads/v0.x") + + def test_initial_prepare_has_no_remote_side_effects(self): + before = release.run("git", "ls-remote", "origin") + result = self.prepare() + self.assertEqual(result["version"], "0.1.0") + self.assertEqual(result["base"], result["commit"]) + self.assertEqual(release.run("git", "tag", "--list"), "") + self.assertEqual(release.run("git", "ls-remote", "origin"), before) + release.run("git", "bundle", "verify", "target/source.bundle") + self.assertEqual(release.run("git", "status", "--porcelain"), "") + + def test_patch_updates_manifest_lock_and_bundle_and_publishes_atomically(self): + self.patch_source() + before = release.run("git", "ls-remote", "origin") + result = self.prepare() + self.assertEqual(result["version"], "0.1.1") + self.assertNotEqual(result["base"], result["commit"]) + self.assertEqual(release.run("git", "ls-remote", "origin"), before) + self.assertEqual(tomllib.loads(Path("Cargo.toml").read_text())["package"]["version"], "0.1.1") + self.assertEqual(tomllib.loads(Path("Cargo.lock").read_text())["package"][0]["version"], "0.1.1") + release.run("cargo", "check", "--locked", "--offline") + clone = self.root / "from-bundle" + release.run("git", "clone", "target/source.bundle", str(clone)) + self.assertEqual(release.run("git", "-C", str(clone), "rev-parse", "HEAD"), result["commit"]) + self.publish(result) + refs = release.run("git", "ls-remote", "origin") + self.assertIn(result["commit"] + "\trefs/heads/v0.x", refs) + self.assertIn(result["commit"] + "\trefs/tags/v0.1.1", refs) + self.publish(result) + self.assertEqual(release.run("git", "ls-remote", "origin"), refs) + + def test_already_tagged_dirty_and_downgraded_versions_fail(self): + Path("untracked").write_text("not committed") + with self.assertRaises(ValueError): + self.prepare() + Path("untracked").unlink() + release.run("git", "tag", "v0.1.0") + with self.assertRaises(ValueError): + self.prepare() + release.run("git", "tag", "-d", "v0.1.0") + with self.assertRaises(ValueError): + release.set_version("0.0.9") + + def test_moved_branch_fails_without_a_tag(self): + result = self.prepare() + Path("new-file").write_text("new branch head") + advanced = self.commit("Advance branch") + release.run("git", "push", "origin", "HEAD:refs/heads/v0.x") + release.run("git", "checkout", "--detach", result["commit"]) + with self.assertRaises(ValueError): + self.publish(result) + self.assertEqual(release.run("git", "ls-remote", "--tags", "origin"), "") + self.assertIn(advanced, release.run("git", "ls-remote", "--heads", "origin")) + + def test_conflicting_remote_tag_fails(self): + self.patch_source() + result = self.prepare() + release.run("git", "tag", result["tag"], result["base"]) + release.run("git", "push", "origin", result["tag"]) + with self.assertRaises(ValueError): + self.publish(result) + + def test_unmerged_release_tags_fail(self): + base = release.run("git", "rev-parse", "HEAD") + Path("other-change").write_text("another release branch") + self.commit("Other branch") + release.run("git", "tag", "v0.2.0") + release.run("git", "checkout", "--detach", base) + with self.assertRaises(subprocess.CalledProcessError): + self.prepare() + + def test_atomic_push_does_not_advance_branch_when_tag_is_rejected(self): + self.patch_source() + result = self.prepare() + hook = self.remote / "hooks" / "update" + hook.write_text('#!/bin/sh\ncase "$1" in refs/tags/*) exit 1;; esac\n') + hook.chmod(0o755) + with self.assertRaises(subprocess.CalledProcessError): + self.publish(result) + self.assertIn(result["base"], release.run("git", "ls-remote", "--heads", "origin")) + self.assertEqual(release.run("git", "ls-remote", "--tags", "origin", result["tag"]), "") + + +if __name__ == "__main__": + unittest.main() |
