@@ -336,26 +336,24 @@ def audit(self, audit_settings: types.ConfigSettings) -> list[problem.Problem]:
336336 def to_json (self , export_settings : types .ConfigSettings ) -> types .ObjectJsonRepr :
337337 """Returns the portfolio representation as JSON"""
338338 self .refresh ()
339- json_data = {}
339+ json_data = {"key" : self .key , "name" : self .name }
340+ if self ._description :
341+ json_data ["description" ] = self ._description
340342 subportfolios = self .sub_portfolios ()
343+ if not self .is_sub_portfolio :
344+ json_data ["visibility" ] = self ._visibility
345+ json_data ["permissions" ] = self .permissions ().export (export_settings = export_settings )
346+ json_data ["tags" ] = self ._tags
341347 if subportfolios :
342348 json_data ["portfolios" ] = {}
343349 for s in subportfolios .values ():
344350 subp_json = s .to_json (export_settings )
345351 subp_key = subp_json .pop ("key" )
346352 json_data ["portfolios" ][subp_key ] = subp_json
347- if not self .is_sub_portfolio :
348- json_data ["permissions" ] = self .permissions ().export (export_settings = export_settings )
349- json_data ["visibility" ] = self ._visibility
350- json_data ["key" ] = self .key
351- json_data ["name" ] = self .name
352- json_data ["tags" ] = self ._tags
353- json_data ["applications" ] = self ._applications
354- if self ._description :
355- json_data ["description" ] = self ._description
356353 mode = self .selection_mode ().copy ()
357354 if mode and "none" not in mode :
358355 json_data ["projects" ] = mode
356+ json_data ["applications" ] = self ._applications
359357 return json_data
360358
361359 def export (self , export_settings : types .ConfigSettings ) -> types .ObjectJsonRepr :
@@ -744,7 +742,7 @@ def export(endpoint: pf.Platform, export_settings: types.ConfigSettings, key_lis
744742 nb_portfolios = count (endpoint = endpoint )
745743 i = 0
746744 exported_portfolios = {}
747- for k , p in get_list (endpoint = endpoint , key_list = key_list ).items ():
745+ for k , p in sorted ( get_list (endpoint = endpoint , key_list = key_list ).items () ):
748746 if not p .is_sub_portfolio :
749747 exported_portfolios [k ] = p .export (export_settings )
750748 exported_portfolios [k ].pop ("key" )
@@ -797,3 +795,20 @@ def __create_portfolio_hierarchy(endpoint: pf.Platform, data: types.ApiPayload,
797795def get_api_branch (branch : str ) -> str :
798796 """Returns the value to pass to the API for the branch parameter"""
799797 return branch if branch != settings .DEFAULT_BRANCH else None
798+
799+
800+ def convert_for_yaml (original_json : types .ObjectJsonRepr ) -> types .ObjectJsonRepr :
801+ """Convert the original JSON defined for JSON export into a JSON format more adapted for YAML export"""
802+ new_json = util .dict_to_list (original_json , "key" )
803+ for p_json in new_json :
804+ try :
805+ p_json ["projects" ] = [{"key" : k , "branch" : br } for k , br in p_json ["projects" ]["manual" ].items ()]
806+ except KeyError :
807+ pass
808+ if "portfolios" in p_json :
809+ p_json ["portfolios" ] = convert_for_yaml (p_json ["portfolios" ])
810+ if "applications" in p_json :
811+ p_json ["applications" ] = [{"key" : k , "branches" : br } for k , br in p_json ["applications" ].items ()]
812+ if "permissions" in p_json :
813+ p_json ["permissions" ] = perms .convert_for_yaml (p_json ["permissions" ])
814+ return new_json
0 commit comments