Skip to content

Commit 2e7310e

Browse files
authored
Merge pull request #118 from moevm/103-optimized-pull_request-and-issue
Оптимизированные запросы на извлечение и загрузка проблем с помощью кэша
2 parents 1d79935 + 63426b9 commit 2e7310e

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed

GitHubRepoAPI.py

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -191,41 +191,33 @@ def get_forks(self, repo: Repository) -> list[Repository]:
191191
return result
192192

193193
def get_comments(self, repo, obj) -> list[Comment]:
194-
result = []
195-
if isinstance(obj, Issue):
196-
# TODO оптимизировать
197-
issues = self.client.get_repo(repo._id).get_issues(state='all')
198-
issue = None
199-
for i in issues:
200-
if i.number == obj._id:
201-
issue = i
202-
break
203-
for c in issue.get_comments():
204-
result.append(
205-
Comment(
206-
body=c.body,
207-
created_at=c.created_at,
208-
author=self.get_user_data(c.user),
209-
)
210-
)
211-
elif isinstance(obj, PullRequest):
212-
# TODO оптимизировать
213-
pulls = self.client.get_repo(repo._id).get_pulls(state='all')
214-
pull = None
215-
for p in pulls:
216-
if p.number == obj._id:
217-
pull = p
218-
break
219-
for c in pull.get_comments():
220-
result.append(
221-
Comment(
222-
body=c.body,
223-
created_at=c.created_at,
224-
author=self.get_user_data(c.user.login),
225-
)
194+
repo_client = self.client.get_repo(repo._id)
195+
196+
try:
197+
if isinstance(obj, Issue):
198+
# Получаем issue напрямую по номеру
199+
issue = repo_client.get_issue(number=obj._id)
200+
comments = issue.get_comments()
201+
elif isinstance(obj, PullRequest):
202+
# Получаем PR напрямую по номеру
203+
pull = repo_client.get_pull(number=obj._id)
204+
comments = pull.get_comments()
205+
else:
206+
return []
207+
208+
# Формируем результат
209+
return [
210+
Comment(
211+
body=comment.body,
212+
created_at=comment.created_at,
213+
author=self.get_user_data(comment.user),
226214
)
215+
for comment in comments
216+
]
227217

228-
return result
218+
except Exception as e:
219+
logging.error(f"Failed to get comments for {type(obj).__name__} {obj._id}: {e}")
220+
return []
229221

230222
def get_invites(self, repo: Repository) -> list[Invite]:
231223
try:
@@ -280,7 +272,7 @@ def get_workflow_runs(self, repo) -> list[WorkflowRun]:
280272
# Точка входа для тестирования
281273
if __name__ == "__main__":
282274
# Создайте клиент GitHub (используйте ваш токен)
283-
# client = Github("tocken")
275+
# client = Github("")
284276
api = GitHubRepoAPI('client')
285277

286278
# Укажите ваш репозиторий

0 commit comments

Comments
 (0)