@@ -149,76 +149,68 @@ def __export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> N
149149 if len (non_existing_projects ) > 0 :
150150 utilities .exit_fatal (f"Project key(s) '{ ',' .join (non_existing_projects )} ' do(es) not exist" , errcodes .NO_SUCH_KEY )
151151
152+ calls = {
153+ options .WHAT_SETTINGS : [__JSON_KEY_SETTINGS , platform .export ],
154+ options .WHAT_RULES : [__JSON_KEY_RULES , rules .export ],
155+ options .WHAT_PROFILES : [__JSON_KEY_PROFILES , qualityprofiles .export ],
156+ options .WHAT_GATES : [__JSON_KEY_GATES , qualitygates .export ],
157+ options .WHAT_PROJECTS : [__JSON_KEY_PROJECTS , projects .export ],
158+ options .WHAT_APPS : [__JSON_KEY_APPS , applications .export ],
159+ options .WHAT_PORTFOLIOS : [__JSON_KEY_PORTFOLIOS , portfolios .export ],
160+ options .WHAT_USERS : [__JSON_KEY_USERS , users .export ],
161+ options .WHAT_GROUPS : [__JSON_KEY_GROUPS , groups .export ],
162+ }
163+
152164 log .info ("Exporting configuration from %s" , kwargs [options .URL ])
153165 key_list = kwargs [options .KEYS ]
154166 sq_settings = {__JSON_KEY_PLATFORM : endpoint .basics ()}
155- if options .WHAT_SETTINGS in what :
156- sq_settings [__JSON_KEY_SETTINGS ] = endpoint .export (export_settings = export_settings )
157- if options .WHAT_RULES in what or options .WHAT_PROFILES in what :
158- sq_settings [__JSON_KEY_RULES ] = rules .export (endpoint , export_settings = export_settings )
159- if options .WHAT_PROFILES in what :
160- sq_settings [__JSON_KEY_PROFILES ] = qualityprofiles .export (endpoint , export_settings = export_settings )
161- if options .WHAT_GATES in what :
162- sq_settings [__JSON_KEY_GATES ] = qualitygates .export (endpoint , export_settings = export_settings )
163- if options .WHAT_PROJECTS in what :
164- sq_settings [__JSON_KEY_PROJECTS ] = projects .export (endpoint , key_list = key_list , export_settings = export_settings )
165- if options .WHAT_APPS in what :
166- try :
167- sq_settings [__JSON_KEY_APPS ] = applications .export (endpoint , key_list = key_list , export_settings = export_settings )
168- except exceptions .UnsupportedOperation as e :
169- log .warning (e .message )
170- if options .WHAT_PORTFOLIOS in what :
167+ for what_item , call_data in calls .items ():
168+ if what_item not in what :
169+ continue
170+ ndx , func = call_data
171171 try :
172- sq_settings [__JSON_KEY_PORTFOLIOS ] = portfolios . export (endpoint , key_list = key_list , export_settings = export_settings )
172+ sq_settings [ndx ] = func (endpoint , export_settings = export_settings , key_list = key_list )
173173 except exceptions .UnsupportedOperation as e :
174174 log .warning (e .message )
175- if options .WHAT_USERS in what :
176- sq_settings [__JSON_KEY_USERS ] = users .export (endpoint , export_settings = export_settings )
177- if options .WHAT_GROUPS in what :
178- sq_settings [__JSON_KEY_GROUPS ] = groups .export (endpoint , export_settings = export_settings )
179-
180175 sq_settings = utilities .remove_empties (sq_settings )
181176 if not kwargs ["dontInlineLists" ]:
182177 sq_settings = utilities .inline_lists (sq_settings , exceptions = ("conditions" ,))
183178 __write_export (sq_settings , kwargs [options .REPORT_FILE ], kwargs [options .FORMAT ])
184179 log .info ("Exporting configuration from %s completed" , kwargs ["url" ])
185180
186181
187- def __import_config (endpoint : platform .Platform , what : list [str ], ** kwargs ) -> None :
188- """Imports a platform configuration from a JSON file"""
189- log .info ("Importing configuration to %s" , kwargs [options .URL ])
190- key_list = kwargs [options .KEYS ]
182+ def __read_input_file (file : str ) -> dict [str , any ]:
191183 try :
192- with open (kwargs [ options . REPORT_FILE ] , "r" , encoding = "utf-8" ) as fd :
184+ with open (file , "r" , encoding = "utf-8" ) as fd :
193185 data = json .loads (fd .read ())
194186 except FileNotFoundError as e :
195187 utilities .exit_fatal (f"OS error while reading file: { e } " , exit_code = errcodes .OS_ERROR )
196- if options . WHAT_GROUPS in what :
197- groups . import_config ( endpoint , data )
198- if options . WHAT_USERS in what :
199- users . import_config ( endpoint , data )
200- if options . WHAT_GATES in what :
201- qualitygates . import_config ( endpoint , data )
202- if options .WHAT_RULES in what :
203- rules . import_config ( endpoint , data )
204- if options . WHAT_PROFILES in what :
205- if options .WHAT_RULES not in what :
206- rules .import_config ( endpoint , data )
207- qualityprofiles . import_config ( endpoint , data )
208- if options . WHAT_SETTINGS in what :
209- endpoint . import_config ( data )
210- if options . WHAT_PROJECTS in what :
211- projects .import_config ( endpoint , data , key_list = key_list )
212- if options .WHAT_APPS in what :
213- try :
214- applications . import_config ( endpoint , data , key_list = key_list )
215- except exceptions . UnsupportedOperation as e :
216- log . warning ( e . message )
217- if options . WHAT_PORTFOLIOS in what :
218- try :
219- portfolios . import_config (endpoint , data , key_list = key_list )
220- except exceptions .UnsupportedOperation as e :
221- log .warning (e .message )
188+ return data
189+
190+
191+ def __import_config ( endpoint : platform . Platform , what : list [ str ], data : dict [ str , any ], ** kwargs ) -> None :
192+ """Imports a platform configuration from a JSON file"""
193+ log . info ( "Importing configuration to %s" , kwargs [ options . URL ] )
194+ key_list = kwargs [ options .KEYS ]
195+
196+ calls = {
197+ options .WHAT_GROUPS : groups . import_config ,
198+ options . WHAT_USERS : users .import_config ,
199+ options . WHAT_GATES : qualitygates . import_config ,
200+ options . WHAT_RULES : rules . import_config ,
201+ options . WHAT_PROFILES : qualityprofiles . import_config ,
202+ options . WHAT_SETTINGS : platform . import_config ,
203+ options . WHAT_PROJECTS : projects .import_config ,
204+ options .WHAT_APPS : applications . import_config ,
205+ options . WHAT_PORTFOLIOS : portfolios . import_config ,
206+ }
207+
208+ for what_item , func in calls . items ():
209+ if what_item in what :
210+ try :
211+ func (endpoint , data , key_list = key_list )
212+ except exceptions .UnsupportedOperation as e :
213+ log .warning (e .message )
222214 log .info ("Importing configuration to %s completed" , kwargs [options .URL ])
223215
224216
@@ -235,6 +227,8 @@ def main() -> None:
235227 utilities .exit_fatal (f"One of --{ options .EXPORT } or --{ options .IMPORT } option must be chosen" , exit_code = errcodes .ARGS_ERROR )
236228
237229 what = utilities .check_what (kwargs .pop (options .WHAT , None ), _EVERYTHING , "exported or imported" )
230+ if options .WHAT_PROFILES in what and options .WHAT_RULES not in what :
231+ what .append (options .WHAT_RULES )
238232 kwargs [options .FORMAT ] = utilities .deduct_format (kwargs [options .FORMAT ], kwargs [options .REPORT_FILE ], allowed_formats = ("json" , "yaml" ))
239233 if kwargs [options .EXPORT ]:
240234 try :
@@ -246,7 +240,7 @@ def main() -> None:
246240 if kwargs ["import" ]:
247241 if kwargs ["file" ] is None :
248242 utilities .exit_fatal ("--file is mandatory to import configuration" , errcodes .ARGS_ERROR )
249- __import_config (endpoint , what , ** kwargs )
243+ __import_config (endpoint , what , __read_input_file ( kwargs [ options . REPORT_FILE ]), ** kwargs )
250244 utilities .stop_clock (start_time )
251245 sys .exit (0 )
252246
0 commit comments