PluralKit/ci/run_ci.py

110 lines
2.8 KiB
Python
Raw Normal View History

2024-09-05 18:47:16 +09:00
#!/usr/bin/env python3
2024-09-06 16:11:14 +09:00
import os, sys, json, subprocess, random, time, datetime
import urllib.request
2024-09-06 12:53:24 +09:00
2024-09-06 16:34:56 +09:00
global_fail = False
2024-09-06 12:53:24 +09:00
def must_get_env(name):
val = os.environ.get(name)
if val == "":
raise "meow"
return val
def docker_build(data):
# file="", tags=[], root="/"
pass
2024-09-06 16:11:14 +09:00
def take_some_time():
time.sleep(random.random() * 10)
2024-09-06 16:46:23 +09:00
def report_status(sha, name, start_time, exit=None):
2024-09-06 16:11:14 +09:00
status=""
match exit:
case None:
status = "in_progress"
case True:
status = "success"
case False:
status = "failure"
data = {
'name': name,
2024-09-06 16:46:23 +09:00
'head_sha': sha,
2024-09-06 16:11:14 +09:00
'status': status,
'started_at': start_time,
'output': {
'title': name,
2024-09-06 16:14:27 +09:00
'summary': f"Check logs at {must_get_env("ACTION_LOGS_URL")}",
2024-09-06 16:11:14 +09:00
'text': "[]",
'annotations': []
},
2024-09-06 12:53:24 +09:00
}
2024-09-06 16:11:14 +09:00
if exit is not None:
data['completed_at'] = datetime.datetime.now(tz=datetime.timezone.utc).isoformat(timespec='seconds')
req = urllib.request.Request(
f"https://api.github.com/repos/pluralkit/pluralkit/check-runs",
method='POST',
headers={
'Accept': 'application/vnd.github+json',
'Authorization': f'Bearer {must_get_env("GITHUB_APP_TOKEN")}',
'content-type':'application/json'
},
2024-09-06 16:25:25 +09:00
data=bytes(json.dumps(data), 'UTF-8')
2024-09-06 16:11:14 +09:00
)
try:
2024-09-06 16:25:25 +09:00
with urllib.request.urlopen(req) as response:
2024-09-06 16:11:14 +09:00
response_code = response.getcode()
response_data = response.read()
print(f"{response_code} updated status {data}: {response_data}")
except urllib.error.HTTPError as e:
response_code = e.getcode()
response_data = e.read()
print(f"{response_code} failed to update status {name}: {response_data}")
2024-09-06 16:38:39 +09:00
global global_fail
2024-09-06 16:34:56 +09:00
global_fail = True
2024-09-06 16:11:14 +09:00
def run_job(data):
2024-09-06 16:28:49 +09:00
subprocess.run(["git", "clone", must_get_env("REPO_URL")])
2024-09-06 16:11:14 +09:00
os.chdir(os.path.basename(must_get_env("REPO_URL")))
2024-09-06 16:50:10 +09:00
subprocess.run(["git", "checkout", data['sha']])
2024-09-06 16:11:14 +09:00
# run actual job
take_some_time()
2024-09-06 12:53:24 +09:00
2024-09-06 13:05:07 +09:00
def main():
2024-09-06 12:53:24 +09:00
print("hello from python!")
2024-09-06 16:11:14 +09:00
dispatch_data = os.environ.get("DISPATCH_DATA")
2024-09-06 13:00:50 +09:00
if dispatch_data == "":
2024-09-06 16:11:14 +09:00
print("no data!")
return 1
2024-09-06 12:53:24 +09:00
data = json.loads(dispatch_data)
2024-09-06 16:11:14 +09:00
print("running {dispatch_data}")
time_started = datetime.datetime.now(tz=datetime.timezone.utc).isoformat(timespec='seconds')
2024-09-06 16:50:10 +09:00
report_status(data['sha'], data["action"], time_started)
2024-09-06 16:11:14 +09:00
ok = True
try:
run_job(data)
except Exception:
ok = False
print("job failed!")
traceback.format_exc()
2024-09-06 16:38:39 +09:00
global global_fail
2024-09-06 16:34:56 +09:00
if global_fail:
ok = False
2024-09-06 16:50:10 +09:00
report_status(data['sha'], data["action"], time_started, ok)
2024-09-06 16:11:14 +09:00
return 0 if ok else 1
2024-09-06 13:05:07 +09:00
if __name__ == "__main__":
2024-09-06 13:06:17 +09:00
sys.exit(main())