-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhyperstellar.py
102 lines (85 loc) · 2.97 KB
/
hyperstellar.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
from urllib import request
import urllib
import json
import os
from zipfile import ZipFile
try:
os.environ['DOTNET_ROOT'] = os.environ['HOME'] + '/.dotnet'
except KeyError:
pass
RUNS_URL = 'https://api.github.com/repos/TCLRainbow/Hyperstellar/actions/workflows/82482571/runs'
BRANCH = 'main'
def explode(cond, msg):
if cond:
print('ERROR: ' + msg)
os._exit(1)
def build_artifact_url(run_id):
return f'https://api.github.com/repos/TCLRainbow/Hyperstellar/actions/runs/{run_id}/artifacts'
with open('hyperstellar-token.txt') as f:
token, current_title = f.readlines()
token = token.rstrip('\n')
req = request.Request(RUNS_URL)
with request.urlopen(req) as resp:
result = json.loads(resp.read())
run_count = result['total_count']
explode(run_count == 0, 'No workflow runs!')
print(f'There are {run_count} runs')
# Get latest run from branch
for run in result['workflow_runs']:
if run['head_branch'] == BRANCH:
break
gha_title = run['display_title']
run_branch = run['head_branch']
print(f'Checking run ({run_branch}): {gha_title}')
explode(run['status'] != 'completed', 'Run is not completed!')
if gha_title == current_title:
print('Already downloaded.')
else:
req = request.Request(run['jobs_url'])
with request.urlopen(req) as resp:
result = json.loads(resp.read())
jobs = result['jobs']
build_ok = False
for job in jobs:
if job['name'] == 'build':
build_ok = True
explode(job['conclusion'] != 'success', 'Build failed!')
break
explode(not build_ok, 'No build job found!')
explode(run['pull_requests'], 'Triggered by PR')
run_id = run['id']
print('Checking artifact')
req = request.Request(build_artifact_url(run_id))
with request.urlopen(req) as resp:
result = json.loads(resp.read())
run_count = result['total_count']
explode(run_count == 0, 'No artifacts found!')
artifact = result['artifacts'][0]
explode(artifact['name'] != 'Bot', 'Artifact name not Bot!')
download_url = artifact['archive_download_url']
req = request.Request(download_url, headers={'Authorization': f'Bearer {token}'})
os.chdir('./Hyperstellar')
print(f'Downloading {download_url}')
try:
with request.urlopen(req) as resp:
with open('bot.zip', 'wb') as f:
f.write(resp.read())
except urllib.error.HTTPError as e:
redirected_url = e.geturl()
print(f'Redirecting to {redirected_url}')
req = request.Request(redirected_url)
with request.urlopen(req) as resp:
with open('bot.zip', 'wb') as f:
f.write(resp.read())
print('Unzipping')
with ZipFile('bot.zip') as f:
f.extractall()
print('Removing zip')
os.remove('bot.zip')
os.chdir('..')
with open('hyperstellar-token.txt', 'w') as f:
f.write(f'{token}\n{gha_title}')
print('Updated token.txt')
os.chdir('./Hyperstellar')
print('Running')
os.execv('Bot', ['./Bot'])