Skip to content

Commit 0f46cd0

Browse files
authored
Remove-now-unneeded-YAML-conversion (#2069)
* Remove calls to convert_for_yaml * Remove convert_for_yaml() * Remove forgotten call to convert_for_yaml() * Remove more convert_for_yaml() * Fix call_data unpacking
1 parent 4a283a7 commit 0f46cd0

File tree

11 files changed

+12
-135
lines changed

11 files changed

+12
-135
lines changed

cli/config.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@
6464
}
6565

6666
_EXPORT_CALLS = {
67-
c.CONFIG_KEY_PLATFORM: [c.CONFIG_KEY_PLATFORM, platform.basics, platform.convert_for_yaml],
68-
options.WHAT_SETTINGS: [c.CONFIG_KEY_SETTINGS, platform.export, platform.convert_for_yaml],
69-
options.WHAT_RULES: [c.CONFIG_KEY_RULES, rules.export, rules.convert_for_yaml],
70-
options.WHAT_PROFILES: [c.CONFIG_KEY_PROFILES, qualityprofiles.export, qualityprofiles.convert_for_yaml],
71-
options.WHAT_GATES: [c.CONFIG_KEY_GATES, qualitygates.export, qualitygates.convert_for_yaml],
72-
options.WHAT_PROJECTS: [c.CONFIG_KEY_PROJECTS, projects.export, projects.convert_for_yaml],
73-
options.WHAT_APPS: [c.CONFIG_KEY_APPS, applications.export, applications.convert_for_yaml],
74-
options.WHAT_PORTFOLIOS: [c.CONFIG_KEY_PORTFOLIOS, portfolios.export, portfolios.convert_for_yaml],
75-
options.WHAT_USERS: [c.CONFIG_KEY_USERS, users.export, users.convert_for_yaml],
76-
options.WHAT_GROUPS: [c.CONFIG_KEY_GROUPS, groups.export, groups.convert_for_yaml],
67+
c.CONFIG_KEY_PLATFORM: [c.CONFIG_KEY_PLATFORM, platform.basics],
68+
options.WHAT_SETTINGS: [c.CONFIG_KEY_SETTINGS, platform.export],
69+
options.WHAT_RULES: [c.CONFIG_KEY_RULES, rules.export],
70+
options.WHAT_PROFILES: [c.CONFIG_KEY_PROFILES, qualityprofiles.export],
71+
options.WHAT_GATES: [c.CONFIG_KEY_GATES, qualitygates.export],
72+
options.WHAT_PROJECTS: [c.CONFIG_KEY_PROJECTS, projects.export],
73+
options.WHAT_APPS: [c.CONFIG_KEY_APPS, applications.export],
74+
options.WHAT_PORTFOLIOS: [c.CONFIG_KEY_PORTFOLIOS, portfolios.export],
75+
options.WHAT_USERS: [c.CONFIG_KEY_USERS, users.export],
76+
options.WHAT_GROUPS: [c.CONFIG_KEY_GROUPS, groups.export],
7777
}
7878

7979
WHAT_EVERYTHING = list(_EXPORT_CALLS.keys())[1:]
@@ -150,22 +150,12 @@ def __normalize_file(file: str, format: str) -> bool:
150150
json_data = __normalize_json(json_data, remove_empty=False, remove_none=True)
151151
with utilities.open_file(file, mode="w") as fd:
152152
if format == "yaml":
153-
print(yaml.dump(__convert_for_yaml(json_data), sort_keys=False), file=fd)
153+
print(yaml.dump(json_data, sort_keys=False), file=fd)
154154
else:
155155
print(utilities.json_dump(json_data), file=fd)
156156
return True
157157

158158

159-
def __convert_for_yaml(json_export: dict[str, any]) -> dict[str, any]:
160-
"""Converts the default JSON produced by export to a modified version more suitable for YAML"""
161-
for what in WHAT_EVERYTHING:
162-
for k in json_export:
163-
if what.lower() == k.lower() or (what == "settings" and k == "globalSettings"):
164-
yamlify_func = _EXPORT_CALLS[what][2]
165-
json_export[k] = yamlify_func(json_export[k])
166-
return json_export
167-
168-
169159
def write_objects(queue: Queue[types.ObjectJsonRepr], fd: TextIO, object_type: str, export_settings: types.ConfigSettings) -> None:
170160
"""
171161
Thread to write projects in the JSON file
@@ -234,7 +224,7 @@ def export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> Non
234224
for what_item, call_data in _EXPORT_CALLS.items():
235225
if what_item not in what:
236226
continue
237-
ndx, func, _ = call_data
227+
ndx, func = call_data
238228
if not is_first:
239229
print(",", file=fd)
240230
is_first = False

sonar/applications.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -598,16 +598,3 @@ def search_by_name(endpoint: pf.Platform, name: str) -> dict[str, Application]:
598598
data[app.key] = app
599599
# return {app.key: app for app in Application.CACHE.values() if app.name == name}
600600
return data
601-
602-
603-
def convert_for_yaml(original_json: types.ObjectJsonRepr) -> types.ObjectJsonRepr:
604-
"""Convert the original JSON defined for JSON export into a JSON format more adapted for YAML export"""
605-
new_json = util.dict_to_list(util.remove_nones(original_json), "key")
606-
for app_json in new_json:
607-
app_json["branches"] = util.dict_to_list(app_json["branches"], "name")
608-
for b in app_json["branches"]:
609-
if "projects" in b:
610-
b["projects"] = [{"key": k, "branch": br} for k, br in b["projects"].items()]
611-
if "permissions" in app_json:
612-
app_json["permissions"] = permissions.convert_for_yaml(app_json["permissions"])
613-
return new_json

sonar/groups.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,3 @@ def exists(endpoint: pf.Platform, name: str) -> bool:
432432
:return: whether the group exists
433433
"""
434434
return Group.get_object(endpoint=endpoint, name=name) is not None
435-
436-
437-
def convert_for_yaml(original_json: types.ObjectJsonRepr) -> types.ObjectJsonRepr:
438-
"""Convert the original JSON defined for JSON export into a JSON format more adapted for YAML export"""
439-
return util.dict_to_list(util.remove_nones(original_json), "name", "description")

sonar/permissions/permissions.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,3 @@ def black_list(perms: types.JsonPermissions, disallowed_perms: list[str]) -> typ
430430
for user_or_group, original_perms in sub_perms.items():
431431
resulting_perms[perm_type][user_or_group] = [p for p in original_perms if p not in disallowed_perms]
432432
return resulting_perms
433-
434-
435-
def convert_for_yaml(json_perms: types.ObjectJsonRepr) -> types.ObjectJsonRepr:
436-
"""Converts permissions in a format that is more friendly for YAML"""
437-
converted_perms = []
438-
for ptype in "groups", "users":
439-
if ptype in json_perms:
440-
converted_perms += utilities.dict_to_list(json_perms[ptype], ptype[:-1], "permissions")
441-
return converted_perms

sonar/platform.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -942,28 +942,6 @@ def _check_for_retry(response: requests.models.Response) -> tuple[bool, str]:
942942
return False, None
943943

944944

945-
def convert_for_yaml(original_json: types.ObjectJsonRepr) -> types.ObjectJsonRepr:
946-
"""Convert the original JSON defined for JSON export into a JSON format more adapted for YAML export"""
947-
original_json = util.remove_nones(original_json)
948-
if "plugins" in original_json:
949-
original_json["plugins"] = util.dict_to_list(original_json["plugins"], "key", "version")
950-
if "languages" in original_json:
951-
original_json["languages"] = util.dict_to_list(original_json["languages"], "language")
952-
if "permissions" in original_json:
953-
original_json["permissions"] = permissions.convert_for_yaml(original_json["permissions"])
954-
if "permissionTemplates" in original_json:
955-
for tpl in original_json["permissionTemplates"].values():
956-
if "permissions" in tpl:
957-
tpl["permissions"] = permissions.convert_for_yaml(tpl["permissions"])
958-
original_json["permissionTemplates"] = util.dict_to_list(original_json["permissionTemplates"], "name")
959-
if "devopsIntegration" in original_json:
960-
original_json["devopsIntegration"] = util.dict_to_list(original_json["devopsIntegration"], "name")
961-
for key in ("analysisScope", "authentication", "generalSettings", "linters"):
962-
if key in original_json:
963-
original_json[key] = util.dict_to_list(original_json[key], "key", "value")
964-
return original_json
965-
966-
967945
def export(endpoint: Platform, export_settings: types.ConfigSettings, **kwargs) -> types.ObjectJsonRepr:
968946
"""Exports all or a list of projects configuration as dict
969947

sonar/portfolios.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -847,23 +847,6 @@ def get_api_branch(branch: str) -> str:
847847
return branch if branch != c.DEFAULT_BRANCH else None
848848

849849

850-
def convert_for_yaml(original_json: types.ObjectJsonRepr) -> types.ObjectJsonRepr:
851-
"""Convert the original JSON defined for JSON export into a JSON format more adapted for YAML export"""
852-
new_json = util.dict_to_list(util.remove_nones(original_json), "key")
853-
for p_json in new_json:
854-
try:
855-
p_json["projects"] = [{"key": k, "branch": br} for k, br in p_json["projects"]["manual"].items()]
856-
except KeyError:
857-
pass
858-
if "portfolios" in p_json:
859-
p_json["portfolios"] = convert_for_yaml(p_json["portfolios"])
860-
if "applications" in p_json:
861-
p_json["applications"] = [{"key": k, "branches": br} for k, br in p_json["applications"].items()]
862-
if "permissions" in p_json:
863-
p_json["permissions"] = perms.convert_for_yaml(p_json["permissions"])
864-
return new_json
865-
866-
867850
def clear_cache(endpoint: pf.Platform) -> None:
868851
"""Clears the cache of an endpoint"""
869852
Portfolio.clear_cache(endpoint)

sonar/projects.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,23 +1696,3 @@ def import_zips(endpoint: pf.Platform, project_list: list[str], threads: int = 2
16961696
log.info("%d/%d imports (%d%%) - Latest: %s - %s", i, nb_projects, int(i * 100 / nb_projects), proj_key, status)
16971697
log.info("%s", ", ".join([f"{k}:{v}" for k, v in statuses_count.items()]))
16981698
return statuses
1699-
1700-
1701-
def convert_proj_for_yaml(proj_json: types.ObjectJsonRepr) -> types.ObjectJsonRepr:
1702-
"""Convert the original JSON defined for JSON export into a JSON format more adapted for YAML export"""
1703-
if "branches" in proj_json:
1704-
proj_json["branches"] = util.dict_to_list(proj_json["branches"], "name")
1705-
if "qualityProfiles" in proj_json:
1706-
proj_json["qualityProfiles"] = util.dict_to_list(proj_json["qualityProfiles"], "language", "name")
1707-
if "permissions" in proj_json:
1708-
proj_json["permissions"] = perms.convert_for_yaml(proj_json["permissions"])
1709-
return proj_json
1710-
1711-
1712-
def convert_for_yaml(original_json: types.ObjectJsonRepr) -> types.ObjectJsonRepr:
1713-
"""Convert the original JSON defined for JSON export into a JSON format more adapted for YAML export"""
1714-
clean_json = util.remove_nones(original_json)
1715-
new_json = []
1716-
for proj in util.dict_to_list(clean_json, "key"):
1717-
new_json.append(convert_proj_for_yaml(proj))
1718-
return new_json

sonar/qualitygates.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,3 @@ def _decode_condition(cond: str) -> tuple[str, str, str]:
561561
def search_by_name(endpoint: pf.Platform, name: str) -> dict[str, QualityGate]:
562562
"""Searches quality gates matching name"""
563563
return util.search_by_name(endpoint, name, QualityGate.API[c.LIST], "qualitygates")
564-
565-
566-
def convert_for_yaml(original_json: types.ObjectJsonRepr) -> types.ObjectJsonRepr:
567-
"""Convert the original JSON defined for JSON export into a JSON format more adapted for YAML export"""
568-
return util.dict_to_list(util.remove_nones(original_json), "name")

sonar/qualityprofiles.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -917,14 +917,3 @@ def convert_one_qp_yaml(qp: types.ObjectJsonRepr) -> types.ObjectJsonRepr:
917917
qp[_CHILDREN_KEY] = {k: convert_one_qp_yaml(q) for k, q in qp[_CHILDREN_KEY].items()}
918918
qp[_CHILDREN_KEY] = util.dict_to_list(qp[_CHILDREN_KEY], "name")
919919
return qp
920-
921-
922-
def convert_for_yaml(original_json: types.ObjectJsonRepr) -> types.ObjectJsonRepr:
923-
"""Convert the original JSON defined for JSON export into a JSON format more adapted for YAML export"""
924-
new_json = {}
925-
for lang, qp_list in util.remove_nones(original_json).items():
926-
new_json[lang] = {"profiles": util.dict_to_list(qp_list, "name")}
927-
new_json = util.dict_to_list(new_json, "language")
928-
for lang in new_json:
929-
lang["profiles"] = [convert_one_qp_yaml(qp) for qp in lang["profiles"]]
930-
return new_json

sonar/rules.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,6 @@ def convert_rule_list_for_yaml(rule_list: types.ObjectJsonRepr) -> list[types.Ob
562562
return utilities.dict_to_list(rule_list, "key", "severity")
563563

564564

565-
def convert_for_yaml(original_json: types.ObjectJsonRepr) -> types.ObjectJsonRepr:
566-
"""Convert the original JSON defined for JSON export into a JSON format more adapted for YAML export"""
567-
clean_json = utilities.remove_nones(original_json)
568-
return {category: convert_rule_list_for_yaml(clean_json[category]) for category in ("instantiated", "extended") if category in clean_json}
569-
570-
571565
def third_party(endpoint: platform.Platform) -> list[Rule]:
572566
"""Returns the list of rules coming from 3rd party plugins"""
573567
return [r for r in get_list(endpoint=endpoint).values() if r.repo and r.repo not in SONAR_REPOS and not r.repo.startswith("external_")]

0 commit comments

Comments
 (0)