-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate a table in release note that shows supported os and arch
- Loading branch information
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: <name>-<version>-<os>-<arch>.<ext> | ||
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) |