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