Skip to content

Commit ed4af7e

Browse files
committed
Refatoring import / export functions signatures
1 parent 16e673d commit ed4af7e

File tree

10 files changed

+84
-55
lines changed

10 files changed

+84
-55
lines changed

cli/config.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,34 @@ 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()}
167+
for what_item, call_data in calls.items():
168+
if what_item in what:
169+
ndx, func = call_data
170+
try:
171+
sq_settings[ndx] = func(endpoint, export_settings=export_settings, key_list=key_list)
172+
except exceptions.UnsupportedOperation as e:
173+
log.warning(e.message)
174+
sq_settings = utilities.remove_empties(sq_settings)
175+
if not kwargs["dontInlineLists"]:
176+
sq_settings = utilities.inline_lists(sq_settings, exceptions=("conditions",))
177+
__write_export(sq_settings, kwargs[options.REPORT_FILE], kwargs[options.FORMAT])
178+
log.info("Exporting configuration from %s completed", kwargs["url"])
179+
155180
if options.WHAT_SETTINGS in what:
156181
sq_settings[__JSON_KEY_SETTINGS] = endpoint.export(export_settings=export_settings)
157182
if options.WHAT_RULES in what or options.WHAT_PROFILES in what:
@@ -177,12 +202,6 @@ def __export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> N
177202
if options.WHAT_GROUPS in what:
178203
sq_settings[__JSON_KEY_GROUPS] = groups.export(endpoint, export_settings=export_settings)
179204

180-
sq_settings = utilities.remove_empties(sq_settings)
181-
if not kwargs["dontInlineLists"]:
182-
sq_settings = utilities.inline_lists(sq_settings, exceptions=("conditions",))
183-
__write_export(sq_settings, kwargs[options.REPORT_FILE], kwargs[options.FORMAT])
184-
log.info("Exporting configuration from %s completed", kwargs["url"])
185-
186205

187206
def __read_input_file(file: str) -> dict[str, any]:
188207
try:
@@ -203,6 +222,7 @@ def __import_config(endpoint: platform.Platform, what: list[str], data: dict[str
203222
options.WHAT_USERS: users.import_config,
204223
options.WHAT_GATES: qualitygates.import_config,
205224
options.WHAT_RULES: rules.import_config,
225+
options.WHAT_PROFILES: qualityprofiles.import_config,
206226
options.WHAT_SETTINGS: platform.import_config,
207227
options.WHAT_PROJECTS: projects.import_config,
208228
options.WHAT_APPS: applications.import_config,
@@ -216,7 +236,6 @@ def __import_config(endpoint: platform.Platform, what: list[str], data: dict[str
216236
except exceptions.UnsupportedOperation as e:
217237
log.warning(e.message)
218238
log.info("Importing configuration to %s completed", kwargs[options.URL])
219-
return None
220239

221240

222241
def main() -> None:

sonar/applications.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,11 +498,10 @@ def export(endpoint: pf.Platform, export_settings: types.ConfigSettings, key_lis
498498
"""Exports applications as JSON
499499
500500
:param Platform endpoint: Reference to the Sonar platform
501+
:param ConfigSetting export_settings: Options to use for export
501502
:param KeyList key_list: list of Application keys to export, defaults to all if None
502-
:param full: Whether to export all attributes, including those that can't be set, defaults to False
503-
:type full: bool
504503
:return: Dict of applications settings
505-
:rtype: dict
504+
:rtype: ObjectJsonRepr
506505
"""
507506
if endpoint.is_sonarcloud():
508507
# log.info("Applications do not exist in SonarCloud, export skipped")

sonar/groups.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,16 @@ def get_list(endpoint: pf.Platform) -> dict[str, Group]:
256256
return search(endpoint)
257257

258258

259-
def export(endpoint: pf.Platform, export_settings: types.ConfigSettings) -> types.ObjectJsonRepr:
260-
"""Exports all groups configuration as dict
261-
Default groups (sonar-users) are not exported
262-
263-
:param endpoint: reference to the SonarQube platform
264-
:type endpoint: pf.Platform
265-
:return: list of groups
266-
:rtype: dict{name: description}
259+
def export(endpoint: pf.Platform, export_settings: types.ConfigSettings, key_list: types.KeyList = None) -> types.ObjectJsonRepr:
260+
"""Exports groups representation in JSON
261+
262+
:param Platform endpoint: reference to the SonarQube platform
263+
:param ConfigSettings export_settings: Export parameters
264+
:param KeyList key_list: List of project keys to export, defaults to None (all projects)
265+
:return: list of groups settings
266+
:rtype: ObjectJsonRepr
267267
"""
268+
268269
log.info("Exporting groups")
269270
g_list = {}
270271
for g_name, g_obj in sorted(search(endpoint=endpoint).items()):

sonar/platform.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ def import_config(endpoint: Platform, config_data: types.ObjectJsonRepr, key_lis
824824
825825
:param Platform endpoint: reference to the SonarQube platform
826826
:param ObjectJsonRepr config_data: the configuration to import
827-
:param KeyList key_list: List of project keys to be considered for the import, defaults to None (all projects)
827+
:param KeyList key_list: Unused
828828
:return: Nothing
829829
"""
830830
endpoint.import_config(config_data)
@@ -853,3 +853,15 @@ def convert_for_yaml(original_json: types.ObjectJsonRepr) -> types.ObjectJsonRep
853853
if "devopsIntegration" in original_json:
854854
original_json["devopsIntegration"] = util.dict_to_list(original_json["devopsIntegration"], "name")
855855
return original_json
856+
857+
858+
def export(endpoint: Platform, export_settings: types.ConfigSettings, key_list: types.KeyList = None) -> types.ObjectJsonRepr:
859+
"""Exports all or a list of projects configuration as dict
860+
861+
:param Platform endpoint: reference to the SonarQube platform
862+
:param ConfigSettings export_settings: Export parameters
863+
:param KeyList key_list: Unused
864+
:return: Platform settings
865+
:rtype: ObjectJsonRepr
866+
"""
867+
return endpoint.export(export_settings)

sonar/portfolios.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,10 +728,10 @@ def export(endpoint: pf.Platform, export_settings: types.ConfigSettings, key_lis
728728
"""Exports portfolios as JSON
729729
730730
:param Platform endpoint: Reference to the SonarQube platform
731-
:param KeyList key_list: list of portfoliios keys to export as csv or list, defaults to all if None
732731
:param ConfigSetting export_settings: Options to use for export
733-
:return: Dict of applications settings
734-
:rtype: dict
732+
:param KeyList key_list: list of portfoliios keys to export as csv or list, defaults to all if None
733+
:return: Dict of portfolio settings
734+
:rtype: ObjectJsonRepr
735735
"""
736736
check_supported(endpoint)
737737

sonar/projects.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,8 +1368,8 @@ def export(endpoint: pf.Platform, export_settings: types.ConfigSettings, key_lis
13681368
:param Platform endpoint: reference to the SonarQube platform
13691369
:param ConfigSettings export_settings: Export parameters
13701370
:param KeyList key_list: List of project keys to export, defaults to None (all projects)
1371-
:return: list of projects
1372-
:rtype: dict{key: Project}
1371+
:return: list of projects settings
1372+
:rtype: ObjectJsonRepr
13731373
"""
13741374
for qp in qualityprofiles.get_list(endpoint).values():
13751375
qp.projects()

sonar/qualitygates.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,14 @@ def get_list(endpoint: pf.Platform) -> dict[str, QualityGate]:
380380
return qg_list
381381

382382

383-
def export(endpoint: pf.Platform, export_settings: types.ConfigSettings) -> types.ObjectJsonRepr:
384-
"""
385-
:return: The list of quality gates in their JSON representation
386-
:rtype: dict
383+
def export(endpoint: pf.Platform, export_settings: types.ConfigSettings, key_list: types.KeyList = None) -> types.ObjectJsonRepr:
384+
"""Exports quality gates as JSON
385+
386+
:param Platform endpoint: Reference to the Sonar platform
387+
:param ConfigSetting export_settings: Options to use for export
388+
:param KeyList key_list: Unused
389+
:return: Quality gates representations as JSON
390+
:rtype: ObjectJsonRepr
387391
"""
388392
log.info("Exporting quality gates")
389393
return {k: qg.to_json(export_settings) for k, qg in sorted(get_list(endpoint).items())}

sonar/qualityprofiles.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -579,14 +579,14 @@ def hierarchize(qp_list: dict[str, str], endpoint: pf.Platform) -> types.ObjectJ
579579
return qp_list
580580

581581

582-
def export(endpoint: pf.Platform, export_settings: types.ConfigSettings, in_hierarchy: bool = True) -> types.ObjectJsonRepr:
583-
"""Exports all quality profiles configuration as dict
582+
def export(endpoint: pf.Platform, export_settings: types.ConfigSettings, key_list: types.KeyList = None) -> types.ObjectJsonRepr:
583+
"""Exports all or a list of quality profiles configuration as dict
584584
585585
:param Platform endpoint: reference to the SonarQube platform
586-
:param dict export_settings: Export settings
587-
:param bool in_hierarchy: Whether quality profiles dict should be organized hierarchically (following inheritance)
588-
:return: dict structure of all quality profiles
589-
:rtype: dict
586+
:param ConfigSettings export_settings: Export parameters
587+
:param KeyList key_list: Unused
588+
:return: Dict of quality profiles JSON representation
589+
:rtype: ObjectJsonRepr
590590
"""
591591
log.info("Exporting quality profiles")
592592
qp_list = {}
@@ -598,8 +598,7 @@ def export(endpoint: pf.Platform, export_settings: types.ConfigSettings, in_hier
598598
if lang not in qp_list:
599599
qp_list[lang] = {}
600600
qp_list[lang][name] = json_data
601-
if in_hierarchy:
602-
qp_list = hierarchize(qp_list, endpoint)
601+
qp_list = hierarchize(qp_list, endpoint)
603602
return dict(sorted(qp_list.items()))
604603

605604

sonar/rules.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -287,24 +287,19 @@ def export_needed(endpoint: platform.Platform, instantiated: bool = True, extend
287287
return utilities.remove_nones(rule_list)
288288

289289

290-
def export(
291-
endpoint: platform.Platform, export_settings: types.ConfigSettings, instantiated: bool = True, extended: bool = True, standard: bool = False
292-
) -> types.ObjectJsonRepr:
290+
def export(endpoint: platform.Platform, export_settings: types.ConfigSettings, key_list: types.KeyList = None) -> types.ObjectJsonRepr:
293291
"""Returns a dict of rules for export
294-
:return: a dict of rule onbjects indexed with rule key
295292
:param Platform endpoint: The SonarQube Platform object to connect to
296293
:param ConfigSettings export_settings: parameters to export
297-
:param bool instantiated: Include instantiated rules in the list
298-
:param bool extended: Include extended rules in the list
299-
:param bool standard: Include standard rules in the list
300-
:param full standard: Include full rule information in the export
301-
:rtype: dict{ruleKey: <ruleJson.}
294+
:param KeyList key_list: Unused
295+
:return: a dict of rules with their JSON representation
296+
:rtype: ObjectJsonRepr
302297
"""
303298
log.info("Exporting rules")
304-
if standard:
305-
return export_all(endpoint, export_settings["FULL_EXPORT"])
306-
else:
307-
return export_needed(endpoint, instantiated, extended, export_settings["FULL_EXPORT"])
299+
# if standard:
300+
return export_all(endpoint, export_settings["FULL_EXPORT"])
301+
# else:
302+
# return export_needed(endpoint, instantiated, extended, export_settings["FULL_EXPORT"])
308303

309304

310305
def import_config(endpoint: platform.Platform, config_data: types.ObjectJsonRepr, key_list: types.KeyList = None) -> bool:

sonar/users.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,14 +403,14 @@ def search(endpoint: pf.Platform, params: types.ApiParams = None) -> dict[str, U
403403
return sqobject.search_objects(endpoint=endpoint, object_class=User, params=params)
404404

405405

406-
def export(endpoint: pf.Platform, export_settings: types.ConfigSettings) -> types.ObjectJsonRepr:
407-
"""Exports all users as dict
406+
def export(endpoint: pf.Platform, export_settings: types.ConfigSettings, key_list: types.KeyList = None) -> types.ObjectJsonRepr:
407+
"""Exports all users in JSON representation
408408
409409
:param Platform endpoint: reference to the SonarQube platform
410-
:param full: Whether to export all settings including those useless for re-import, defaults to False
411-
:type full: bool, optional
412-
:return: list of projects
413-
:rtype: dict{key: Project}
410+
:param ConfigSettings export_settings: Export parameters
411+
:param KeyList key_list: Unused
412+
:return: list of users JSON representation
413+
:rtype: ObjectJsonRepr
414414
"""
415415
log.info("Exporting users")
416416
u_list = {}

0 commit comments

Comments
 (0)