-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
170 lines (153 loc) · 5.04 KB
/
main.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
import asyncio
import httpx
import os
import toml
from dataclasses import dataclass
from datetime import datetime
from jinja2 import Environment, FileSystemLoader
from typing import Literal
Severity = Literal["red", "yellow", "green"]
Criteria = tuple[str, Severity]
env = Environment(loader=FileSystemLoader("web"))
template = env.get_template("page.jinja2")
@dataclass
class OrganizationConfig:
name: str
token_ref: str
@dataclass
class Config:
organizations: list[OrganizationConfig]
@dataclass
class Repo:
license: str | None
url: str
name: str
topics: list[str] | None
is_fork: bool
description: str | None
is_private: bool
is_archived: bool
visibility: str
created: datetime
updated: datetime
@property
def license_check(self) -> Criteria:
if self.license:
return self.license, "green"
else:
return "No license", "red"
@property
def topics_check(self) -> Criteria:
if self.topics:
return ", ".join(self.topics), "green"
else:
return "No topics", "red"
@property
def visibility_check(self) -> Criteria:
if self.visibility == "public":
return "Public", "green"
elif self.visibility == "internal":
return "Internal", "yellow"
else:
return "Private", "red"
@property
def description_check(self) -> Criteria:
if self.description:
return "Description Available", "green"
else:
return "No description", "red"
@property
def fork_check(self) -> Criteria:
if self.is_fork:
return "Fork", "yellow"
else:
return "Not a fork", "green"
@property
def archived_check(self) -> Criteria:
if self.is_archived:
return "Archived", "yellow"
else:
return "Not archived", "green"
@property
def last_update_days(self) -> int:
return (datetime.now() - self.updated).days
@property
def last_update_check(self) -> Criteria:
days = self.last_update_days
if days < 365:
return f"{days} day(s) ago", "green"
else:
return f"{days} day(s) ago", "yellow"
@dataclass
class Stats:
licenses_ok: int
topics_ok: int
visibility_ok: int
description_ok: int
public_ok: int
@classmethod
def from_repos(cls, repos: list[Repo]) -> "Stats":
licenses_ok = sum(1 for repo in repos if repo.license_check[1] == "green")
topics_ok = sum(1 for repo in repos if repo.topics_check[1] == "green")
visibility_ok = sum(1 for repo in repos if repo.visibility_check[1] == "green")
description_ok = sum(1 for repo in repos if repo.description_check[1] == "green")
public_ok = sum(1 for repo in repos if repo.visibility == "public")
return cls(
licenses_ok=licenses_ok,
topics_ok=topics_ok,
visibility_ok=visibility_ok,
description_ok=description_ok,
public_ok=public_ok,
)
async def repo_from_resp(response) -> Repo:
created = datetime.fromisoformat(response["created_at"].replace("Z", ""))
updated = datetime.fromisoformat(response["updated_at"].replace("Z", ""))
return Repo(
license=response["license"]["name"] if response["license"] else None,
url=response["html_url"],
name=response["name"],
topics=response["topics"] if "topics" in response else None,
is_fork=response["fork"],
description=response["description"],
is_private=response["private"],
visibility=response["visibility"],
is_archived=response["archived"],
created=created,
updated=updated,
)
async def get_org_repos(org: OrganizationConfig) -> list[Repo]:
token = os.environ[org.token_ref]
headers = {"Authorization": f"Bearer {token}"}
repos = []
async with httpx.AsyncClient() as client:
page = 1
while True:
response = await client.get(
f"https://api.github.com/orgs/{org.name}/repos?page={page}&per_page=100",
headers=headers,
)
if response.status_code == 200:
page_repos = response.json()
if not page_repos:
break
repos += [await repo_from_resp(repo) for repo in page_repos]
page += 1
else:
response.raise_for_status()
return repos
if __name__ == "__main__":
with open("config.toml") as f:
data = toml.load(f)
config = Config(
organizations=[
OrganizationConfig(name=org["name"], token_ref=org["token_ref"])
for org in data["organizations"]
]
)
repos = []
for org in config.organizations:
repos += asyncio.run(get_org_repos(org))
repos = sorted(repos, key=lambda repo: repo.name)
out = template.render({"repos": repos})
with open("public/index.html", "w") as f:
f.write(out)