|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Detect the type of change based on version tags.""" |
| 3 | + |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +from packaging.version import Version |
| 10 | + |
| 11 | + |
| 12 | +def get_previous_tag() -> Optional[str]: |
| 13 | + """Get the previous release tag.""" |
| 14 | + try: |
| 15 | + # Get all tags sorted by version |
| 16 | + result = subprocess.run( |
| 17 | + ["git", "tag", "-l", "v*", "--sort=-version:refname"], |
| 18 | + capture_output=True, |
| 19 | + text=True, |
| 20 | + check=True, |
| 21 | + ) |
| 22 | + tags = result.stdout.strip().split("\n") |
| 23 | + |
| 24 | + # Current tag is the first one (if we're on a tag) |
| 25 | + current_ref = os.environ.get("GITHUB_REF", "") |
| 26 | + if current_ref.startswith("refs/tags/"): |
| 27 | + current_tag = current_ref.replace("refs/tags/", "") |
| 28 | + # Find current tag in list and return the next one |
| 29 | + if current_tag in tags: |
| 30 | + current_index = tags.index(current_tag) |
| 31 | + if current_index + 1 < len(tags): |
| 32 | + return tags[current_index + 1] |
| 33 | + |
| 34 | + # If not on a tag or no previous tag found |
| 35 | + return tags[0] if tags else None |
| 36 | + except subprocess.CalledProcessError: |
| 37 | + return None |
| 38 | + |
| 39 | + |
| 40 | +def detect_change_type(current_version: str, previous_version: Optional[str]) -> str: |
| 41 | + """Detect the type of change based on version comparison.""" |
| 42 | + if not previous_version: |
| 43 | + # First release or no previous version |
| 44 | + return "major" |
| 45 | + |
| 46 | + try: |
| 47 | + # Remove 'v' prefix if present |
| 48 | + current_clean = current_version.lstrip("v") |
| 49 | + previous_clean = previous_version.lstrip("v") |
| 50 | + |
| 51 | + curr_version = Version(current_clean) |
| 52 | + prev_version = Version(previous_clean) |
| 53 | + |
| 54 | + # Compare versions using packaging.version |
| 55 | + if curr_version <= prev_version: |
| 56 | + # Same or lower version (shouldn't happen in normal flow) |
| 57 | + return "none" |
| 58 | + |
| 59 | + # Extract major.minor.patch components for semantic comparison |
| 60 | + curr_release = curr_version.release |
| 61 | + prev_release = prev_version.release |
| 62 | + |
| 63 | + # Ensure we have at least 3 components (major, minor, patch) |
| 64 | + curr_major, curr_minor, curr_patch = (curr_release + (0, 0, 0))[:3] |
| 65 | + prev_major, prev_minor, prev_patch = (prev_release + (0, 0, 0))[:3] |
| 66 | + |
| 67 | + if curr_major > prev_major: |
| 68 | + return "major" |
| 69 | + elif curr_minor > prev_minor: |
| 70 | + return "minor" |
| 71 | + elif curr_patch > prev_patch: |
| 72 | + return "patch" |
| 73 | + else: |
| 74 | + # This shouldn't happen if curr_version > prev_version |
| 75 | + return "minor" |
| 76 | + except Exception: |
| 77 | + # If we can't parse versions, default to minor |
| 78 | + return "minor" |
| 79 | + |
| 80 | + |
| 81 | +def main() -> None: |
| 82 | + """Main function to detect change type.""" |
| 83 | + # Get current tag from GITHUB_REF |
| 84 | + github_ref = os.environ.get("GITHUB_REF", "") |
| 85 | + if not github_ref.startswith("refs/tags/"): |
| 86 | + print("Not a tag push, no change type detection needed") |
| 87 | + sys.exit(0) |
| 88 | + |
| 89 | + current_tag = github_ref.replace("refs/tags/", "") |
| 90 | + previous_tag = get_previous_tag() |
| 91 | + |
| 92 | + change_type = detect_change_type(current_tag, previous_tag) |
| 93 | + |
| 94 | + print(f"Current tag: {current_tag}") |
| 95 | + if previous_tag: |
| 96 | + print(f"Previous tag: {previous_tag}") |
| 97 | + else: |
| 98 | + print("No previous tag found") |
| 99 | + print(f"Change type: {change_type}") |
| 100 | + |
| 101 | + # Output for GitHub Actions |
| 102 | + if github_output := os.environ.get("GITHUB_OUTPUT"): |
| 103 | + with open(github_output, "a") as f: |
| 104 | + f.write(f"change_type={change_type}\n") |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + main() |
0 commit comments