Skip to content

Commit 079f6e7

Browse files
committed
Move stuff in platform_helper
1 parent d8681e0 commit 079f6e7

File tree

2 files changed

+65
-40
lines changed

2 files changed

+65
-40
lines changed

sonar/platform.py

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import sonar.utilities as util
4141
from sonar.util import types, update_center
4242
import sonar.util.constants as c
43+
import sonar.util.platform_helper as pfhelp
4344

4445
from sonar import errcodes, settings, devops, version, sif, exceptions, organizations
4546
from sonar.permissions import permissions, global_permissions, permission_templates
@@ -262,7 +263,7 @@ def delete(self, api: str, params: types.ApiParams = None, **kwargs) -> requests
262263
def __run_request(self, request: callable, api: str, params: types.ApiParams = None, **kwargs) -> requests.Response:
263264
"""Makes an HTTP request to SonarQube"""
264265
mute = kwargs.pop("mute", ())
265-
api = _normalize_api(api)
266+
api = pfhelp.normalize_api(api)
266267
headers = {"user-agent": self._user_agent, "accept": _APP_JSON} | kwargs.get("headers", {})
267268
params = params or {}
268269
with_org = kwargs.pop("with_organization", True)
@@ -791,19 +792,6 @@ def set_standard_experience(self) -> bool:
791792
this.context = Platform(os.getenv("SONAR_HOST_URL", "http://localhost:9000"), os.getenv("SONAR_TOKEN", ""))
792793

793794

794-
def _normalize_api(api: str) -> str:
795-
"""Normalizes an API based on its multiple original forms"""
796-
if api.startswith("/api/"):
797-
pass
798-
elif api.startswith("api/"):
799-
api = "/" + api
800-
elif api.startswith("/"):
801-
api = "/api" + api
802-
else:
803-
api = "/api/" + api
804-
return api
805-
806-
807795
def _audit_setting_value(key: str, platform_settings: dict[str, Any], audit_settings: types.ConfigSettings, url: str) -> list[Problem]:
808796
"""Audits a particular platform setting is set to expected value"""
809797
if (v := _get_multiple_values(4, audit_settings[key], "MEDIUM", "CONFIGURATION")) is None:
@@ -963,29 +951,3 @@ def audit(endpoint: Platform, audit_settings: types.ConfigSettings, **kwargs) ->
963951
pbs = endpoint.audit(audit_settings)
964952
"write_q" in kwargs and kwargs["write_q"].put(pbs)
965953
return pbs
966-
967-
968-
def old_to_new_json(old_json: dict[str, Any]) -> dict[str, Any]:
969-
"""Converts sonar-config "plaform" section old JSON report format to new format"""
970-
if "plugins" in old_json:
971-
old_json["plugins"] = util.dict_to_list(old_json["plugins"], "key")
972-
return old_json
973-
974-
975-
def global_settings_old_to_new_json(old_json: dict[str, Any]) -> dict[str, Any]:
976-
"""Converts sonar-config "globalSettings" section old JSON report format to new format"""
977-
new_json = {}
978-
special_categories = (settings.LANGUAGES_SETTINGS, settings.DEVOPS_INTEGRATION, "permissions", "permissionTemplates")
979-
for categ in [cat for cat in settings.CATEGORIES if cat not in special_categories]:
980-
new_json[categ] = util.sort_list_by_key(util.dict_to_list(old_json[categ], "key"), "key")
981-
for k, v in old_json[settings.LANGUAGES_SETTINGS].items():
982-
new_json[settings.LANGUAGES_SETTINGS] = new_json.get(settings.LANGUAGES_SETTINGS, None) or {}
983-
new_json[settings.LANGUAGES_SETTINGS][k] = util.sort_list_by_key(util.dict_to_list(v, "key"), "key")
984-
new_json[settings.LANGUAGES_SETTINGS] = util.dict_to_list(new_json[settings.LANGUAGES_SETTINGS], "language", "settings")
985-
new_json[settings.DEVOPS_INTEGRATION] = util.dict_to_list(old_json[settings.DEVOPS_INTEGRATION], "key")
986-
new_json["permissions"] = util.perms_to_list(old_json["permissions"])
987-
for v in old_json["permissionTemplates"].values():
988-
if "permissions" in v:
989-
v["permissions"] = util.perms_to_list(v["permissions"])
990-
new_json["permissionTemplates"] = util.dict_to_list(old_json["permissionTemplates"], "key")
991-
return new_json

sonar/util/platform_helper.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#
2+
# sonar-tools
3+
# Copyright (C) 2025 Olivier Korach
4+
# mailto:olivier.korach AT gmail DOT com
5+
#
6+
# This program is free software; you can redistribute it and/or
7+
# modify it under the terms of the GNU Lesser General Public
8+
# License as published by the Free Software Foundation; either
9+
# version 3 of the License, or (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
# Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with this program; if not, write to the Free Software Foundation,
18+
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
#
20+
"""Helper tools for the Platform object"""
21+
22+
from typing import Any
23+
import settings
24+
import utilities as util
25+
26+
27+
def normalize_api(api: str) -> str:
28+
"""Normalizes an API based on its multiple original forms"""
29+
if api.startswith("/api/"):
30+
pass
31+
elif api.startswith("api/"):
32+
api = "/" + api
33+
elif api.startswith("/"):
34+
api = "/api" + api
35+
else:
36+
api = "/api/" + api
37+
return api
38+
39+
40+
def old_to_new_json(old_json: dict[str, Any]) -> dict[str, Any]:
41+
"""Converts sonar-config "plaform" section old JSON report format to new format"""
42+
if "plugins" in old_json:
43+
old_json["plugins"] = util.dict_to_list(old_json["plugins"], "key")
44+
return old_json
45+
46+
47+
def global_settings_old_to_new_json(old_json: dict[str, Any]) -> dict[str, Any]:
48+
"""Converts sonar-config "globalSettings" section old JSON report format to new format"""
49+
new_json = {}
50+
special_categories = (settings.LANGUAGES_SETTINGS, settings.DEVOPS_INTEGRATION, "permissions", "permissionTemplates")
51+
for categ in [cat for cat in settings.CATEGORIES if cat not in special_categories]:
52+
new_json[categ] = util.sort_list_by_key(util.dict_to_list(old_json[categ], "key"), "key")
53+
for k, v in old_json[settings.LANGUAGES_SETTINGS].items():
54+
new_json[settings.LANGUAGES_SETTINGS] = new_json.get(settings.LANGUAGES_SETTINGS, None) or {}
55+
new_json[settings.LANGUAGES_SETTINGS][k] = util.sort_list_by_key(util.dict_to_list(v, "key"), "key")
56+
new_json[settings.LANGUAGES_SETTINGS] = util.dict_to_list(new_json[settings.LANGUAGES_SETTINGS], "language", "settings")
57+
new_json[settings.DEVOPS_INTEGRATION] = util.dict_to_list(old_json[settings.DEVOPS_INTEGRATION], "key")
58+
new_json["permissions"] = util.perms_to_list(old_json["permissions"])
59+
for v in old_json["permissionTemplates"].values():
60+
if "permissions" in v:
61+
v["permissions"] = util.perms_to_list(v["permissions"])
62+
new_json["permissionTemplates"] = util.dict_to_list(old_json["permissionTemplates"], "key")
63+
return new_json

0 commit comments

Comments
 (0)