1010import re
1111import requests
1212import requests_cache
13+ import rich
1314import shutil
1415import time
1516
@@ -323,43 +324,66 @@ def make_pull_request(self):
323324 "base" : self .from_branch ,
324325 }
325326
327+ stderr = rich .console .Console (stderr = True , force_terminal = nf_core .utils .rich_force_colors ())
328+
326329 while True :
327- r = requests .post (
328- url = "https://api.github.com/repos/{}/pulls" .format (self .gh_repo ),
329- data = json .dumps (pr_content ),
330- auth = requests .auth .HTTPBasicAuth (self .gh_username , os .environ ["GITHUB_AUTH_TOKEN" ]),
331- )
332330 try :
333- self .gh_pr_returned_data = json .loads (r .content )
334- returned_data_prettyprint = json .dumps (self .gh_pr_returned_data , indent = 4 )
335- r_headers_pp = json .dumps (r .headers , indent = 4 )
336- except :
337- self .gh_pr_returned_data = r .content
338- returned_data_prettyprint = r .content
339- r_headers_pp = r .headers
340-
341- # PR worked
342- if r .status_code == 201 :
343- self .pr_url = self .gh_pr_returned_data ["html_url" ]
344- log .debug (f"GitHub API PR worked:\n { returned_data_prettyprint } \n \n { r_headers_pp } " )
345- log .info (f"GitHub PR created: { self .gh_pr_returned_data ['html_url' ]} " )
346- break
347-
348- # Returned 403 error - too many simultaneous requests
349- # https://github.com/nf-core/tools/issues/911
350- if r .status_code == 403 :
351- log .debug (f"GitHub API PR failed with 403 error:\n { returned_data_prettyprint } \n \n { r_headers_pp } " )
352- wait_time = float (re .sub ("[^0-9]" , "" , str (r .headers .get ("Retry-After" , 0 ))))
353- if wait_time == 0 :
354- log .debug ("Couldn't find 'Retry-After' header, guessing a length of time to wait" )
355- wait_time = random .randrange (10 , 60 )
356- log .warning (f"Got 403 code - probably the abuse protection. Trying again after { wait_time } seconds.." )
357- time .sleep (wait_time )
358-
359- # Something went wrong
360- else :
331+ log .debug ("Submitting PR to GitHub API" )
332+ returned_data_prettyprint = ""
333+ r_headers_pp = ""
334+ with requests_cache .disabled ():
335+ r = requests .post (
336+ url = "https://api.github.com/repos/{}/pulls" .format (self .gh_repo ),
337+ data = json .dumps (pr_content ),
338+ auth = requests .auth .HTTPBasicAuth (self .gh_username , os .environ ["GITHUB_AUTH_TOKEN" ]),
339+ )
340+ try :
341+ self .gh_pr_returned_data = json .loads (r .content )
342+ returned_data_prettyprint = json .dumps (dict (self .gh_pr_returned_data ), indent = 4 )
343+ r_headers_pp = json .dumps (dict (r .headers ), indent = 4 )
344+ except :
345+ self .gh_pr_returned_data = r .content
346+ returned_data_prettyprint = r .content
347+ r_headers_pp = r .headers
348+ log .error ("Could not parse JSON response from GitHub API!" )
349+ stderr .print_exception ()
350+
351+ # Dump the responses to the log just in case..
352+ log .debug (f"PR response from GitHub. Data:\n { returned_data_prettyprint } \n \n Headers:\n { r_headers_pp } " )
353+
354+ # PR worked
355+ if r .status_code == 201 :
356+ self .pr_url = self .gh_pr_returned_data ["html_url" ]
357+ log .debug (f"GitHub API PR worked, return code 201" )
358+ log .info (f"GitHub PR created: { self .gh_pr_returned_data ['html_url' ]} " )
359+ break
360+
361+ # Returned 403 error - too many simultaneous requests
362+ # https://github.com/nf-core/tools/issues/911
363+ if r .status_code == 403 :
364+ log .debug (f"GitHub API PR failed with 403 error" )
365+ wait_time = float (re .sub ("[^0-9]" , "" , str (r .headers .get ("Retry-After" , 0 ))))
366+ if wait_time == 0 :
367+ log .debug ("Couldn't find 'Retry-After' header, guessing a length of time to wait" )
368+ wait_time = random .randrange (10 , 60 )
369+ log .warning (
370+ f"Got 403 code - probably the abuse protection. Trying again after { wait_time } seconds.."
371+ )
372+ time .sleep (wait_time )
373+
374+ # Something went wrong
375+ else :
376+ raise PullRequestException (
377+ f"GitHub API returned code { r .status_code } : \n \n { returned_data_prettyprint } \n \n { r_headers_pp } "
378+ )
379+ # Don't catch the PullRequestException that we raised inside
380+ except PullRequestException :
381+ raise
382+ # Do catch any other exceptions that we hit
383+ except Exception as e :
384+ stderr .print_exception ()
361385 raise PullRequestException (
362- f"GitHub API returned code { r . status_code } : \n \n { returned_data_prettyprint } \n \n { r_headers_pp } "
386+ f"Something went badly wrong - { e } : \n \n { returned_data_prettyprint } \n \n { r_headers_pp } "
363387 )
364388
365389 def close_open_template_merge_prs (self ):
@@ -371,10 +395,11 @@ def close_open_template_merge_prs(self):
371395
372396 # Look for existing pull-requests
373397 list_prs_url = f"https://api.github.com/repos/{ self .gh_repo } /pulls"
374- list_prs_request = requests .get (
375- url = list_prs_url ,
376- auth = requests .auth .HTTPBasicAuth (self .gh_username , os .environ ["GITHUB_AUTH_TOKEN" ]),
377- )
398+ with requests_cache .disabled ():
399+ list_prs_request = requests .get (
400+ url = list_prs_url ,
401+ auth = requests .auth .HTTPBasicAuth (self .gh_username , os .environ ["GITHUB_AUTH_TOKEN" ]),
402+ )
378403 try :
379404 list_prs_json = json .loads (list_prs_request .content )
380405 list_prs_pp = json .dumps (list_prs_json , indent = 4 )
@@ -412,18 +437,20 @@ def close_open_pr(self, pr):
412437 f"This pull-request is now outdated and has been closed in favour of { self .pr_url } \n \n "
413438 f"Please use { self .pr_url } to merge in the new changes from the nf-core template as soon as possible."
414439 )
415- comment_request = requests .post (
416- url = pr ["comments_url" ],
417- data = json .dumps ({"body" : comment_text }),
418- auth = requests .auth .HTTPBasicAuth (self .gh_username , os .environ ["GITHUB_AUTH_TOKEN" ]),
419- )
440+ with requests_cache .disabled ():
441+ comment_request = requests .post (
442+ url = pr ["comments_url" ],
443+ data = json .dumps ({"body" : comment_text }),
444+ auth = requests .auth .HTTPBasicAuth (self .gh_username , os .environ ["GITHUB_AUTH_TOKEN" ]),
445+ )
420446
421447 # Update the PR status to be closed
422- pr_request = requests .patch (
423- url = pr ["url" ],
424- data = json .dumps ({"state" : "closed" }),
425- auth = requests .auth .HTTPBasicAuth (self .gh_username , os .environ ["GITHUB_AUTH_TOKEN" ]),
426- )
448+ with requests_cache .disabled ():
449+ pr_request = requests .patch (
450+ url = pr ["url" ],
451+ data = json .dumps ({"state" : "closed" }),
452+ auth = requests .auth .HTTPBasicAuth (self .gh_username , os .environ ["GITHUB_AUTH_TOKEN" ]),
453+ )
427454 try :
428455 pr_request_json = json .loads (pr_request .content )
429456 pr_request_pp = json .dumps (pr_request_json , indent = 4 )
0 commit comments