|
1 | 1 | # Backend version information |
2 | | -VERSION = "WILL BE ADDED BY CI/CD, DO NOT CHANGE HERE" |
| 2 | +import subprocess |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +def get_version_from_git(): |
| 7 | + """Get version from git tags using the same logic as CI/CD.""" |
| 8 | + try: |
| 9 | + # Get the directory where this file is located |
| 10 | + backend_dir = Path(__file__).parent |
| 11 | + project_root = backend_dir.parent |
| 12 | + |
| 13 | + # Change to project root directory |
| 14 | + original_cwd = os.getcwd() |
| 15 | + os.chdir(project_root) |
| 16 | + |
| 17 | + try: |
| 18 | + # Get the latest needlectl tag |
| 19 | + result = subprocess.run( |
| 20 | + ["git", "tag", "-l", "needlectl/v*", "--sort=-v:refname"], |
| 21 | + capture_output=True, |
| 22 | + text=True, |
| 23 | + check=True |
| 24 | + ) |
| 25 | + tags = result.stdout.strip().split('\n') |
| 26 | + |
| 27 | + if not tags or tags == ['']: |
| 28 | + # No tags found, use base version |
| 29 | + base_version = "0.1.0" |
| 30 | + commit_count = subprocess.run( |
| 31 | + ["git", "rev-list", "--count", "HEAD"], |
| 32 | + capture_output=True, |
| 33 | + text=True, |
| 34 | + check=True |
| 35 | + ).stdout.strip() |
| 36 | + else: |
| 37 | + # Get the latest tag |
| 38 | + latest_tag = tags[0] |
| 39 | + # Extract version number without prefix |
| 40 | + base_version = latest_tag.replace("needlectl/v", "") |
| 41 | + |
| 42 | + # Count commits since last tag |
| 43 | + commit_count = subprocess.run( |
| 44 | + ["git", "rev-list", "--count", f"{latest_tag}..HEAD"], |
| 45 | + capture_output=True, |
| 46 | + text=True, |
| 47 | + check=True |
| 48 | + ).stdout.strip() |
| 49 | + |
| 50 | + # Split the base version |
| 51 | + major, minor, patch = map(int, base_version.split('.')) |
| 52 | + |
| 53 | + # Calculate new version |
| 54 | + new_patch = patch + int(commit_count) |
| 55 | + new_version = f"{major}.{minor}.{new_patch}" |
| 56 | + |
| 57 | + return new_version |
| 58 | + |
| 59 | + finally: |
| 60 | + # Restore original working directory |
| 61 | + os.chdir(original_cwd) |
| 62 | + |
| 63 | + except Exception as e: |
| 64 | + # Fallback to a default version if git fails |
| 65 | + return "0.1.0" |
| 66 | + |
| 67 | +# Get version dynamically from git |
| 68 | +VERSION = get_version_from_git() |
0 commit comments