11import github
22from github import Github
33import os ,sys ,platform ,base64 ,time ,re
4- import requests
54
6- # Intializing the Variables
5+ # Initializing the Variables
76BOT_TOKEN = os .environ .get ('CONCORE_BOT_TOKEN' , '' )
87
98# Fix 1: Fail fast if token is missing
1211 sys .exit (1 )
1312
1413# Fix 2: Token format validation
15- token_pattern = r"^(ghp_|github_pat_)[A-Za-z0-9_]{20,}$"
14+ token_pattern = r"^(( ghp_|github_pat_|ghs_ )[A-Za-z0-9_]{20,}|[0-9a-fA-F]{40}) $"
1615if not re .match (token_pattern , BOT_TOKEN ):
1716 print ("Error: Invalid GitHub token format." )
1817 sys .exit (1 )
@@ -32,25 +31,21 @@ def checkInputValidity():
3231 print ("Please Provide necessary Inputs" )
3332 exit (1 )
3433 if not os .path .isdir (STUDY_NAME_PATH ):
35- print ("Directory doesnot Exists.Invalid Path" )
34+ print ("Directory does not Exists.Invalid Path" )
3635 exit (1 )
3736
38- # Fix 5: Retry + backoff wrapper for GitHub API requests
39- def github_request (method , url , headers = None , json = None , retries = 3 ):
37+ # Fix 5: Retry + backoff wrapper for PyGithub operations
38+ def with_retry (operation , retries = 3 ):
39+ """Retry wrapper for PyGithub operations with exponential backoff."""
4040 for attempt in range (retries ):
4141 try :
42- response = requests .request (method , url , headers = headers , json = json , timeout = 30 )
43- if response .status_code == 429 or response .status_code >= 500 :
42+ return operation ()
43+ except github .GithubException as e :
44+ if (e .status == 429 or e .status >= 500 ) and attempt < retries - 1 :
4445 wait_time = 2 ** attempt
4546 time .sleep (wait_time )
4647 continue
47- return response
48- except requests .exceptions .ConnectionError :
49- print ("Network error while contacting GitHub API." )
50- sys .exit (1 )
51- except requests .exceptions .Timeout :
52- print ("GitHub API request timed out." )
53- sys .exit (1 )
48+ raise
5449 print ("Error: GitHub API request failed after retries." )
5550 sys .exit (1 )
5651
@@ -62,11 +57,11 @@ def anyOpenPR(upstream_repo):
6257 try :
6358 prs = upstream_repo .get_pulls (state = 'open' , head = f'{ BOT_ACCOUNT } :{ BRANCH_NAME } ' )
6459 return prs [0 ] if prs .totalCount > 0 else None
65- except requests . exceptions . ConnectionError :
66- print ( "Network error while fetching PR status." )
67- exit ( 1 )
68- except requests . exceptions . Timeout :
69- print ("Request timed out while fetching PR status." )
60+ except github . GithubException as e :
61+ if e . status == 429 or e . status >= 500 :
62+ print ( "GitHub API rate limit or server error while fetching PR status." )
63+ else :
64+ print ("Unable to fetch PR status. Try again later ." )
7065 exit (1 )
7166 except Exception :
7267 print ("Unable to fetch PR status. Try again later." )
@@ -81,8 +76,8 @@ def commitAndUpdateRef(repo,tree_content,commit,branch):
8176 exit (1 )
8277 ref = repo .get_git_ref ("heads/" + branch .name )
8378 ref .edit (new_commit .sha ,True )
84- except requests . exceptions . HTTPError as e :
85- print (f"GitHub API error: { e .response . status_code } " )
79+ except github . GithubException as e :
80+ print (f"GitHub API error: { e .status } " )
8681 exit (1 )
8782 except Exception :
8883 print ("Failed to upload your example. Please try after some time." ,end = "" )
@@ -103,8 +98,8 @@ def runWorkflow(repo,upstream_repo):
10398 inputs = {'title' : f"[BOT]: { PR_TITLE } " , 'body' : PR_BODY , 'upstreamRepo' : UPSTREAM_ACCOUNT , 'botRepo' : BOT_ACCOUNT , 'repo' : REPO_NAME }
10499 )
105100 printPRStatus (upstream_repo )
106- except requests . exceptions . HTTPError as e :
107- print (f"GitHub API error while triggering workflow: { e .response . status_code } " )
101+ except github . GithubException as e :
102+ print (f"GitHub API error while triggering workflow: { e .status } " )
108103 exit (1 )
109104 except Exception :
110105 print ("Error triggering workflow. Try again later." )
@@ -161,11 +156,8 @@ def remove_prefix(text, prefix):
161156 except github .GithubException :
162157 print (f"No Branch is available with the name { BRANCH_NAME } " )
163158 is_present = False
164- except requests .exceptions .ConnectionError :
165- print ("Network error during GitHub authentication." )
166- exit (1 )
167- except requests .exceptions .Timeout :
168- print ("GitHub authentication request timed out." )
159+ except github .GithubException as e :
160+ print (f"GitHub API error during authentication: { e .status } " )
169161 exit (1 )
170162except Exception :
171163 print ("Authentication failed" , end = "" )
@@ -200,14 +192,8 @@ def remove_prefix(text, prefix):
200192 appendBlobInTree (repo ,content ,file_path ,tree_content )
201193 commitAndUpdateRef (repo ,tree_content ,base_ref .commit ,branch )
202194 runWorkflow (repo ,upstream_repo )
203- except requests .exceptions .HTTPError as e :
204- print (f"GitHub API error: { e .response .status_code } " )
205- exit (1 )
206- except requests .exceptions .ConnectionError :
207- print ("Network error while uploading study." )
208- exit (1 )
209- except requests .exceptions .Timeout :
210- print ("Request timed out while uploading study." )
195+ except github .GithubException as e :
196+ print (f"GitHub API error: { e .status } " )
211197 exit (1 )
212198except Exception :
213199 print ("Some error occurred. Please try again after some time." ,end = "" )
0 commit comments