From e5479c4b4f4899494289545001d4a2dc32d61776 Mon Sep 17 00:00:00 2001 From: Pythonic-Rainbow Date: Sat, 28 Sep 2024 03:20:42 +0800 Subject: [PATCH] Finally fixed gha-4 --- Bot/hyperstellar.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/Bot/hyperstellar.py b/Bot/hyperstellar.py index d876043..1b0d87e 100644 --- a/Bot/hyperstellar.py +++ b/Bot/hyperstellar.py @@ -1,4 +1,5 @@ from urllib import request +import urllib import json import os from zipfile import ZipFile @@ -9,6 +10,7 @@ pass RUNS_URL = 'https://api.github.com/repos/TCLRainbow/Hyperstellar/actions/workflows/82482571/runs' +BRANCH = 'main' def explode(cond, msg): @@ -32,10 +34,8 @@ def build_artifact_url(run_id): explode(run_count == 0, 'No workflow runs!') print(f'There are {run_count} runs') -BRANCH = 'gha-4' - -for i in range(run_count): - run = result['workflow_runs'][i] +# Get latest run from branch +for run in result['workflow_runs']: if run['head_branch'] == BRANCH: break @@ -58,7 +58,6 @@ def build_artifact_url(run_id): explode(job['conclusion'] != 'success', 'Build failed!') break explode(not build_ok, 'No build job found!') - # explode(run['head_branch'] != 'main', 'Branch not main') explode(run['pull_requests'], 'Triggered by PR') run_id = run['id'] @@ -72,18 +71,21 @@ def build_artifact_url(run_id): explode(artifact['name'] != 'Bot', 'Artifact name not Bot!') download_url = artifact['archive_download_url'] - print(download_url) - # download_url = f"https://github.com/TCLRainbow/Hyperstellar/actions/runs/11033362227/artifacts/1976833830" - - headers = {'Authorization': f'Bearer {token}', - 'User-Agent': 'insomnia/10.0', - 'header': 'application/vnd.github+json'} - req = request.Request(download_url, headers=headers) + req = request.Request(download_url, headers={'Authorization': f'Bearer {token}'}) os.chdir('./Hyperstellar') - print('Downloading') - with request.urlopen(req) as resp: - with open('bot.zip', 'wb') as f: - f.write(resp.read()) + 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()