-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_helper.py
144 lines (114 loc) · 3.51 KB
/
github_helper.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
from dataclasses import dataclass
import subprocess
import json
from datetime import datetime
from functools import reduce
@dataclass
class Commit:
sha: str
message: str
timestamp: datetime
url: str
def __str__(self) -> str:
return f"{self.timestamp.date()}: {self.message}\n URL: {self.url}"
@dataclass
class Repo:
name: str
description: str
created: datetime
commits: list[Commit]
def __str__(self) -> str:
return f"{self.name}, {self.description},{self.created}\n{self.commits}"
def parse_iso_generous(s: str) -> datetime:
return datetime.strptime(s, "%Y-%m-%dT%H:%M:%Sz")
def gh_api(path, paginate=False, method="GET", **kwargs):
cmd = ["gh", "api", path, "--method", method]
if paginate:
cmd += ["--paginate", "--slurp"]
for key, value in kwargs.items():
cmd += ["--field", f"{key}={value}"]
cmd += ["--cache", "20h"]
# print(" ".join(cmd))
output = subprocess.check_output(cmd, text=True)
parsed = json.loads(output)
if parsed and paginate and path != "graphql":
# merge lists
return reduce(lambda x, y: x + y, parsed, [])
else:
return parsed
def get_user():
user_json = gh_api("/user")
return user_json["login"]
def get_repos():
"""Get user's repos."""
repos_json = gh_api("/user/repos", paginate=True)
repos = [r["full_name"] for r in repos_json]
return repos
def get_main_repos() -> list[Repo]:
"""
Get user's repos but for forks fetch the parent repo.
"""
repos_json = gh_api(
"graphql",
paginate=True,
method="POST",
query="""
query($endCursor: String) {
viewer {
repositories(first: 100, after: $endCursor) {
nodes {
nameWithOwner
description
isFork
createdAt
parent {
nameWithOwner
description
createdAt
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
""",
)
main_repos: list[Repo] = []
for page in repos_json:
nodes = page["data"]["viewer"]["repositories"]["nodes"]
for item in nodes:
root = item
if item["isFork"]:
root = item["parent"]
created = parse_iso_generous(root["createdAt"])
main_repos.append(
Repo(root["nameWithOwner"], root["description"], created, [])
)
return main_repos
def get_commits(
repo: str, author: str, since: datetime, until: datetime
) -> list[Commit]:
"""
gh api /repos/azeemba/azeemba.com/commits --method GET --field author="azeemba" --field per_page=1 --cache 1h
"""
def make_iso(d: datetime):
return d.strftime("%Y-%m-%dT%H:%M:%SZ")
commits_json = gh_api(
f"/repos/{repo}/commits",
paginate=True,
author=author,
since=make_iso(since),
until=make_iso(until),
)
def makeLightCommit(commit):
return Commit(
commit["sha"],
commit["commit"]["message"],
parse_iso_generous(commit["commit"]["author"]["date"]),
commit["html_url"],
)
commits = [makeLightCommit(c) for c in commits_json]
return commits