|
| 1 | +import os |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | +from collections import defaultdict |
| 5 | + |
| 6 | +# Regex patterns for Conventional Commits |
| 7 | +feat_pattern = re.compile(r"^feat(\([^)]+\))?:", re.IGNORECASE) |
| 8 | +feat_breaking_pattern = re.compile(r"^feat!|^feat!\([^)]*\):", re.IGNORECASE) |
| 9 | +fix_pattern = re.compile(r"^fix(\([^)]+\))?:", re.IGNORECASE) |
| 10 | + |
| 11 | + |
| 12 | +def get_version_bump_from_commits(last_tag: str | None) -> str | None: |
| 13 | + """ |
| 14 | + Determines the version bump type ('major', 'minor', 'patch') from commit messages. |
| 15 | + """ |
| 16 | + try: |
| 17 | + if last_tag: |
| 18 | + commits = subprocess.check_output( |
| 19 | + ["git", "log", f"{last_tag}..HEAD", "--merges", "--pretty=format:%B"], |
| 20 | + text=True, |
| 21 | + ).strip() |
| 22 | + else: |
| 23 | + commits = subprocess.check_output( |
| 24 | + ["git", "log", "--merges", "--pretty=format:%B"], text=True |
| 25 | + ).strip() |
| 26 | + except Exception as exc: |
| 27 | + print(f"Error getting commits: {exc}") |
| 28 | + commits = "" |
| 29 | + |
| 30 | + if not commits: |
| 31 | + return None |
| 32 | + |
| 33 | + commit_lines = [c.strip() for c in commits.splitlines() if c.strip()] |
| 34 | + bump_commits = defaultdict(list) |
| 35 | + |
| 36 | + for c in commit_lines: |
| 37 | + if feat_breaking_pattern.match(c): |
| 38 | + bump_commits["major"].append(c) |
| 39 | + elif feat_pattern.match(c): |
| 40 | + bump_commits["minor"].append(c) |
| 41 | + elif fix_pattern.match(c): |
| 42 | + bump_commits["patch"].append(c) |
| 43 | + |
| 44 | + if bump_commits["major"]: |
| 45 | + bump_type = "major" |
| 46 | + elif bump_commits["minor"]: |
| 47 | + bump_type = "minor" |
| 48 | + elif bump_commits["patch"]: |
| 49 | + bump_type = "patch" |
| 50 | + else: |
| 51 | + bump_type = None |
| 52 | + |
| 53 | + print(f"\nVersion bump type from commits: {bump_type}") |
| 54 | + for bt in ["major", "minor", "patch"]: |
| 55 | + if bump_commits[bt]: |
| 56 | + print(f"\n{bt.upper()} commits ({len(bump_commits[bt])}):") |
| 57 | + for commit in bump_commits[bt]: |
| 58 | + print(f" {commit}") |
| 59 | + |
| 60 | + return bump_type |
| 61 | + |
| 62 | + |
| 63 | +def main(): |
| 64 | + """ |
| 65 | + Main function to determine version bump part and current version. |
| 66 | + """ |
| 67 | + # Determine version part |
| 68 | + part = os.environ.get("VERSION_PART_INPUT") |
| 69 | + if not part: |
| 70 | + try: |
| 71 | + last_tag = ( |
| 72 | + subprocess.check_output(["git", "describe", "--tags", "--abbrev=0"]) |
| 73 | + .decode() |
| 74 | + .strip() |
| 75 | + ) |
| 76 | + except subprocess.CalledProcessError as exc: |
| 77 | + print( |
| 78 | + f"Could not get last tag, probably because there are no tags yet. Error: {exc}" |
| 79 | + ) |
| 80 | + last_tag = None |
| 81 | + print(f"Last tag: {last_tag}") |
| 82 | + part = get_version_bump_from_commits(last_tag) |
| 83 | + else: |
| 84 | + print(f"Got version bump part from input: {part}") |
| 85 | + |
| 86 | + # Set outputs |
| 87 | + with open(os.environ["GITHUB_OUTPUT"], "a") as f: |
| 88 | + if part: |
| 89 | + f.write(f"part={part}\n") |
| 90 | + f.write("bump_needed=true\n") |
| 91 | + else: |
| 92 | + f.write("bump_needed=false\n") |
| 93 | + print("No version bump needed.") |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + main() |
0 commit comments