|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | + |
| 6 | + |
| 7 | +def version_split(value: str) -> tuple[int, ...]: |
| 8 | + """Split string into a tuple of ints.""" |
| 9 | + try: |
| 10 | + return tuple(int(n) for n in value.split(".")) |
| 11 | + except ValueError: |
| 12 | + return (-1,) |
| 13 | + |
| 14 | + |
| 15 | +def is_prerelease(tag: str) -> bool: |
| 16 | + """Determine if a tag is a pre-release version.""" |
| 17 | + omit = {"rc", "alpha", "beta", "dev"} |
| 18 | + |
| 19 | + return any(n in tag for n in omit) |
| 20 | + |
| 21 | + |
| 22 | +def get_latest_stable() -> str | None: |
| 23 | + """Return the latest stable tag.""" |
| 24 | + stdout = subprocess.check_output(["git", "tag"], text=True) |
| 25 | + tags = [tag for tag in stdout.splitlines() if not is_prerelease(tag)] |
| 26 | + tags.sort(key=version_split) |
| 27 | + |
| 28 | + return tags[-1] if tags else None |
| 29 | + |
| 30 | + |
| 31 | +def main() -> None: |
| 32 | + if not (current_tag := os.environ.get("GIT_TAG")): |
| 33 | + reason = "GIT_TAG environment variable not set, skipping latest tag" |
| 34 | + apply_latest = "false" |
| 35 | + elif is_prerelease(current_tag): |
| 36 | + reason = f"{current_tag} is a pre-release" |
| 37 | + apply_latest = "false" |
| 38 | + else: |
| 39 | + latest_stable = get_latest_stable() |
| 40 | + if current_tag == latest_stable: |
| 41 | + reason = f"{current_tag} is the latest stable" |
| 42 | + apply_latest = "true" |
| 43 | + else: |
| 44 | + reason = f"{current_tag} is not the latest stable ({latest_stable} is)" |
| 45 | + apply_latest = "false" |
| 46 | + |
| 47 | + print(reason) |
| 48 | + |
| 49 | + if github_output := os.environ.get("GITHUB_OUTPUT"): |
| 50 | + with open(github_output, "a") as f: |
| 51 | + f.write(f"apply_latest={apply_latest}\n") |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + main() |
0 commit comments