-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_bundles.py
106 lines (90 loc) · 4.49 KB
/
generate_bundles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import asyncio
import json
import subprocess
import os
from httpx import AsyncClient, Timeout
GH_PAT = os.getenv('GH_PAT')
async def get_latest_release(repo_url, prerelease, latest_flag=False):
async def get_version_urls(release):
version = release['tag_name']
patches_url = None
integrations_url = None
for asset in release["assets"]:
if asset["browser_download_url"].endswith(".jar") or asset["browser_download_url"].endswith(".rvp"):
patches_url = asset['browser_download_url']
elif asset["browser_download_url"].endswith(".apk"):
integrations_url = asset['browser_download_url']
return version, patches_url, integrations_url
api_url = f"{repo_url}/releases"
headers = {'Authorization': f'token {GH_PAT}'}
timeout = Timeout(connect=None, read=None, write=None, pool=None)
async with AsyncClient(timeout=timeout, headers=headers) as client:
response = await client.get(api_url)
if response.status_code == 200:
releases = response.json()
if latest_flag:
target_release = max(releases, key=lambda x: x["published_at"])
elif prerelease:
target_release = max((release for release in releases if release["prerelease"]), key=lambda x: x["published_at"], default=None)
else:
target_release = max((release for release in releases if not release["prerelease"]), key=lambda x: x["published_at"], default=None)
if target_release:
return await get_version_urls(target_release)
else:
print(f"No {'pre' if prerelease else ''}release found for {repo_url}")
return None, None, None
else:
print(f"Failed to fetch releases from {repo_url}")
return None, None, None
async def fetch_release_data(source, repo):
try:
prerelease = repo.get('prerelease', False)
latest_flag = repo.get('latest', False)
patches_version, patches_asset_url, _ = await get_latest_release(repo.get('patches'), prerelease, latest_flag)
integrations_version, _, integration_asset_url = await get_latest_release(repo.get('integration'), prerelease, latest_flag)
if patches_version and patches_asset_url and integrations_version and integration_asset_url:
info_dict = {
"patches": {
"version": patches_version,
"url": patches_asset_url
},
"integrations": {
"version": integrations_version,
"url": integration_asset_url
}
}
base_source = source.replace('-dev', '').replace('-latest', '').replace('-stable', '')
directory = os.path.join('patch-bundles', f"{base_source}-patch-bundles")
os.makedirs(directory, exist_ok=True)
filepath = os.path.join(directory, f'{source}-patches-bundle.json')
with open(filepath, 'w') as file:
json.dump(info_dict, file, indent=2)
print(f"Latest release information saved to {filepath}")
# Stage the changes made to the JSON file
subprocess.run(["git", "add", filepath], check=True)
else:
print(f"Error: Unable to fetch release information for {source}")
except Exception as e:
print(f"Error in fetch_release_data for {source}: {e}")
async def main():
try:
with open('bundle-sources.json') as file:
sources = json.load(file)
# Configure Git user name and email
subprocess.run(["git", "config", "user.email", "41898282+github-actions[bot]@users.noreply.github.com"], check=True)
subprocess.run(["git", "config", "user.name", "github-actions[bot]"], check=True)
# Pull the latest changes from the remote branch
subprocess.run(["git", "pull", "origin", "bundles"], check=True)
for source, repo in sources.items():
await fetch_release_data(source, repo)
await asyncio.sleep(0)
# Commit the changes
subprocess.run(["git", "commit", "-m", "Update patch-bundle.json to latest"], check=True)
# Push the changes to the remote branch
subprocess.run(["git", "push", "origin", "bundles"], check=True)
except subprocess.CalledProcessError as e:
print(f"Subprocess failed: {e}")
except Exception as e:
print(f"Error in main: {e}")
if __name__ == "__main__":
asyncio.run(main())