7575_WRITE_LOCK = Lock ()
7676
7777
78+ _EXPORT_CALLS = {
79+ options .WHAT_SETTINGS : [__JSON_KEY_SETTINGS , platform .export ],
80+ options .WHAT_RULES : [__JSON_KEY_RULES , rules .export ],
81+ options .WHAT_PROFILES : [__JSON_KEY_PROFILES , qualityprofiles .export ],
82+ options .WHAT_GATES : [__JSON_KEY_GATES , qualitygates .export ],
83+ options .WHAT_PROJECTS : [__JSON_KEY_PROJECTS , projects .export ],
84+ options .WHAT_APPS : [__JSON_KEY_APPS , applications .export ],
85+ options .WHAT_PORTFOLIOS : [__JSON_KEY_PORTFOLIOS , portfolios .export ],
86+ options .WHAT_USERS : [__JSON_KEY_USERS , users .export ],
87+ options .WHAT_GROUPS : [__JSON_KEY_GROUPS , groups .export ],
88+ }
89+
90+
7891def __parse_args (desc ):
7992 parser = options .set_common_args (desc )
8093 parser = options .set_key_arg (parser )
@@ -173,8 +186,8 @@ def __convert_for_yaml(json_export: dict[str, any]) -> dict[str, any]:
173186 return json_export
174187
175188
176- def __export_config (endpoint : platform .Platform , what : list [str ], ** kwargs ) -> None :
177- """Exports a platform configuration in a JSON file """
189+ def __export_config_sync (endpoint : platform .Platform , what : list [str ], ** kwargs ) -> None :
190+ """Exports config in a synchronous way """
178191 export_settings = {
179192 "INLINE_LISTS" : not kwargs ["dontInlineLists" ],
180193 "EXPORT_DEFAULTS" : kwargs ["exportDefaults" ],
@@ -186,42 +199,66 @@ def __export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> N
186199 non_existing_projects = [key for key in kwargs [options .KEYS ] if not projects .exists (key , endpoint )]
187200 if len (non_existing_projects ) > 0 :
188201 utilities .exit_fatal (f"Project key(s) '{ ',' .join (non_existing_projects )} ' do(es) not exist" , errcodes .NO_SUCH_KEY )
202+ log .info ("Exporting configuration synchronously from %s" , kwargs [options .URL ])
203+ key_list = kwargs [options .KEYS ]
204+ sq_settings = {__JSON_KEY_PLATFORM : endpoint .basics ()}
205+ for what_item , call_data in _EXPORT_CALLS .items ():
206+ if what_item not in what :
207+ continue
208+ ndx , func = call_data
209+ try :
210+ sq_settings [ndx ] = func (endpoint , export_settings = export_settings , key_list = key_list )
211+ except exceptions .UnsupportedOperation as e :
212+ log .warning (e .message )
213+ except exceptions .ObjectNotFound as e :
214+ log .error (e .message )
215+ sq_settings = utilities .remove_empties (sq_settings )
216+ if not kwargs ["dontInlineLists" ]:
217+ sq_settings = utilities .inline_lists (sq_settings , exceptions = ("conditions" ,))
218+ __write_export (sq_settings , kwargs [options .REPORT_FILE ], kwargs [options .FORMAT ])
219+ log .info ("Synchronous export of configuration from %s completed" , kwargs ["url" ])
189220
190- calls = {
191- options .WHAT_SETTINGS : [__JSON_KEY_SETTINGS , platform .export ],
192- options .WHAT_RULES : [__JSON_KEY_RULES , rules .export ],
193- options .WHAT_PROFILES : [__JSON_KEY_PROFILES , qualityprofiles .export ],
194- options .WHAT_GATES : [__JSON_KEY_GATES , qualitygates .export ],
195- # options.WHAT_PROJECTS: [__JSON_KEY_PROJECTS, projects.export],
196- options .WHAT_APPS : [__JSON_KEY_APPS , applications .export ],
197- options .WHAT_PORTFOLIOS : [__JSON_KEY_PORTFOLIOS , portfolios .export ],
198- options .WHAT_USERS : [__JSON_KEY_USERS , users .export ],
199- options .WHAT_GROUPS : [__JSON_KEY_GROUPS , groups .export ],
200- }
201221
202- log .info ("Exporting configuration from %s" , kwargs [options .URL ])
222+ def __export_config_async (endpoint : platform .Platform , what : list [str ], ** kwargs ) -> None :
223+ """Exports a platform configuration in a JSON file"""
224+ export_settings = {
225+ "INLINE_LISTS" : not kwargs ["dontInlineLists" ],
226+ "EXPORT_DEFAULTS" : kwargs ["exportDefaults" ],
227+ "FULL_EXPORT" : kwargs ["fullExport" ],
228+ "THREADS" : kwargs [options .NBR_THREADS ],
229+ options .REPORT_FILE : kwargs [options .REPORT_FILE ],
230+ "WRITE_CALLBACK" : write_project ,
231+ }
232+ log .info ("Exporting configuration from %s (asynchronously)" , kwargs [options .URL ])
203233 key_list = kwargs [options .KEYS ]
204234 sq_settings = {__JSON_KEY_PLATFORM : endpoint .basics ()}
205- for what_item , call_data in calls .items ():
235+ for what_item , call_data in _EXPORT_CALLS .items ():
206236 if what_item not in what or what_item == options .WHAT_PROJECTS :
207237 continue
208238 ndx , func = call_data
209239 try :
210- sq_settings [ndx ] = func (endpoint , export_settings = export_settings , key_list = key_list )
240+ sq_settings [ndx ] = utilities . remove_empties ( func (endpoint , export_settings = export_settings , key_list = key_list ) )
211241 __write_export (sq_settings , kwargs [options .REPORT_FILE ], kwargs [options .FORMAT ])
212242 except exceptions .UnsupportedOperation as e :
213243 log .warning (e .message )
214- sq_settings = utilities .remove_empties (sq_settings )
244+ except exceptions .ObjectNotFound as e :
245+ log .error (e .message )
215246 if not kwargs ["dontInlineLists" ]:
216247 sq_settings = utilities .inline_lists (sq_settings , exceptions = ("conditions" ,))
248+ __write_export (sq_settings , kwargs [options .REPORT_FILE ], kwargs [options .FORMAT ])
217249
218- export_settings ["WRITE_CALLBACK" ] = write_project
219250 __remove_chars_at_end (kwargs [options .REPORT_FILE ], 3 )
220251 __add_project_header (kwargs [options .REPORT_FILE ])
221- projects .export (endpoint , export_settings = export_settings , key_list = key_list )
252+ projects .export (endpoint , export_settings = export_settings , key_list = None )
222253 __add_project_footer (kwargs [options .REPORT_FILE ])
254+ log .info ("Asynchronous export of configuration from %s completed" , kwargs ["url" ])
223255
224- log .info ("Exporting configuration from %s completed" , kwargs ["url" ])
256+
257+ def __export_config (endpoint : platform .Platform , what : list [str ], ** kwargs ) -> None :
258+ if kwargs [options .KEYS ] or options .WHAT_PROJECTS not in what or kwargs [options .FORMAT ] != "json" :
259+ __export_config_sync (endpoint = endpoint , what = what , ** kwargs )
260+ else :
261+ __export_config_async (endpoint = endpoint , what = what , ** kwargs )
225262
226263
227264def __import_config (endpoint : platform .Platform , what : list [str ], ** kwargs ) -> None :
0 commit comments