1- from github import Github , GithubException
1+ from github import Github
22
3+ from src .utils import (
4+ log_exceptions ,
5+ )
36from src .interface_wrapper import (
47 Branch ,
58 Comment ,
1215 Repository ,
1316 User ,
1417 WikiPage ,
15- logging ,
1618 WorkflowRun ,
1719)
1820
@@ -22,16 +24,10 @@ def __init__(self, client: Github):
2224 self .client = self ._client_validation (client )
2325
2426 @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
27+ @log_exceptions (default_return = None , message = "Github: Connect: user could not be authenticated. Please try again." )
28+ def _client_validation (client : Github ) -> Github | None :
29+ client .get_user ().login
30+ return client
3531
3632 def get_user_data (self , user ) -> User :
3733 return User (
@@ -57,125 +53,96 @@ def get_commit_data(self, commit, files=False) -> Commit:
5753 deletions = commit .stats .deletions ,
5854 )
5955
56+ @log_exceptions (default_return = None , message = "Failed to get repository from GitHub" )
6057 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
58+ repo = self .client .get_repo (id )
59+ return Repository (
60+ _id = repo .full_name ,
61+ name = repo .name ,
62+ url = repo .html_url ,
63+ default_branch = Branch (name = repo .default_branch , last_commit = None ),
64+ owner = self .get_user_data (repo .owner ),
65+ )
7366
7467 def get_collaborator_permission (self , repo : Repository , user : User ) -> str :
7568 return self .client .get_repo (repo ._id ).get_collaborator_permission (user .login )
7669
70+ @log_exceptions (default_return = [], message = "Failed to get commits from GitHub" )
7771 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 []
72+ commits = self .client .get_repo (repo ._id ).get_commits ()
73+ return [self .get_commit_data (c , files ) for c in commits ]
8774
75+ @log_exceptions (default_return = [], message = "Failed to get contributors from GitHub" )
8876 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 []
77+ contributors = self .client .get_repo (repo ._id ).get_contributors ()
78+ return [Contributor (c .login , c .email or "" ) for c in contributors ]
9779
80+ @log_exceptions (default_return = [], message = "Failed to get issues from GitHub" )
9881 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 []
82+ issues = self .client .get_repo (repo ._id ).get_issues (state = 'all' )
83+ return [
84+ Issue (
85+ _id = i .number ,
86+ title = i .title ,
87+ state = i .state ,
88+ created_at = i .created_at ,
89+ closed_at = i .closed_at ,
90+ closed_by = self .get_user_data (i .closed_by ) if i .closed_by else None ,
91+ body = i .body ,
92+ user = self .get_user_data (i .user ),
93+ labels = [label .name for label in i .labels ],
94+ milestone = i .milestone .title if i .milestone else None ,
95+ )
96+ for i in issues
97+ ]
11998
99+ @log_exceptions (default_return = [], message = "Failed to get pull requests from GitHub" )
120100 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 } "
101+ pulls = self .client .get_repo (repo ._id ).get_pulls (state = 'all' )
102+ return [
103+ PullRequest (
104+ _id = p .number ,
105+ title = p .title ,
106+ author = self .get_user_data (p .user ),
107+ state = p .state ,
108+ created_at = p .created_at ,
109+ head_label = p .head .label ,
110+ base_label = p .base .label ,
111+ head_ref = p .head .ref ,
112+ base_ref = p .base .ref ,
113+ merged_by = self .get_user_data (p .merged_by ) if p .merged_by else None ,
114+ files = [file .filename for file in p .get_files ()],
115+ issue_url = p .issue_url ,
116+ labels = [label .name for label in p .labels ],
117+ milestone = p .milestone .title if p .milestone else None ,
145118 )
146- return []
119+ for p in pulls
120+ ]
147121
122+ @log_exceptions (default_return = [], message = "Failed to get branches from GitHub" )
148123 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 ))
124+ repo_client = self .client .get_repo (repo ._id )
125+ branches = repo_client .get_branches ()
126+ result = []
160127
161- return result
128+ for branch in branches :
129+ commit = repo_client .get_commit (branch .commit .sha )
130+ commit_obj = self .get_commit_data (commit )
131+ result .append (Branch (name = branch .name , last_commit = commit_obj ))
162132
163- except Exception as e :
164- logging .error (
165- f"Failed to get branches from GitHub for repo { repo .name } : { e } "
166- )
167- return []
133+ return result
168134
169135 def get_wiki_pages (self , repo : Repository ) -> list [WikiPage ]:
170- return
136+ raise Exception ( 'not implemented' )
171137
138+ @log_exceptions (default_return = [], message = "Failed to get forks from GitHub" )
172139 def get_forks (self , repo : Repository ) -> list [Repository ]:
173140 repo_client = self .client .get_repo (repo ._id )
174141 result = []
175142
176143 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 ), )
144+ default_branch = Branch (name = r .default_branch , last_commit = None )
145+ owner = self .get_user_data (r .owner )
179146
180147 result .append (
181148 Repository (
@@ -189,83 +156,64 @@ def get_forks(self, repo: Repository) -> list[Repository]:
189156
190157 return result
191158
159+ @log_exceptions (default_return = [], message = "Failed to get comments" )
192160 def get_comments (self , repo , obj ) -> list [Comment ]:
193161 repo_client = self .client .get_repo (repo ._id )
194162
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 } " )
163+ if isinstance (obj , Issue ):
164+ issue = repo_client .get_issue (number = obj ._id )
165+ comments = issue .get_comments ()
166+ elif isinstance (obj , PullRequest ):
167+ pull = repo_client .get_pull (number = obj ._id )
168+ comments = pull .get_comments ()
169+ else :
219170 return []
220171
172+ return [
173+ Comment (
174+ body = comment .body ,
175+ created_at = comment .created_at ,
176+ author = self .get_user_data (comment .user ),
177+ )
178+ for comment in comments
179+ ]
180+
181+ @log_exceptions (default_return = [], message = "Failed to get invites from GitHub" )
221182 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 } "
183+ invites = self .client .get_repo (repo ._id ).get_pending_invitations ()
184+ return [
185+ Invite (
186+ _id = i ._id ,
187+ invitee = self .get_user_data (i .invitee ),
188+ created_at = i .created_at ,
189+ html_url = i .html_url ,
236190 )
237- return []
191+ for i in invites
192+ ]
238193
239194 def get_rate_limiting (self ) -> tuple [int , int ]:
240195 return self .client .rate_limiting
241196
197+ @log_exceptions (default_return = [], message = "Failed to get workflow runs from GitHub" )
242198 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 } "
199+ runs = self .client .get_repo (repo ._id ).get_workflow_runs ()
200+ return [
201+ WorkflowRun (
202+ display_title = r .display_title ,
203+ event = r .event ,
204+ head_branch = r .head_branch ,
205+ head_sha = r .head_sha ,
206+ name = r .name ,
207+ path = r .path ,
208+ created_at = r .created_at ,
209+ run_started_at = r .run_started_at ,
210+ updated_at = r .updated_at ,
211+ conclusion = r .conclusion ,
212+ status = r .status ,
213+ url = r .url ,
267214 )
268- return []
215+ for r in runs
216+ ]
269217
270218 def get_base_url (self ) -> str :
271219 return 'https://api.github.com'
0 commit comments