From f5a9a079171b17d825bbced487374df6499762ac Mon Sep 17 00:00:00 2001 From: Illyoung Choi Date: Thu, 12 Sep 2024 14:43:21 -0700 Subject: [PATCH] generate a table in release note that shows supported os and arch --- .github/workflows/release.yml | 21 ++++ .github/workflows/update_release_table.py | 125 ++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 .github/workflows/update_release_table.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9428ded..115d9e0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -35,3 +35,24 @@ jobs: project_path: "./cmd/" binary_name: "gocmd" ldflags: "-X ${{ env.PKG }}/commons.clientVersion=${{ github.ref_name }} -X ${{ env.PKG }}/commons.gitCommit=${{ github.sha }} -X ${{ env.PKG }}/commons.buildDate=${{ env.BUILD_DATE }}" + + update-releases: + name: Update Release Go Binary + runs-on: ubuntu-20.04 + needs: releases-matrix + steps: + - uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.x' # Specify the version of Python to use + + - name: Install dependencies + run: python -m pip install --upgrade pip && pip install requests + + - name: Run Python script + run: | + python .github/workflows/update_release_table.py ${{ github.ref_name }} + env: + GITHUB_TOKEN: ${{ secrets.RELEASE_GITHUB_TOKEN }} + \ No newline at end of file diff --git a/.github/workflows/update_release_table.py b/.github/workflows/update_release_table.py new file mode 100644 index 0000000..a064580 --- /dev/null +++ b/.github/workflows/update_release_table.py @@ -0,0 +1,125 @@ +import requests +import argparse +import os +import re + +# GitHub repository details +REPO = "cyverse/gocommands" +API_URL = f"https://api.github.com/repos/{REPO}/releases" + +def get_releases_data(): + headers = { + 'Authorization': f'token {os.getenv("GITHUB_TOKEN")}', + 'Accept': 'application/vnd.github.v3+json', + } + response = requests.get(API_URL, headers=headers) + response.raise_for_status() + return response.json() + +def find_release_by_version(releases, version): + for release in releases: + if release["tag_name"] == version: + return release + return None + +def extract_os_arch_from_filename(filename): + # Assuming filenames follow the pattern: ---. + pattern = r'[-]([a-zA-Z]+)[-]([a-zA-Z0-9_]+)[.]' + + match = re.search(pattern, filename) + + if match: + os = match.group(1) + arch = match.group(2) + + if os == "darwin": + os = "MacOS" + elif os == "linux": + os = "Linux" + elif os == "windows": + os = "Windows" + + if arch == "amd64": + arch = "Intel/AMD 64-bit" + elif arch == "386": + arch = "Intel/AMD 32-bit" + elif arch == "arm64": + if os == "MacOS": + arch = "M1/M2/M3 (ARM 64-bit)" + else: + arch = "ARM 64-bit" + elif arch == "arm": + arch = "ARM 32-bit" + + return os, arch + else: + return None, None + +def generate_markdown_table(release): + table = "### Release Assets\n" + table += "| OS | Architecture | Link |\n" + table += "|---------|----------|-------------|\n" + + for asset in release["assets"]: + if not asset["name"].endswith(".md5"): + # parse + os, arch = extract_os_arch_from_filename(asset["name"]) + download_url = asset["browser_download_url"] + table += f"| {os} | {arch} | [Download]({download_url}) |\n" + + return table + +def update_release_body(release_id, new_body): + headers = { + 'Authorization': f'token {os.getenv("GITHUB_TOKEN")}', + 'Accept': 'application/vnd.github.v3+json', + } + update_url = f"https://api.github.com/repos/{REPO}/releases/{release_id}" + data = { + "body": new_body + } + + response = requests.patch(update_url, headers=headers, json=data) + response.raise_for_status() + +""" +def update_readme(table): + with open("README.md", "r") as file: + lines = file.readlines() + + with open("README.md", "w") as file: + inside_table = False + for line in lines: + if line.startswith("| Version"): + inside_table = True + file.write(table) + continue + if inside_table and line.startswith("|"): + continue + file.write(line) +""" + +def main(target_version): + releases = get_releases_data() + release_to_update = find_release_by_version(releases, target_version) + + if release_to_update: + release_id = release_to_update["id"] + current_body = release_to_update.get("body", "") + + markdown_table = generate_markdown_table(release_to_update) + print(markdown_table) + + if "### Release Assets\n" not in current_body: + updated_body = current_body + "\n\n" + markdown_table + update_release_body(release_id, updated_body) + print(f"Release {target_version} updated successfully.") + else: + print(f"Release with version {target_version} not found.") + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Update a specific release in GitHub.") + parser.add_argument("version", help="The tag name of the release to update") + args = parser.parse_args() + + main(args.version) \ No newline at end of file