-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGitHubRepoAPI.py
322 lines (279 loc) · 11 KB
/
GitHubRepoAPI.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from github import Github, GithubException
from interface_wrapper import (
Branch,
Comment,
Commit,
Contributor,
Invite,
IRepositoryAPI,
Issue,
PullRequest,
Repository,
User,
WikiPage,
logging,
WorkflowRun,
)
class GitHubRepoAPI(IRepositoryAPI):
def __init__(self, client: Github):
self.client = self._client_validation(client)
@staticmethod
def _client_validation(client: Github) -> Github:
try:
client.get_user().login
except GithubException as err:
logging.error(f'Github: Connect: error {err.data}')
logging.error(
'Github: Connect: user could not be authenticated please try again.'
)
exit(1)
else:
return client
def get_user_data(self, user) -> User:
return User(
login=user.login,
username=user.name,
email=user.email, # always None
html_url=user.html_url,
node_id=user.node_id,
type=user.type,
bio=user.bio,
site_admin=user.site_admin,
_id=user.id,
)
def get_commit_data(self, commit, files=False) -> Commit:
return Commit(
_id=commit.sha,
message=commit.commit.message,
author=self.get_user_data(commit.author),
date=commit.commit.author.date,
files=[f.filename for f in commit.files] if files else None,
additions=commit.stats.additions,
deletions=commit.stats.deletions,
)
def get_repository(self, id: str) -> Repository | None:
try:
repo = self.client.get_repo(id)
return Repository(
_id=repo.full_name,
name=repo.name,
url=repo.html_url,
default_branch=Branch(name=repo.default_branch, last_commit=None),
owner=self.get_user_data(repo.owner),
)
except Exception as e:
logging.error(f"Failed to get repository {id} from GitHub: {e}")
return None
def get_collaborator_permission(self, repo: Repository, user: User) -> str:
return self.client.get_repo(repo._id).get_collaborator_permission(user.login)
def get_commits(self, repo: Repository, files: bool = True) -> list[Commit]:
try:
commits = self.client.get_repo(repo._id).get_commits()
return [self.get_commit_data(c, files) for c in commits]
except Exception as e:
logging.error(
f"Failed to get commits from GitHub for repo {repo.name}: {e}"
)
return []
def get_contributors(self, repo: Repository) -> list[Contributor]:
try:
contributors = self.client.get_repo(repo._id).get_contributors()
return [Contributor(c.login, c.email or "") for c in contributors]
except Exception as e:
logging.error(
f"Failed to get contributors from GitHub for repo {repo.name}: {e}"
)
return []
def get_issues(self, repo: Repository) -> list[Issue]:
try:
issues = self.client.get_repo(repo._id).get_issues(state='all')
return [
Issue(
_id=i.number,
title=i.title,
state=i.state,
created_at=i.created_at,
closed_at=i.closed_at,
closed_by=self.get_user_data(i.closed_by) if i.closed_by else None,
body=i.body,
user=self.get_user_data(i.user),
labels=[label.name for label in i.labels],
milestone=i.milestone.title if i.milestone else None,
)
for i in issues
]
except Exception as e:
logging.error(f"Failed to get issues from GitHub for repo {repo.name}: {e}")
return []
def get_pull_requests(self, repo: Repository) -> list[PullRequest]:
try:
pulls = self.client.get_repo(repo._id).get_pulls(state='all')
return [
PullRequest(
_id=p.number,
title=p.title,
author=self.get_user_data(p.user),
state=p.state,
created_at=p.created_at,
head_label=p.head.label,
base_label=p.base.label,
head_ref=p.head.ref,
base_ref=p.base.ref,
merged_by=self.get_user_data(p.merged_by) if p.merged_by else None,
files=[file.filename for file in p.get_files()],
issue_url=p.issue_url,
labels=[label.name for label in p.labels],
milestone=p.milestone.title if p.milestone else None,
)
for p in pulls
]
except Exception as e:
logging.error(
f"Failed to get pull requests from GitHub for repo {repo.name}: {e}"
)
return []
def get_branches(self, repo: Repository) -> list[Branch]:
try:
repo_client = self.client.get_repo(repo._id)
branches = repo_client.get_branches()
result = []
for branch in branches:
commit = repo_client.get_commit(branch.commit.sha)
commit_obj = self.get_commit_data(commit)
result.append(Branch(name=branch.name, last_commit=commit_obj))
return result
except Exception as e:
logging.error(
f"Failed to get branches from GitHub for repo {repo.name}: {e}"
)
return []
def get_wiki_pages(self, repo: Repository) -> list[WikiPage]:
return
def get_forks(self, repo: Repository) -> list[Repository]:
repo_client = self.client.get_repo(repo._id)
result = []
for r in repo_client.get_forks():
default_branch = (Branch(name=r.default_branch, last_commit=None),)
owner = (self.get_user_data(r.owner),)
result.append(
Repository(
_id=r.full_name,
name=r.name,
url=r.html_url,
default_branch=default_branch,
owner=owner,
)
)
return result
def get_comments(self, repo, obj) -> list[Comment]:
repo_client = self.client.get_repo(repo._id)
try:
if isinstance(obj, Issue):
# Получаем issue напрямую по номеру
issue = repo_client.get_issue(number=obj._id)
comments = issue.get_comments()
elif isinstance(obj, PullRequest):
# Получаем PR напрямую по номеру
pull = repo_client.get_pull(number=obj._id)
comments = pull.get_comments()
else:
return []
# Формируем результат
return [
Comment(
body=comment.body,
created_at=comment.created_at,
author=self.get_user_data(comment.user),
)
for comment in comments
]
except Exception as e:
logging.error(f"Failed to get comments for {type(obj).__name__} {obj._id}: {e}")
return []
def get_invites(self, repo: Repository) -> list[Invite]:
try:
invites = self.client.get_repo(repo._id).get_pending_invitations()
return [
Invite(
_id=i._id,
invitee=self.get_user_data(i.invitee),
created_at=i.created_at,
html_url=i.html_url,
)
for i in invites
]
except Exception as e:
logging.error(
f"Failed to get invites from GitHub for repo {repo.name}: {e}"
)
return []
def get_rate_limiting(self) -> tuple[int, int]:
return self.client.rate_limiting
def get_workflow_runs(self, repo) -> list[WorkflowRun]:
try:
runs = self.client.get_repo(repo._id).get_workflow_runs()
return [
WorkflowRun(
display_title=r.display_title,
event=r.event,
head_branch=r.head_branch,
head_sha=r.head_sha,
name=r.name,
path=r.path,
created_at=r.created_at,
run_started_at=r.run_started_at,
updated_at=r.updated_at,
conclusion=r.conclusion,
status=r.status,
url=r.url,
)
for r in runs
]
except Exception as e:
logging.error(
f"Failed to get workflow runs from GitHub for repo {repo.name}: {e}"
)
return []
# Точка входа для тестирования
if __name__ == "__main__":
# Создайте клиент GitHub (используйте ваш токен)
# client = Github("")
api = GitHubRepoAPI('client')
# Укажите ваш репозиторий
repo_name = ""
# Получение репозитория
repo = api.get_repository(repo_name)
if not repo:
print("Repository not found.")
exit()
# Вывод информации о репозитории
print(f"Repository: {repo.name}, URL: {repo.url}")
# Получение коммитов
commits = api.get_commits(repo)
print(f"Total commits: {len(commits)}")
for commit in commits[:10]: # Выведем первые 10 коммитов
print(
f"Commit: {commit._id}, Message: {commit.message}, Author: {commit.author.username}"
)
# Получение контрибьюторов
contributors = api.get_contributors(repo)
print(f"Total contributors: {len(contributors)}")
for contributor in contributors:
print(f"Contributor: {contributor.username}, Email: {contributor.email}")
# Получение issues
issues = api.get_issues(repo)
print(f"Total issues: {len(issues)}")
for issue in issues[:10]: # Выведем первые 10 issues
print(f"Issue: {issue._id}, Title: {issue.title}, State: {issue.state}")
# Получение pull requests
pulls = api.get_pull_requests(repo)
print(f"Total pull requests: {len(pulls)}")
for pull in pulls[:10]: # Выведем первые 10 pull requests
print(f"Pull Request: {pull._id}, Title: {pull.title}, State: {pull.state}")
# Получение веток
branches = api.get_branches(repo)
print(f"Total branches: {len(branches)}")
for branch in branches:
print(
f"Branch: {branch.name}, Last Commit: {branch.last_commit._id if branch.last_commit else 'None'}"
)