diff --git a/GitHubRepoAPI.py b/GitHubRepoAPI.py index f6acbda3..9f1b30e9 100644 --- a/GitHubRepoAPI.py +++ b/GitHubRepoAPI.py @@ -191,41 +191,33 @@ def get_forks(self, repo: Repository) -> list[Repository]: return result def get_comments(self, repo, obj) -> list[Comment]: - result = [] - if isinstance(obj, Issue): - # TODO оптимизировать - issues = self.client.get_repo(repo._id).get_issues(state='all') - issue = None - for i in issues: - if i.number == obj._id: - issue = i - break - for c in issue.get_comments(): - result.append( - Comment( - body=c.body, - created_at=c.created_at, - author=self.get_user_data(c.user), - ) - ) - elif isinstance(obj, PullRequest): - # TODO оптимизировать - pulls = self.client.get_repo(repo._id).get_pulls(state='all') - pull = None - for p in pulls: - if p.number == obj._id: - pull = p - break - for c in pull.get_comments(): - result.append( - Comment( - body=c.body, - created_at=c.created_at, - author=self.get_user_data(c.user.login), - ) + 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 + ] - return result + 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: @@ -280,7 +272,7 @@ def get_workflow_runs(self, repo) -> list[WorkflowRun]: # Точка входа для тестирования if __name__ == "__main__": # Создайте клиент GitHub (используйте ваш токен) - # client = Github("tocken") + # client = Github("") api = GitHubRepoAPI('client') # Укажите ваш репозиторий