66import re
77import shutil
88from pathlib import Path
9- from typing import Dict , Optional , Union
9+ from typing import Any , Dict , Optional , Tuple , Union
1010
1111import git
1212import questionary
@@ -105,7 +105,7 @@ def __init__(
105105 with open (template_yaml_path ) as f :
106106 self .config_yml .template = yaml .safe_load (f )
107107 with open (self .config_yml_path , "w" ) as fh :
108- yaml .safe_dump (self .config_yml .model_dump (), fh )
108+ yaml .safe_dump (self .config_yml .model_dump (exclude_none = True ), fh )
109109 log .info (f"Saved pipeline creation settings to '{ self .config_yml_path } '" )
110110 raise SystemExit (
111111 f"Please commit your changes and delete the { template_yaml_path } file. Then run the sync command again."
@@ -120,7 +120,7 @@ def __init__(
120120 requests .auth .HTTPBasicAuth (self .gh_username , os .environ ["GITHUB_AUTH_TOKEN" ])
121121 )
122122
123- def sync (self ):
123+ def sync (self ) -> None :
124124 """Find workflow attributes, create a new template pipeline on TEMPLATE"""
125125
126126 # Clear requests_cache so that we don't get stale API responses
@@ -271,7 +271,7 @@ def make_template_pipeline(self):
271271
272272 self .config_yml .template .force = True
273273 with open (self .config_yml_path , "w" ) as config_path :
274- yaml .safe_dump (self .config_yml .model_dump (), config_path )
274+ yaml .safe_dump (self .config_yml .model_dump (exclude_none = True ), config_path )
275275
276276 try :
277277 pipeline_create_obj = nf_core .pipelines .create .create .PipelineCreate (
@@ -291,7 +291,7 @@ def make_template_pipeline(self):
291291 self .config_yml .template .outdir = "."
292292 # Update nf-core version
293293 self .config_yml .nf_core_version = nf_core .__version__
294- dump_yaml_with_prettier (self .config_yml_path , self .config_yml .model_dump ())
294+ dump_yaml_with_prettier (self .config_yml_path , self .config_yml .model_dump (exclude_none = True ))
295295
296296 except Exception as err :
297297 # Reset to where you were to prevent git getting messed up.
@@ -416,12 +416,8 @@ def close_open_template_merge_prs(self):
416416 list_prs_url = f"https://api.github.com/repos/{ self .gh_repo } /pulls"
417417 with self .gh_api .cache_disabled ():
418418 list_prs_request = self .gh_api .get (list_prs_url )
419- try :
420- list_prs_json = json .loads (list_prs_request .content )
421- list_prs_pp = json .dumps (list_prs_json , indent = 4 )
422- except Exception :
423- list_prs_json = list_prs_request .content
424- list_prs_pp = list_prs_request .content
419+
420+ list_prs_json , list_prs_pp = self ._parse_json_response (list_prs_request )
425421
426422 log .debug (f"GitHub API listing existing PRs:\n { list_prs_url } \n { list_prs_pp } " )
427423 if list_prs_request .status_code != 200 :
@@ -462,12 +458,8 @@ def close_open_pr(self, pr) -> bool:
462458 # Update the PR status to be closed
463459 with self .gh_api .cache_disabled ():
464460 pr_request = self .gh_api .patch (url = pr ["url" ], data = json .dumps ({"state" : "closed" }))
465- try :
466- pr_request_json = json .loads (pr_request .content )
467- pr_request_pp = json .dumps (pr_request_json , indent = 4 )
468- except Exception :
469- pr_request_json = pr_request .content
470- pr_request_pp = pr_request .content
461+
462+ pr_request_json , pr_request_pp = self ._parse_json_response (pr_request )
471463
472464 # PR update worked
473465 if pr_request .status_code == 200 :
@@ -481,6 +473,22 @@ def close_open_pr(self, pr) -> bool:
481473 log .warning (f"Could not close PR ('{ pr_request .status_code } '):\n { pr ['url' ]} \n { pr_request_pp } " )
482474 return False
483475
476+ @staticmethod
477+ def _parse_json_response (response ) -> Tuple [Any , str ]:
478+ """Helper method to parse JSON response and create pretty-printed string.
479+
480+ Args:
481+ response: requests.Response object
482+
483+ Returns:
484+ Tuple of (parsed_json, pretty_printed_str)
485+ """
486+ try :
487+ json_data = json .loads (response .content )
488+ return json_data , json .dumps (json_data , indent = 4 )
489+ except Exception :
490+ return response .content , str (response .content )
491+
484492 def reset_target_dir (self ):
485493 """
486494 Reset the target pipeline directory. Check out the original branch.
0 commit comments