|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Resolve the next FA4 beta tag and optionally create + push it. |
| 3 | +
|
| 4 | +Usage: |
| 5 | + python bump_beta_tag.py # dry-run by default |
| 6 | + python bump_beta_tag.py --push # create and push the tag |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import argparse |
| 12 | +import os |
| 13 | +import re |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | + |
| 17 | +TAG_PATTERN = re.compile(r"^(fa4-v.+\.beta)(\d+)$") |
| 18 | + |
| 19 | + |
| 20 | +def git(*args: str) -> str: |
| 21 | + result = subprocess.run( |
| 22 | + ["git", *args], capture_output=True, text=True, check=True |
| 23 | + ) |
| 24 | + return result.stdout.strip() |
| 25 | + |
| 26 | + |
| 27 | +def get_beta_tags() -> list[tuple[str, int]]: |
| 28 | + raw = git("tag", "-l", "fa4-v*.beta*") |
| 29 | + if not raw: |
| 30 | + return [] |
| 31 | + tags = [] |
| 32 | + for line in raw.splitlines(): |
| 33 | + m = TAG_PATTERN.match(line.strip()) |
| 34 | + if m: |
| 35 | + tags.append((line.strip(), int(m.group(2)))) |
| 36 | + return sorted(tags, key=lambda t: t[1]) |
| 37 | + |
| 38 | + |
| 39 | +def tag_exists(tag: str) -> bool: |
| 40 | + result = subprocess.run( |
| 41 | + ["git", "rev-parse", tag], capture_output=True, text=True |
| 42 | + ) |
| 43 | + return result.returncode == 0 |
| 44 | + |
| 45 | + |
| 46 | +def set_github_output(key: str, value: str) -> None: |
| 47 | + path = os.environ.get("GITHUB_OUTPUT") |
| 48 | + if path: |
| 49 | + with open(path, "a") as f: |
| 50 | + f.write(f"{key}={value}\n") |
| 51 | + |
| 52 | + |
| 53 | +def main() -> None: |
| 54 | + parser = argparse.ArgumentParser() |
| 55 | + parser.add_argument("--push", action="store_true", help="Create and push the tag (default: dry-run)") |
| 56 | + args = parser.parse_args() |
| 57 | + |
| 58 | + tags = get_beta_tags() |
| 59 | + if not tags: |
| 60 | + print("::error::No existing fa4-v*.beta* tags found", file=sys.stderr) |
| 61 | + sys.exit(1) |
| 62 | + |
| 63 | + latest_tag, latest_num = tags[-1] |
| 64 | + next_num = latest_num + 1 |
| 65 | + prefix = TAG_PATTERN.match(latest_tag) |
| 66 | + if prefix is None: |
| 67 | + print(f"::error::Latest tag {latest_tag!r} no longer matches pattern", file=sys.stderr) |
| 68 | + sys.exit(1) |
| 69 | + next_tag = f"{prefix.group(1)}{next_num}" |
| 70 | + |
| 71 | + already_exists = tag_exists(next_tag) |
| 72 | + |
| 73 | + if already_exists: |
| 74 | + print(f"Tag {next_tag} already exists, reusing it") |
| 75 | + else: |
| 76 | + print(f"Bumping: {latest_tag} -> {next_tag}") |
| 77 | + |
| 78 | + set_github_output("next_tag", next_tag) |
| 79 | + |
| 80 | + if args.push and not already_exists: |
| 81 | + try: |
| 82 | + git("tag", next_tag) |
| 83 | + git("push", "origin", next_tag) |
| 84 | + except subprocess.CalledProcessError: |
| 85 | + if tag_exists(next_tag): |
| 86 | + print(f"Tag {next_tag} was created by a concurrent run, reusing it") |
| 87 | + else: |
| 88 | + raise |
| 89 | + else: |
| 90 | + print(f"Pushed {next_tag}") |
| 91 | + elif not args.push: |
| 92 | + print(f"Dry-run: would create and push {next_tag}") |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + main() |
0 commit comments