|
1 | | -name: Auto Release Tag |
2 | | - |
3 | | -on: |
4 | | - push: |
5 | | - branches: |
6 | | - - main |
7 | | - paths: |
8 | | - - 'src/index.json' |
9 | | - |
10 | | -jobs: |
11 | | - create-release: |
12 | | - runs-on: windows-latest |
13 | | - |
14 | | - steps: |
15 | | - - uses: actions/checkout@v4 |
16 | | - with: |
17 | | - fetch-depth: 2 |
18 | | - |
19 | | - - name: Set up Python |
20 | | - uses: actions/setup-python@v5 |
21 | | - with: |
22 | | - python-version: '3.x' |
23 | | - |
24 | | - - name: Install dependencies |
25 | | - run: | |
26 | | - python -m pip install --upgrade pip |
27 | | - pip install requests |
28 | | - |
29 | | - - name: Create Release Tag |
30 | | - env: |
31 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
32 | | - run: | |
33 | | - python ./create_release_tag.py |
| 1 | +import os |
| 2 | +import re |
| 3 | +import requests |
| 4 | +import subprocess |
| 5 | +from typing import List, Dict, Tuple, Optional |
| 6 | +import sys |
| 7 | + |
| 8 | +TARGET_FILE = "src/index.json" |
| 9 | + |
| 10 | +def get_api_urls() -> Tuple[str, str]: |
| 11 | + """Generate GitHub API URLs based on GITHUB_REPOSITORY environment variable.""" |
| 12 | + repo = os.environ.get("GITHUB_REPOSITORY") |
| 13 | + if not repo: |
| 14 | + print("Error: GITHUB_REPOSITORY environment variable is not set") |
| 15 | + print("This script is designed to run in GitHub Actions environment") |
| 16 | + sys.exit(1) |
| 17 | + |
| 18 | + base_url = f"https://api.github.com/repos/{repo}" |
| 19 | + return f"{base_url}/releases", f"{base_url}/git/tags" |
| 20 | + |
| 21 | +def get_file_changes() -> List[str]: |
| 22 | + diff_output = subprocess.check_output( |
| 23 | + ["git", "diff", "HEAD^", "HEAD", "--", TARGET_FILE], |
| 24 | + text=True |
| 25 | + ) |
| 26 | + |
| 27 | + added_lines = [ |
| 28 | + line[1:].strip() |
| 29 | + for line in diff_output.splitlines() |
| 30 | + if line.startswith("+") and not line.startswith("+++") |
| 31 | + ] |
| 32 | + |
| 33 | + return added_lines |
| 34 | + |
| 35 | +def parse_filename(added_lines: List[str]) -> Optional[str]: |
| 36 | + for line in added_lines: |
| 37 | + if '"filename":' in line: |
| 38 | + try: |
| 39 | + filename = line.split(":")[1].strip().strip('",') |
| 40 | + return filename |
| 41 | + except IndexError: |
| 42 | + print(f"Error parsing line: {line}") |
| 43 | + return None |
| 44 | + |
| 45 | +def generate_tag_and_title(filename: str) -> Tuple[str, str]: |
| 46 | + match = re.match(r"^(.*?)[-_](\d+\.\d+\.\d+[a-z0-9]*)", filename) |
| 47 | + if not match: |
| 48 | + raise ValueError(f"Invalid filename format: {filename}") |
| 49 | + |
| 50 | + name = match.group(1).replace("_", "-") |
| 51 | + version = match.group(2) |
| 52 | + |
| 53 | + tag_name = f"{name}-{version}" |
| 54 | + release_title = f"Release {tag_name}" |
| 55 | + return tag_name, release_title |
| 56 | + |
| 57 | +def check_tag_exists(tag_name: str, headers: Dict[str, str]) -> bool: |
| 58 | + response = requests.get( |
| 59 | + f"{TAG_API_URL}/{tag_name}", |
| 60 | + headers=headers |
| 61 | + ) |
| 62 | + return response.status_code == 200 |
| 63 | + |
| 64 | +def create_release(release_data: Dict[str, str], headers: Dict[str, str]) -> None: |
| 65 | + try: |
| 66 | + response = requests.post( |
| 67 | + RELEASE_API_URL, |
| 68 | + json=release_data, |
| 69 | + headers=headers |
| 70 | + ) |
| 71 | + response.raise_for_status() |
| 72 | + print(f"Successfully created release for {release_data['tag_name']}") |
| 73 | + except requests.exceptions.RequestException as e: |
| 74 | + print(f"Failed to create release for {release_data['tag_name']}: {e}") |
| 75 | + |
| 76 | +def main(): |
| 77 | + github_token = os.environ.get("GITHUB_TOKEN") |
| 78 | + if not github_token: |
| 79 | + print("Error: GITHUB_TOKEN environment variable is required") |
| 80 | + sys.exit(1) |
| 81 | + |
| 82 | + headers = { |
| 83 | + "Authorization": f"token {github_token}", |
| 84 | + "Accept": "application/vnd.github.v3+json" |
| 85 | + } |
| 86 | + |
| 87 | + # Get API URLs from environment |
| 88 | + RELEASE_API_URL, TAG_API_URL = get_api_urls() |
| 89 | + |
| 90 | + try: |
| 91 | + added_lines = get_file_changes() |
| 92 | + if not added_lines: |
| 93 | + print("No changes found in index.json") |
| 94 | + return |
| 95 | + |
| 96 | + filename = parse_filename(added_lines) |
| 97 | + if not filename: |
| 98 | + print("No filename found in changes") |
| 99 | + return |
| 100 | + |
| 101 | + try: |
| 102 | + tag_name, release_title = generate_tag_and_title(filename) |
| 103 | + |
| 104 | + commit_sha = subprocess.check_output( |
| 105 | + ["git", "rev-parse", "HEAD"], |
| 106 | + text=True |
| 107 | + ).strip() |
| 108 | + |
| 109 | + if check_tag_exists(commit_sha, headers): |
| 110 | + print(f"Commit {commit_sha} already has a tag, skipping...") |
| 111 | + return |
| 112 | + |
| 113 | + release_data = { |
| 114 | + "tag_name": tag_name, |
| 115 | + "target_commitish": commit_sha, |
| 116 | + "name": release_title, |
| 117 | + "body": release_title |
| 118 | + } |
| 119 | + |
| 120 | + create_release(release_data, headers) |
| 121 | + |
| 122 | + except ValueError as e: |
| 123 | + print(f"Error generating tag for filename {filename}: {e}") |
| 124 | + return |
| 125 | + |
| 126 | + except Exception as e: |
| 127 | + print(f"Unexpected error: {e}") |
| 128 | + raise |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + main() |
0 commit comments