|
| 1 | +import os |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +def run(*args: str) -> str: |
| 8 | + return subprocess.check_output(args, text=True).strip() |
| 9 | + |
| 10 | + |
| 11 | +def write_output(key: str, value: str) -> None: |
| 12 | + output_path = os.getenv("GITHUB_OUTPUT") |
| 13 | + if not output_path: |
| 14 | + return |
| 15 | + with open(output_path, "a", encoding="utf-8") as fh: |
| 16 | + fh.write(f"{key}={value}\n") |
| 17 | + |
| 18 | + |
| 19 | +def latest_version_tag() -> str | None: |
| 20 | + tags = run("git", "tag", "--list", "v*", "--sort=-v:refname").splitlines() |
| 21 | + for tag in tags: |
| 22 | + if re.fullmatch(r"v\d+\.\d+\.\d+", tag): |
| 23 | + return tag |
| 24 | + return None |
| 25 | + |
| 26 | + |
| 27 | +def read_setup_version() -> str: |
| 28 | + setup_text = Path("setup.py").read_text(encoding="utf-8") |
| 29 | + match = re.search(r"version\s*=\s*['\"](\d+\.\d+\.\d+)['\"]", setup_text) |
| 30 | + if not match: |
| 31 | + raise RuntimeError("Could not parse current version from setup.py") |
| 32 | + return match.group(1) |
| 33 | + |
| 34 | + |
| 35 | +def commit_log_since(tag: str | None) -> str: |
| 36 | + if tag: |
| 37 | + return run("git", "log", "--format=%s%n%b", f"{tag}..HEAD") |
| 38 | + return run("git", "log", "--format=%s%n%b") |
| 39 | + |
| 40 | + |
| 41 | +def commits_since(tag: str | None) -> int: |
| 42 | + if tag: |
| 43 | + return int(run("git", "rev-list", "--count", f"{tag}..HEAD")) |
| 44 | + return int(run("git", "rev-list", "--count", "HEAD")) |
| 45 | + |
| 46 | + |
| 47 | +def determine_bump(log_text: str) -> str: |
| 48 | + if re.search(r"BREAKING CHANGE|!:", log_text, re.IGNORECASE): |
| 49 | + return "major" |
| 50 | + if re.search(r"(?mi)^feat(\(.+\))?:", log_text): |
| 51 | + return "minor" |
| 52 | + return "patch" |
| 53 | + |
| 54 | + |
| 55 | +def bump_version(current: str, bump: str) -> str: |
| 56 | + major, minor, patch = [int(part) for part in current.split(".")] |
| 57 | + if bump == "major": |
| 58 | + return f"{major + 1}.0.0" |
| 59 | + if bump == "minor": |
| 60 | + return f"{major}.{minor + 1}.0" |
| 61 | + return f"{major}.{minor}.{patch + 1}" |
| 62 | + |
| 63 | + |
| 64 | +def update_setup_py(new_version: str) -> bool: |
| 65 | + setup_path = Path("setup.py") |
| 66 | + setup_text = setup_path.read_text(encoding="utf-8") |
| 67 | + new_text, replacements = re.subn( |
| 68 | + r"version\s*=\s*['\"]\d+\.\d+\.\d+['\"]", |
| 69 | + f"version='{new_version}'", |
| 70 | + setup_text, |
| 71 | + count=1, |
| 72 | + ) |
| 73 | + if replacements == 0: |
| 74 | + raise RuntimeError("Could not find version assignment in setup.py") |
| 75 | + if new_text == setup_text: |
| 76 | + return False |
| 77 | + setup_path.write_text(new_text, encoding="utf-8") |
| 78 | + return True |
| 79 | + |
| 80 | + |
| 81 | +def main() -> None: |
| 82 | + tag = latest_version_tag() |
| 83 | + current_version = tag[1:] if tag else read_setup_version() |
| 84 | + |
| 85 | + if commits_since(tag) == 0: |
| 86 | + write_output("released", "false") |
| 87 | + return |
| 88 | + |
| 89 | + bump_kind = determine_bump(commit_log_since(tag)) if tag else "patch" |
| 90 | + next_version = bump_version(current_version, bump_kind) |
| 91 | + changed = update_setup_py(next_version) |
| 92 | + |
| 93 | + write_output("version", next_version) |
| 94 | + write_output("released", "true" if changed else "false") |
| 95 | + print(f"Bumped {current_version} -> {next_version} ({bump_kind})") |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + main() |
0 commit comments