-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch_main_dashboard.py
177 lines (134 loc) · 4.98 KB
/
fetch_main_dashboard.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python3
import json
import sys
from functools import cache
from pathlib import Path
from typing import Any, Tuple, Optional
import multiprocessing
import requests
import toml
import tqdm
from github import Github
from tqdm.contrib.logging import logging_redirect_tqdm
sys.path.append(Path(__file__).resolve().parent)
from utils import get_catalog
APPSTORE_PATH = Path(__file__).resolve().parent
@cache
def config() -> dict[str, Any]:
try:
config = toml.loads((APPSTORE_PATH / "config.toml").read_text())
return config
except Exception:
raise RuntimeError(
"You should create a config.toml with the appropriate key/values, cf config.toml.example"
)
@cache
def github_api() -> Github:
github_token = config().get("GITHUB_TOKEN")
if github_token is None:
raise RuntimeError("You should add a GITHUB_TOKEN to config.toml")
return Github(github_token)
@cache
def catalog() -> dict:
return get_catalog()
@cache
def ci_apps_bullseye_results() -> dict:
return requests.get(
"https://ci-apps-bullseye.yunohost.org/ci/api/results", timeout=60
).json()
@cache
def ci_apps_bookworm_results() -> dict:
return requests.get(
"https://ci-apps.yunohost.org/ci/api/results", timeout=60
).json()
@cache
def ci_apps_trixie_results() -> dict:
return requests.get(
"https://ci-apps-trixie.yunohost.org/ci/api/results", timeout=60
).json()
def get_app_ci_results(results: dict[str, dict], name: str) -> Optional[dict]:
app_results = results.get(name)
if app_results:
return {
"level": app_results["level"],
"timestamp": app_results["timestamp"],
}
else:
return {}
def get_github_infos(github_orga_and_repo: str) -> Tuple[str, dict]:
repo = github_api().get_repo(github_orga_and_repo)
infos = {}
pulls = [p for p in repo.get_pulls()]
infos["nb_prs"] = len(pulls)
infos["nb_issues"] = repo.open_issues_count - infos["nb_prs"]
testings = [p for p in pulls if p.head.ref == "testing"]
testing = testings[0] if testings else None
ci_auto_updates = [p for p in pulls if p.head.ref.startswith("ci-auto-update")]
ci_auto_update = (
sorted(ci_auto_updates, key=lambda p: p.created_at, reverse=True)[0]
if ci_auto_updates
else None
)
for p in ([testing] if testing else []) + (
[ci_auto_update] if ci_auto_update else []
):
if p.head.label != "YunoHost-Apps:testing" and not (
p.user.login == "yunohost-bot" and p.head.ref.startswith("ci-auto-update-")
):
continue
infos["testing" if p.head.ref == "testing" else "ci-auto-update"] = {
"branch": p.head.ref,
"url": p.html_url,
"timestamp_created": int(p.created_at.timestamp()),
"timestamp_updated": int(p.updated_at.timestamp()),
"statuses": [
{
"state": s.state,
"context": s.context,
"url": s.target_url,
"timestamp": int(s.updated_at.timestamp()),
}
for s in repo.get_commit(p.head.sha).get_combined_status().statuses
],
}
return repo.name, infos
def get_consolidated_infos(name_and_infos: Tuple[str, dict]) -> Tuple[str, dict]:
name, infos = name_and_infos
if infos["state"] != "working":
return None
data = {
"public_level": infos["level"],
"url": infos["git"]["url"],
"timestamp_latest_commit": infos["lastUpdate"],
"maintainers": infos["manifest"]["maintainers"],
"antifeatures": infos["antifeatures"],
"packaging_format": infos["manifest"]["packaging_format"],
"ci_results": {
"bullseye": get_app_ci_results(ci_apps_bullseye_results(), name),
"bookworm": get_app_ci_results(ci_apps_bookworm_results(), name),
"trixie": get_app_ci_results(ci_apps_trixie_results(), name),
},
}
if infos["git"]["url"].lower().startswith("https://github.com/"):
org_and_name = infos["git"]["url"].lower().replace("https://github.com/", "")
_, github_infos = get_github_infos(org_and_name)
data.update(github_infos)
return name, data
def main() -> None:
consolidated_infos = {}
with multiprocessing.Pool(processes=10) as pool:
with logging_redirect_tqdm():
tasks = pool.imap(get_consolidated_infos, catalog()["apps"].items())
for result in tqdm.tqdm(
tasks, total=len(catalog()["apps"].keys()), ascii=" ·#"
):
if result is None:
continue
name, infos = result
if infos is None:
continue
consolidated_infos[name] = infos
dashboard_file = APPSTORE_PATH / ".cache" / "dashboard.json"
dashboard_file.write_text(json.dumps(consolidated_infos))
if __name__ == "__main__":
main()