|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Publish one stable Boatstack root/module tag pair atomically.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +import re |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +STABLE_TAG = re.compile(r"^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$") |
| 15 | + |
| 16 | + |
| 17 | +class PublicationBlocked(RuntimeError): |
| 18 | + """The selected tag pair cannot be published safely.""" |
| 19 | + |
| 20 | + |
| 21 | +def git(repository: Path, *arguments: str, check: bool = True) -> subprocess.CompletedProcess[str]: |
| 22 | + result = subprocess.run( |
| 23 | + ["git", *arguments], |
| 24 | + cwd=repository, |
| 25 | + text=True, |
| 26 | + capture_output=True, |
| 27 | + ) |
| 28 | + if check and result.returncode != 0: |
| 29 | + detail = result.stderr.strip() or result.stdout.strip() |
| 30 | + raise PublicationBlocked(f"git {' '.join(arguments)} failed: {detail}") |
| 31 | + return result |
| 32 | + |
| 33 | + |
| 34 | +def git_output(repository: Path, *arguments: str) -> str: |
| 35 | + return git(repository, *arguments).stdout.strip() |
| 36 | + |
| 37 | + |
| 38 | +def remote_ref(repository: Path, remote: str, ref: str) -> tuple[str, str] | None: |
| 39 | + result = git(repository, "ls-remote", remote, ref, f"{ref}^{{}}") |
| 40 | + refs: dict[str, str] = {} |
| 41 | + for line in result.stdout.splitlines(): |
| 42 | + object_id, name = line.split(maxsplit=1) |
| 43 | + refs[name] = object_id |
| 44 | + direct = refs.get(ref) |
| 45 | + if direct is None: |
| 46 | + return None |
| 47 | + return direct, refs.get(f"{ref}^{{}}", direct) |
| 48 | + |
| 49 | + |
| 50 | +def exact_commit(repository: Path, source: str) -> str: |
| 51 | + resolved = git_output(repository, "rev-parse", "--verify", f"{source}^{{commit}}") |
| 52 | + if resolved != source: |
| 53 | + raise PublicationBlocked(f"release source must be an exact commit SHA: {source}") |
| 54 | + return resolved |
| 55 | + |
| 56 | + |
| 57 | +def require_source(repository: Path, remote: str, source: str) -> None: |
| 58 | + exact_commit(repository, source) |
| 59 | + head = git_output(repository, "rev-parse", "HEAD") |
| 60 | + if head != source: |
| 61 | + raise PublicationBlocked(f"checked-out source {head} does not match release source {source}") |
| 62 | + main = remote_ref(repository, remote, "refs/heads/main") |
| 63 | + if main is None: |
| 64 | + raise PublicationBlocked("remote main is absent") |
| 65 | + if main[0] != source: |
| 66 | + raise PublicationBlocked(f"remote main moved from {source} to {main[0]}") |
| 67 | + |
| 68 | + |
| 69 | +def inspect_pair( |
| 70 | + repository: Path, |
| 71 | + remote: str, |
| 72 | + root_tag: str, |
| 73 | + module_tag: str, |
| 74 | +) -> tuple[tuple[str, str] | None, tuple[str, str] | None]: |
| 75 | + root = remote_ref(repository, remote, f"refs/tags/{root_tag}") |
| 76 | + module = remote_ref(repository, remote, f"refs/tags/{module_tag}") |
| 77 | + return root, module |
| 78 | + |
| 79 | + |
| 80 | +def require_pair_absent( |
| 81 | + repository: Path, |
| 82 | + remote: str, |
| 83 | + root_tag: str, |
| 84 | + module_tag: str, |
| 85 | +) -> None: |
| 86 | + root, module = inspect_pair(repository, remote, root_tag, module_tag) |
| 87 | + if root is not None and module is not None and root[1] != module[1]: |
| 88 | + raise PublicationBlocked( |
| 89 | + f"paired tag targets differ: {root_tag}={root[1]}, {module_tag}={module[1]}" |
| 90 | + ) |
| 91 | + existing = [ |
| 92 | + tag |
| 93 | + for tag, target in ((root_tag, root), (module_tag, module)) |
| 94 | + if target is not None |
| 95 | + ] |
| 96 | + if existing: |
| 97 | + raise PublicationBlocked(f"release tag already exists: {', '.join(existing)}") |
| 98 | + |
| 99 | + |
| 100 | +def publish( |
| 101 | + repository: Path, |
| 102 | + remote: str, |
| 103 | + source: str, |
| 104 | + root_tag: str, |
| 105 | + module_tag: str, |
| 106 | +) -> dict[str, str]: |
| 107 | + if STABLE_TAG.fullmatch(root_tag) is None: |
| 108 | + raise PublicationBlocked(f"root tag is not a stable vMAJOR.MINOR.PATCH tag: {root_tag}") |
| 109 | + expected_module_tag = f"boatstack/{root_tag}" |
| 110 | + if module_tag != expected_module_tag: |
| 111 | + raise PublicationBlocked( |
| 112 | + f"module tag {module_tag} does not match derived tag {expected_module_tag}" |
| 113 | + ) |
| 114 | + |
| 115 | + require_source(repository, remote, source) |
| 116 | + require_pair_absent(repository, remote, root_tag, module_tag) |
| 117 | + for tag in (root_tag, module_tag): |
| 118 | + local = git(repository, "show-ref", "--verify", "--quiet", f"refs/tags/{tag}", check=False) |
| 119 | + if local.returncode == 0: |
| 120 | + raise PublicationBlocked(f"local release tag already exists: {tag}") |
| 121 | + if local.returncode != 1: |
| 122 | + raise PublicationBlocked(f"could not inspect local release tag: {tag}") |
| 123 | + |
| 124 | + git(repository, "tag", "-a", root_tag, "-m", f"Boatstack {root_tag}", source) |
| 125 | + git( |
| 126 | + repository, |
| 127 | + "tag", |
| 128 | + "-a", |
| 129 | + module_tag, |
| 130 | + "-m", |
| 131 | + f"Boatstack Go module {root_tag}", |
| 132 | + source, |
| 133 | + ) |
| 134 | + for tag in (root_tag, module_tag): |
| 135 | + target = git_output(repository, "rev-parse", f"refs/tags/{tag}^{{commit}}") |
| 136 | + if target != source: |
| 137 | + raise PublicationBlocked(f"local tag {tag} resolves to {target}, expected {source}") |
| 138 | + |
| 139 | + # These checks deliberately run again after local tag creation. A racing |
| 140 | + # remote write is then rejected by the final non-force atomic push. |
| 141 | + require_source(repository, remote, source) |
| 142 | + require_pair_absent(repository, remote, root_tag, module_tag) |
| 143 | + git( |
| 144 | + repository, |
| 145 | + "push", |
| 146 | + "--atomic", |
| 147 | + remote, |
| 148 | + f"refs/tags/{root_tag}", |
| 149 | + f"refs/tags/{module_tag}", |
| 150 | + ) |
| 151 | + |
| 152 | + root, module = inspect_pair(repository, remote, root_tag, module_tag) |
| 153 | + if root is None or module is None or root[1] != source or module[1] != source: |
| 154 | + raise PublicationBlocked("published tag pair does not resolve to the release source") |
| 155 | + return { |
| 156 | + "module_tag": module_tag, |
| 157 | + "release_source": source, |
| 158 | + "root_tag": root_tag, |
| 159 | + } |
| 160 | + |
| 161 | + |
| 162 | +def main() -> int: |
| 163 | + parser = argparse.ArgumentParser() |
| 164 | + parser.add_argument("--repo", type=Path, default=Path.cwd()) |
| 165 | + parser.add_argument("--remote", default="origin") |
| 166 | + parser.add_argument("--source", required=True) |
| 167 | + parser.add_argument("--root-tag", required=True) |
| 168 | + parser.add_argument("--module-tag", required=True) |
| 169 | + arguments = parser.parse_args() |
| 170 | + |
| 171 | + try: |
| 172 | + result = publish( |
| 173 | + arguments.repo.resolve(), |
| 174 | + arguments.remote, |
| 175 | + arguments.source, |
| 176 | + arguments.root_tag, |
| 177 | + arguments.module_tag, |
| 178 | + ) |
| 179 | + except PublicationBlocked as error: |
| 180 | + print(f"BLOCKED: {error}", file=sys.stderr) |
| 181 | + return 2 |
| 182 | + print(json.dumps(result, sort_keys=True)) |
| 183 | + return 0 |
| 184 | + |
| 185 | + |
| 186 | +if __name__ == "__main__": |
| 187 | + raise SystemExit(main()) |
0 commit comments