This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate_badge.py
108 lines (67 loc) · 2.86 KB
/
update_badge.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
107
108
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# MIT License
# Copyright (c) 2021 MingMoe
import json
import sys
import urllib.request
TARGET_FILE = "README.md"
AUTO_LIST_BEGIN = "<!---ubadge-auto-list-begin-->"
AUTO_LIST_END = "<!---ubadge-auto-list-end-->"
# 获取参数
if(len(sys.argv) != 2):
print("Received wrong arguments!");
sys.exit(1);
check_suite_url = sys.argv[1];
print(f"check suite url:\n{check_suite_url}\n");
print(f"in file:\n{TARGET_FILE}\n");
print(f"from:\n{AUTO_LIST_BEGIN}\n");
print(f"to:\n{AUTO_LIST_END}\n");
# 获取check_suite信息
check_suite_info = urllib.request.urlopen(check_suite_url);
check_suite_json = json.load(check_suite_info)
# 获取check_runs的url
jobs_url = check_suite_json["check_runs_url"]
print(f"check runs url:\n{jobs_url}\n");
# 读取check_runs
contents = urllib.request.urlopen(jobs_url).read();
all_jobs = json.loads(contents)['check_runs'];
print(f"all check runs:\n{all_jobs}");
# 生成一个markdown列表
def generate_markdown_list(texts,jobs):
begin_index = texts.find(AUTO_LIST_BEGIN);
end_index = texts.find(AUTO_LIST_END);
result = "\n| Checking | Status |\n| :-------:|:------:|\n"
for job in jobs:
CHECK_NAME = job['name']
CHECK_STATUS = job['conclusion']
result += "| " + CHECK_NAME + " | ![" + CHECK_NAME + "](";
if (CHECK_STATUS == "success"):
result += "https://img.shields.io/badge/build-passing-green?style=for-the-badge&logo=githubactions&logoColor=white";
elif (CHECK_STATUS == "failure"):
result += "https://img.shields.io/badge/build-failed-red?style=for-the-badge&logo=githubactions&logoColor=white";
elif (CHECK_STATUS == "cancelled"):
result += "https://img.shields.io/badge/build-cancelled-lightgrey?style=for-the-badge&logo=githubactions&logoColor=white";
elif (CHECK_STATUS == "skipped"):
result += "https://img.shields.io/badge/build-skipped-lightgrey?style=for-the-badge&logo=githubactions&logoColor=white";
elif (CHECK_STATUS == "timed_out"):
result += "https://img.shields.io/badge/build-timed_out-lightgrey?style=for-the-badge&logo=githubactions&logoColor=white";
else:
result += "https://img.shields.io/badge/build-unknown_status-lightgrey?style=for-the-badge&logo=githubactions&logoColor=white";
result += ") |\n"
# 替换
texts = texts[:begin_index+len(AUTO_LIST_BEGIN)] + result + texts[end_index:];
return texts;
# 读取目标文件
output = "";
with open(TARGET_FILE, "r") as f:
output = f.read();
# 处理jobs
#for job in all_jobs:
# print(f"process job:`{job['name']}` status {job['conclusion']}");
# output = process_text(output,job['name'],job['conclusion']);
# 生成列表
output = generate_markdown_list(output,all_jobs);
# 写入文件
with open(TARGET_FILE, "w") as f:
f.write(output);