@@ -124,11 +124,15 @@ def __parse_args(desc: str) -> object:
124124
125125def __normalize_json (json_data : dict [str , any ], remove_empty : bool = True , remove_none : bool = True ) -> dict [str , any ]:
126126 """Sorts a JSON file and optionally remove empty and none values"""
127+ SORT_FIELDS = {"users" : "login" }
127128 log .info ("Normalizing JSON - remove empty = %s, remove nones = %s" , str (remove_empty ), str (remove_none ))
128129 json_data = utilities .clean_data (json_data , remove_none = remove_none , remove_empty = remove_empty )
129130 json_data = utilities .order_keys (json_data , * _SECTIONS_ORDER )
130131 for key in [k for k in _SECTIONS_TO_SORT if k in json_data ]:
131- json_data [key ] = {k : json_data [key ][k ] for k in sorted (json_data [key ])}
132+ if isinstance (json_data [key ], dict ):
133+ json_data [key ] = {k : json_data [key ][k ] for k in sorted (json_data [key ])}
134+ else :
135+ json_data [key ] = utilities .sort_list_by_key (json_data [key ], SORT_FIELDS .get (key , "key" ))
132136 return json_data
133137
134138
@@ -169,27 +173,37 @@ def write_objects(queue: Queue[types.ObjectJsonRepr], fd: TextIO, object_type: s
169173 done = False
170174 prefix = ""
171175 log .info ("Waiting %s to write..." , object_type )
172- print (f'"{ object_type } ": ' + "{" , file = fd )
176+ objects_exported_as_lists = ("projects" , "applications" , "users" , "portfolios" )
177+ objects_exported_as_whole = ("qualityGates" , "groups" )
178+ log .info ("Waiting %s to write..." , object_type )
179+ if object_type in objects_exported_as_lists :
180+ start , stop = ("[" , "]" )
181+ elif object_type in objects_exported_as_whole :
182+ start , stop = ("" , "" )
183+ else :
184+ start , stop = ("{" , "}" )
185+ print (f'"{ object_type } ": ' + start , file = fd )
173186 while not done :
174187 obj_json = queue .get ()
175188 if not (done := obj_json is utilities .WRITE_END ):
176189 if object_type == "groups" :
177190 obj_json = __prep_json_for_write (obj_json , {** export_settings , EXPORT_EMPTY : True })
178191 else :
179192 obj_json = __prep_json_for_write (obj_json , export_settings )
180- if object_type in ("projects" , "applications" , "portfolios" , "users" ):
181- if object_type == "users" :
182- key = obj_json .pop ("login" , None )
183- else :
184- key = obj_json .pop ("key" , None )
185- log .debug ("Writing %s key '%s'" , object_type [:- 1 ], key )
193+ key = "" if isinstance (obj_json , list ) else obj_json .get ("key" , obj_json .get ("login" , obj_json .get ("name" , "unknown" )))
194+ log .debug ("Writing %s key '%s'" , object_type [:- 1 ], key )
195+ if object_type in objects_exported_as_lists :
196+ print (f"{ prefix } { utilities .json_dump (obj_json )} " , end = "" , file = fd )
197+ elif object_type in objects_exported_as_whole :
198+ print (f"{ prefix } { utilities .json_dump (obj_json )} " , end = "" , file = fd )
199+ elif object_type in ("applications" , "portfolios" , "users" ):
186200 print (f'{ prefix } "{ key } ": { utilities .json_dump (obj_json )} ' , end = "" , file = fd )
187201 else :
188202 log .debug ("Writing %s" , object_type )
189203 print (f"{ prefix } { utilities .json_dump (obj_json )[2 :- 1 ]} " , end = "" , file = fd )
190204 prefix = ",\n "
191205 queue .task_done ()
192- print ("\n }" , file = fd , end = "" )
206+ print ("\n " + stop , file = fd , end = "" )
193207 log .info ("Writing %s complete" , object_type )
194208
195209
0 commit comments