Source code for github_search_engine.clients.github_client_manager

  1from githubkit import GitHub
  2from githubkit.versions.v2022_11_28.models import Issue
  3from githubkit.versions.v2022_11_28.models import IssueComment
  4from githubkit.versions.v2022_11_28.models import TimelineCrossReferencedEvent
  5
  6
[docs] 7class GithubClientManager: 8 def __init__(self, access_token: str): 9 """A GithubClientManager to handle interactions with the GitHub API. 10 11 Initializes a GitHub API client manager. 12 13 Args: 14 access_token: The personal access token used to authenticate with the GitHub API. 15 """
[docs] 16 self.github_client = GitHub( 17 auth=access_token, 18 )
19
[docs] 20 async def get_repository_issues( 21 self, owner: str, repository_name: str 22 ) -> list[Issue]: 23 """Retrieve issues from a specified GitHub repository. 24 25 This function connects to a given GitHub repository and retrieves a list of 26 issues using the GitHub API client. It returns a paginated list of issues 27 associated with the repository. 28 29 Args: 30 owner: The owner of the repository from which to retrieve issues. 31 repository_name: The name of the repository from which to retrieve 32 issues. 33 34 Returns: 35 A paginated list of issues retrieved from the specified 36 repository. 37 """ 38 issues = [] 39 async for issue in self.github_client.paginate( 40 self.github_client.rest.issues.async_list_for_repo, 41 owner=owner, 42 repo=repository_name, 43 state="all", 44 ): 45 issue: Issue 46 47 issues.append(issue) 48 return issues
49
[docs] 50 def get_issue_comments( 51 self, owner: str, repository_name: str, issue_number: int 52 ) -> list[IssueComment]: 53 """Retrieve comments for a specific issue in a repository. 54 55 Args: 56 owner: The owner of the repository. 57 repository_name: The name of the repository. 58 issue_number: The number of the issue. 59 60 Returns: 61 A list of comments for the specified issue. 62 """ 63 return self.github_client.rest.issues.list_comments( 64 owner=owner, 65 repo=repository_name, 66 issue_number=issue_number, 67 ).parsed_data
68
[docs] 69 def get_issue_references( 70 self, 71 owner: str, 72 repository_name: str, 73 issue_number: int, 74 ) -> list[TimelineCrossReferencedEvent]: 75 """Fetch mentions to a given issue. 76 77 Fetches the timeline events for a specific issue and filters out the 78 cross-referenced events. This returns a list of the other issues mentioning 79 the current issue. 80 81 Args: 82 owner: The owner of the repository. 83 repository_name: The name of the repository. 84 issue_number: The number of the issue. 85 86 Returns: 87 A list of cross-referenced events. 88 """ 89 timeline = self.github_client.rest.issues.list_events_for_timeline( 90 owner=owner, 91 repo=repository_name, 92 issue_number=issue_number, 93 ).parsed_data 94 95 cross_reference_events = [ 96 event 97 for event in timeline 98 if isinstance(event, TimelineCrossReferencedEvent) 99 ] 100 return cross_reference_events