-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterface_wrapper.py
237 lines (191 loc) · 5.35 KB
/
interface_wrapper.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
import logging
from abc import ABC, abstractmethod
from dataclasses import dataclass
from datetime import datetime
from github import Auth, Github
from pyforgejo import PyforgejoApi
# Настройка логирования
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
# Модельные классы
@dataclass
class Contributor:
username: str
email: str
@dataclass
class User:
_id: int
login: str
username: str
email: str
html_url: str
node_id: str
type: str
bio: str
site_admin: bool
@dataclass
class Commit:
_id: str
message: str
author: User
date: datetime
files: list[str]
additions: int
deletions: int
@dataclass
class Branch:
name: str
last_commit: Commit | None
@dataclass
class Repository:
_id: str
name: str
url: str
default_branch: Branch
owner: User
@dataclass
class Issue:
_id: str
title: str
state: str
created_at: datetime
closed_at: datetime
body: str
user: User
closed_by: User
labels: list[str]
milestone: str
@dataclass
class PullRequest:
_id: int
title: str
author: User
state: str
created_at: datetime
head_label: str
base_label: str
head_ref: str
base_ref: str
merged_by: User
files: list[str]
issue_url: str
labels: list[str]
milestone: str
@dataclass
class Invite:
_id: int
invitee: User
created_at: datetime
html_url: str
@dataclass
class Comment:
body: str
created_at: datetime
author: User
@dataclass
class WikiPage:
title: str
content: str
@dataclass
class WorkflowRun:
display_title: str
event: str
head_branch: str
head_sha: str
name: str
path: str
created_at: datetime
run_started_at: datetime
updated_at: datetime
conclusion: str
status: str
url: str
# Интерфейс API
class IRepositoryAPI(ABC):
@abstractmethod
def get_user_data(self, user) -> User:
pass
@abstractmethod
def get_repository(self, id: str) -> Repository | None:
"""Получить репозиторий по его идентификатору."""
pass
@abstractmethod
def get_collaborator_permission(self, repo: Repository, user: User) -> str:
pass
@abstractmethod
def get_commits(self, repo: Repository, files: bool = True) -> list[Commit]:
"""Получить список коммитов для репозитория."""
pass
@abstractmethod
def get_contributors(self, repo: Repository) -> list[Contributor]:
"""Получить список контрибьюторов для репозитория."""
pass
@abstractmethod
def get_issues(self, repo: Repository) -> list[Issue]:
"""Получить список issues для репозитория."""
pass
@abstractmethod
def get_pull_requests(self, repo: Repository) -> list[PullRequest]:
"""Получить список pull requests для репозитория."""
pass
@abstractmethod
def get_branches(self, repo: Repository) -> list[Branch]:
"""Получить список веток для репозитория."""
pass
@abstractmethod
def get_forks(self, repo: Repository) -> list[Repository]:
pass
@abstractmethod
def get_wiki_pages(self, repo: Repository) -> list[WikiPage]:
"""Получить список wiki-страниц для репозитория."""
pass
@abstractmethod
def get_comments(self, obj) -> list[Comment]:
pass
@abstractmethod
def get_invites(self, repo: Repository) -> list[Invite]:
pass
@abstractmethod
def get_rate_limiting(self) -> tuple[int, int]:
pass
@abstractmethod
def get_workflow_runs(self, repo: Repository) -> list[WorkflowRun]:
pass
# Фабрика для создания API
class RepositoryFactory:
@staticmethod
def create_api(
source: str, token: str, base_url: str | None = None
) -> IRepositoryAPI:
from ForgejoRepoAPI import ForgejoRepoAPI
from GitHubRepoAPI import GitHubRepoAPI
if source == 'github':
return GitHubRepoAPI(Github(auth=Auth.Token(token)))
elif source == 'forgejo':
if not isinstance(base_url, str):
raise ValueError(
f"base_url for PyforgejoApi should be str, got {type(base_url)}"
)
return ForgejoRepoAPI(PyforgejoApi(api_key=token, base_url=base_url))
else:
raise ValueError(f"Unsupported source: {source}")
# Сервис для расчёта метрик
class CommitmentCalculator:
def __init__(self, api: IRepositoryAPI):
self.api = api
def calculate(self, repo: Repository) -> dict[str, int]:
if not repo:
return {}
try:
commits = self.api.get_commits(repo)
if not commits:
return {}
result = {}
for commit in commits:
author = commit.author.username
result[author] = result.get(author, 0) + 1
return result
except Exception as e:
logging.error(f"Failed to calculate commitment for repo {repo.name}: {e}")
return {}