11from github import Github , GithubException
22
3+ from src .utils import (
4+ log_exceptions ,
5+ )
36from src .interface_wrapper import (
47 Branch ,
58 Comment ,
@@ -22,16 +25,10 @@ def __init__(self, client: Github):
2225 self .client = self ._client_validation (client )
2326
2427 @staticmethod
25- def _client_validation (client : Github ) -> Github :
26- try :
27- client .get_user ().login
28- except GithubException as err :
29- logging .error (f'Github: Connect: error { err .data } ' )
30- logging .error (
31- 'Github: Connect: user could not be authenticated please try again.'
32- )
33- else :
34- return client
28+ @log_exceptions (default_return = None , message = "Github: Connect: user could not be authenticated. Please try again." )
29+ def _client_validation (client : Github ) -> Github | None :
30+ client .get_user ().login
31+ return client
3532
3633 def get_user_data (self , user ) -> User :
3734 return User (
@@ -57,125 +54,97 @@ def get_commit_data(self, commit, files=False) -> Commit:
5754 deletions = commit .stats .deletions ,
5855 )
5956
57+ @log_exceptions (default_return = None , message = "Failed to get repository from GitHub" )
6058 def get_repository (self , id : str ) -> Repository | None :
61- try :
62- repo = self .client .get_repo (id )
63- return Repository (
64- _id = repo .full_name ,
65- name = repo .name ,
66- url = repo .html_url ,
67- default_branch = Branch (name = repo .default_branch , last_commit = None ),
68- owner = self .get_user_data (repo .owner ),
69- )
70- except Exception as e :
71- logging .error (f"Failed to get repository { id } from GitHub: { e } " )
72- return None
59+ repo = self .client .get_repo (id )
60+ return Repository (
61+ _id = repo .full_name ,
62+ name = repo .name ,
63+ url = repo .html_url ,
64+ default_branch = Branch (name = repo .default_branch , last_commit = None ),
65+ owner = self .get_user_data (repo .owner ),
66+ )
7367
7468 def get_collaborator_permission (self , repo : Repository , user : User ) -> str :
7569 return self .client .get_repo (repo ._id ).get_collaborator_permission (user .login )
7670
71+ @log_exceptions (default_return = [], message = "Failed to get commits from GitHub" )
7772 def get_commits (self , repo : Repository , files : bool = True ) -> list [Commit ]:
78- try :
79- commits = self .client .get_repo (repo ._id ).get_commits ()
80- return [self .get_commit_data (c , files ) for c in commits ]
81-
82- except Exception as e :
83- logging .error (
84- f"Failed to get commits from GitHub for repo { repo .name } : { e } "
85- )
86- return []
73+ commits = self .client .get_repo (repo ._id ).get_commits ()
74+ return [self .get_commit_data (c , files ) for c in commits ]
8775
76+ @log_exceptions (default_return = [], message = "Failed to get contributors from GitHub" )
8877 def get_contributors (self , repo : Repository ) -> list [Contributor ]:
89- try :
90- contributors = self .client .get_repo (repo ._id ).get_contributors ()
91- return [Contributor (c .login , c .email or "" ) for c in contributors ]
92- except Exception as e :
93- logging .error (
94- f"Failed to get contributors from GitHub for repo { repo .name } : { e } "
95- )
96- return []
78+ contributors = self .client .get_repo (repo ._id ).get_contributors ()
79+ return [Contributor (c .login , c .email or "" ) for c in contributors ]
9780
81+ @log_exceptions (default_return = [], message = "Failed to get issues from GitHub" )
9882 def get_issues (self , repo : Repository ) -> list [Issue ]:
99- try :
100- issues = self .client .get_repo (repo ._id ).get_issues (state = 'all' )
101- return [
102- Issue (
103- _id = i .number ,
104- title = i .title ,
105- state = i .state ,
106- created_at = i .created_at ,
107- closed_at = i .closed_at ,
108- closed_by = self .get_user_data (i .closed_by ) if i .closed_by else None ,
109- body = i .body ,
110- user = self .get_user_data (i .user ),
111- labels = [label .name for label in i .labels ],
112- milestone = i .milestone .title if i .milestone else None ,
113- )
114- for i in issues
115- ]
116- except Exception as e :
117- logging .error (f"Failed to get issues from GitHub for repo { repo .name } : { e } " )
118- return []
83+ issues = self .client .get_repo (repo ._id ).get_issues (state = 'all' )
84+ return [
85+ Issue (
86+ _id = i .number ,
87+ title = i .title ,
88+ state = i .state ,
89+ created_at = i .created_at ,
90+ closed_at = i .closed_at ,
91+ closed_by = self .get_user_data (i .closed_by ) if i .closed_by else None ,
92+ body = i .body ,
93+ user = self .get_user_data (i .user ),
94+ labels = [label .name for label in i .labels ],
95+ milestone = i .milestone .title if i .milestone else None ,
96+ )
97+ for i in issues
98+ ]
99+
119100
101+ @log_exceptions (default_return = [], message = "Failed to get pull requests from GitHub" )
120102 def get_pull_requests (self , repo : Repository ) -> list [PullRequest ]:
121- try :
122- pulls = self .client .get_repo (repo ._id ).get_pulls (state = 'all' )
123- return [
124- PullRequest (
125- _id = p .number ,
126- title = p .title ,
127- author = self .get_user_data (p .user ),
128- state = p .state ,
129- created_at = p .created_at ,
130- head_label = p .head .label ,
131- base_label = p .base .label ,
132- head_ref = p .head .ref ,
133- base_ref = p .base .ref ,
134- merged_by = self .get_user_data (p .merged_by ) if p .merged_by else None ,
135- files = [file .filename for file in p .get_files ()],
136- issue_url = p .issue_url ,
137- labels = [label .name for label in p .labels ],
138- milestone = p .milestone .title if p .milestone else None ,
139- )
140- for p in pulls
141- ]
142- except Exception as e :
143- logging .error (
144- f"Failed to get pull requests from GitHub for repo { repo .name } : { e } "
103+ pulls = self .client .get_repo (repo ._id ).get_pulls (state = 'all' )
104+ return [
105+ PullRequest (
106+ _id = p .number ,
107+ title = p .title ,
108+ author = self .get_user_data (p .user ),
109+ state = p .state ,
110+ created_at = p .created_at ,
111+ head_label = p .head .label ,
112+ base_label = p .base .label ,
113+ head_ref = p .head .ref ,
114+ base_ref = p .base .ref ,
115+ merged_by = self .get_user_data (p .merged_by ) if p .merged_by else None ,
116+ files = [file .filename for file in p .get_files ()],
117+ issue_url = p .issue_url ,
118+ labels = [label .name for label in p .labels ],
119+ milestone = p .milestone .title if p .milestone else None ,
145120 )
146- return []
121+ for p in pulls
122+ ]
147123
124+ @log_exceptions (default_return = [], message = "Failed to get branches from GitHub" )
148125 def get_branches (self , repo : Repository ) -> list [Branch ]:
149- try :
150- repo_client = self .client .get_repo (repo ._id )
151- branches = repo_client .get_branches ()
152- result = []
153-
154- for branch in branches :
155- commit = repo_client .get_commit (branch .commit .sha )
156-
157- commit_obj = self .get_commit_data (commit )
158-
159- result .append (Branch (name = branch .name , last_commit = commit_obj ))
126+ repo_client = self .client .get_repo (repo ._id )
127+ branches = repo_client .get_branches ()
128+ result = []
160129
161- return result
130+ for branch in branches :
131+ commit = repo_client .get_commit (branch .commit .sha )
132+ commit_obj = self .get_commit_data (commit )
133+ result .append (Branch (name = branch .name , last_commit = commit_obj ))
162134
163- except Exception as e :
164- logging .error (
165- f"Failed to get branches from GitHub for repo { repo .name } : { e } "
166- )
167- return []
135+ return result
168136
169137 def get_wiki_pages (self , repo : Repository ) -> list [WikiPage ]:
170- return
138+ raise Exception ( 'not implemented' )
171139
140+ @log_exceptions (default_return = [], message = "Failed to get forks from GitHub" )
172141 def get_forks (self , repo : Repository ) -> list [Repository ]:
173142 repo_client = self .client .get_repo (repo ._id )
174143 result = []
175144
176145 for r in repo_client .get_forks ():
177- default_branch = ( Branch (name = r .default_branch , last_commit = None ), )
178- owner = ( self .get_user_data (r .owner ), )
146+ default_branch = Branch (name = r .default_branch , last_commit = None )
147+ owner = self .get_user_data (r .owner )
179148
180149 result .append (
181150 Repository (
@@ -189,83 +158,64 @@ def get_forks(self, repo: Repository) -> list[Repository]:
189158
190159 return result
191160
161+ @log_exceptions (default_return = [], message = "Failed to get comments" )
192162 def get_comments (self , repo , obj ) -> list [Comment ]:
193163 repo_client = self .client .get_repo (repo ._id )
194164
195- try :
196- if isinstance (obj , Issue ):
197- # Получаем issue напрямую по номеру
198- issue = repo_client .get_issue (number = obj ._id )
199- comments = issue .get_comments ()
200- elif isinstance (obj , PullRequest ):
201- # Получаем PR напрямую по номеру
202- pull = repo_client .get_pull (number = obj ._id )
203- comments = pull .get_comments ()
204- else :
205- return []
206-
207- # Формируем результат
208- return [
209- Comment (
210- body = comment .body ,
211- created_at = comment .created_at ,
212- author = self .get_user_data (comment .user ),
213- )
214- for comment in comments
215- ]
216-
217- except Exception as e :
218- logging .error (f"Failed to get comments for { type (obj ).__name__ } { obj ._id } : { e } " )
165+ if isinstance (obj , Issue ):
166+ issue = repo_client .get_issue (number = obj ._id )
167+ comments = issue .get_comments ()
168+ elif isinstance (obj , PullRequest ):
169+ pull = repo_client .get_pull (number = obj ._id )
170+ comments = pull .get_comments ()
171+ else :
219172 return []
220173
174+ return [
175+ Comment (
176+ body = comment .body ,
177+ created_at = comment .created_at ,
178+ author = self .get_user_data (comment .user ),
179+ )
180+ for comment in comments
181+ ]
182+
183+ @log_exceptions (default_return = [], message = "Failed to get invites from GitHub" )
221184 def get_invites (self , repo : Repository ) -> list [Invite ]:
222- try :
223- invites = self .client .get_repo (repo ._id ).get_pending_invitations ()
224- return [
225- Invite (
226- _id = i ._id ,
227- invitee = self .get_user_data (i .invitee ),
228- created_at = i .created_at ,
229- html_url = i .html_url ,
230- )
231- for i in invites
232- ]
233- except Exception as e :
234- logging .error (
235- f"Failed to get invites from GitHub for repo { repo .name } : { e } "
185+ invites = self .client .get_repo (repo ._id ).get_pending_invitations ()
186+ return [
187+ Invite (
188+ _id = i ._id ,
189+ invitee = self .get_user_data (i .invitee ),
190+ created_at = i .created_at ,
191+ html_url = i .html_url ,
236192 )
237- return []
193+ for i in invites
194+ ]
238195
239196 def get_rate_limiting (self ) -> tuple [int , int ]:
240197 return self .client .rate_limiting
241198
199+ @log_exceptions (default_return = [], message = "Failed to get workflow runs from GitHub" )
242200 def get_workflow_runs (self , repo ) -> list [WorkflowRun ]:
243- try :
244- runs = self .client .get_repo (repo ._id ).get_workflow_runs ()
245-
246- return [
247- WorkflowRun (
248- display_title = r .display_title ,
249- event = r .event ,
250- head_branch = r .head_branch ,
251- head_sha = r .head_sha ,
252- name = r .name ,
253- path = r .path ,
254- created_at = r .created_at ,
255- run_started_at = r .run_started_at ,
256- updated_at = r .updated_at ,
257- conclusion = r .conclusion ,
258- status = r .status ,
259- url = r .url ,
260- )
261- for r in runs
262- ]
263-
264- except Exception as e :
265- logging .error (
266- f"Failed to get workflow runs from GitHub for repo { repo .name } : { e } "
201+ runs = self .client .get_repo (repo ._id ).get_workflow_runs ()
202+ return [
203+ WorkflowRun (
204+ display_title = r .display_title ,
205+ event = r .event ,
206+ head_branch = r .head_branch ,
207+ head_sha = r .head_sha ,
208+ name = r .name ,
209+ path = r .path ,
210+ created_at = r .created_at ,
211+ run_started_at = r .run_started_at ,
212+ updated_at = r .updated_at ,
213+ conclusion = r .conclusion ,
214+ status = r .status ,
215+ url = r .url ,
267216 )
268- return []
217+ for r in runs
218+ ]
269219
270220 def get_base_url (self ) -> str :
271221 return 'https://api.github.com'
0 commit comments