Skip to content

Commit 7bff6e8

Browse files
committed
Revert "Change-sonar-config-format (#2040)"
This reverts commit 4651e56.
1 parent d3944de commit 7bff6e8

19 files changed

+224
-326
lines changed

cli/config.py

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
Exports SonarQube platform configuration as JSON
2323
"""
2424

25-
from typing import TextIO, Any
25+
from typing import TextIO
2626
from threading import Thread
2727
from queue import Queue
2828

@@ -122,20 +122,16 @@ def __parse_args(desc: str) -> object:
122122
return options.parse_and_check(parser=parser, logger_name=TOOL_NAME)
123123

124124

125-
def __normalize_json(json_data: dict[str, Any], remove_empty: bool = True, remove_none: bool = True) -> dict[str, any]:
125+
def __normalize_json(json_data: dict[str, any], remove_empty: bool = True, remove_none: bool = True) -> dict[str, any]:
126126
"""Sorts a JSON file and optionally remove empty and none values"""
127127
log.info("Normalizing JSON - remove empty = %s, remove nones = %s", str(remove_empty), str(remove_none))
128-
json_data = utilities.clean_data(json_data, remove_empty, remove_none)
128+
if remove_empty:
129+
json_data = utilities.remove_empties(json_data)
130+
if remove_none:
131+
json_data = utilities.remove_nones(json_data)
129132
json_data = utilities.order_keys(json_data, *_SECTIONS_ORDER)
130133
for key in [k for k in _SECTIONS_TO_SORT if k in json_data]:
131-
if isinstance(json_data[key], (list, tuple, set)):
132-
if len(json_data[key]) > 0:
133-
sort_field = next((k for k in ("key", "name", "login") if k in json_data[key][0]), None)
134-
if sort_field:
135-
tmp_d = {v[sort_field]: v for v in json_data[key]}
136-
json_data[key] = list(dict(sorted(tmp_d.items())).values())
137-
else:
138-
json_data[key] = {k: json_data[key][k] for k in sorted(json_data[key])}
134+
json_data[key] = {k: json_data[key][k] for k in sorted(json_data[key])}
139135
return json_data
140136

141137

@@ -175,37 +171,28 @@ def write_objects(queue: Queue[types.ObjectJsonRepr], fd: TextIO, object_type: s
175171
"""
176172
done = False
177173
prefix = ""
178-
objects_exported_as_lists = ("projects", "applications", "users", "portfolios")
179-
objects_exported_as_whole = ("qualityGates", "groups")
180174
log.info("Waiting %s to write...", object_type)
181-
if object_type in objects_exported_as_lists:
182-
start, stop = ("[", "]")
183-
elif object_type in objects_exported_as_whole:
184-
start, stop = ("", "")
185-
else:
186-
start, stop = ("{", "}")
187-
print(f'"{object_type}": ' + start, file=fd)
175+
print(f'"{object_type}": ' + "{", file=fd)
188176
while not done:
189177
obj_json = queue.get()
190178
if not (done := obj_json is utilities.WRITE_END):
191179
if object_type == "groups":
192180
obj_json = __prep_json_for_write(obj_json, {**export_settings, EXPORT_EMPTY: True})
193181
else:
194182
obj_json = __prep_json_for_write(obj_json, export_settings)
195-
key = "" if isinstance(obj_json, list) else obj_json.get("key", obj_json.get("login", obj_json.get("name", "unknown")))
196-
log.debug("Writing %s key '%s'", object_type[:-1], key)
197-
if object_type in objects_exported_as_lists:
198-
print(f"{prefix}{utilities.json_dump(obj_json)}", end="", file=fd)
199-
elif object_type in objects_exported_as_whole:
200-
print(f"{prefix}{utilities.json_dump(obj_json)}", end="", file=fd)
201-
elif object_type in ("applications", "portfolios", "users"):
183+
if object_type in ("projects", "applications", "portfolios", "users"):
184+
if object_type == "users":
185+
key = obj_json.pop("login", None)
186+
else:
187+
key = obj_json.pop("key", None)
188+
log.debug("Writing %s key '%s'", object_type[:-1], key)
202189
print(f'{prefix}"{key}": {utilities.json_dump(obj_json)}', end="", file=fd)
203190
else:
204191
log.debug("Writing %s", object_type)
205192
print(f"{prefix}{utilities.json_dump(obj_json)[2:-1]}", end="", file=fd)
206193
prefix = ",\n"
207194
queue.task_done()
208-
print("\n" + stop, file=fd, end="")
195+
print("\n}", file=fd, end="")
209196
log.info("Writing %s complete", object_type)
210197

211198

@@ -270,7 +257,10 @@ def __prep_json_for_write(json_data: types.ObjectJsonRepr, export_settings: type
270257
if export_settings.get("MODE", "CONFIG") == "MIGRATION":
271258
return json_data
272259
if not export_settings.get("FULL_EXPORT", False):
273-
json_data = utilities.clean_data(json_data, remove_empty=not export_settings.get(EXPORT_EMPTY, False), remove_none=True)
260+
json_data = utilities.remove_nones(json_data)
261+
if not export_settings.get(EXPORT_EMPTY, False):
262+
log.debug("Removing empties")
263+
json_data = utilities.remove_empties(json_data)
274264
if export_settings.get("INLINE_LISTS", True):
275265
json_data = utilities.inline_lists(json_data, exceptions=("conditions",))
276266
return json_data

conf/build.sh

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ CONF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2121

2222
build_docs=0
2323
build_docker=0
24-
offline=0
2524

2625
. "${CONF_DIR}/env.sh"
2726

@@ -33,9 +32,6 @@ while [[ $# -ne 0 ]]; do
3332
docker)
3433
build_docker=1
3534
;;
36-
offline)
37-
offline=1
38-
;;
3935
*)
4036
;;
4137
esac
@@ -45,18 +41,9 @@ done
4541
echo "======= FORMATTING CODE ========="
4642
ruff format
4743
echo "======= BUILDING PACKAGE ========="
48-
if [[ "${offline}" = "1" ]]; then
49-
cp "${ROOT_DIR}/conf/offline/setup.py" "${ROOT_DIR}/"
50-
cp "${ROOT_DIR}/conf/offline/sonar-tools" "${ROOT_DIR}/"
51-
mv "${ROOT_DIR}/pyproject.toml" "${ROOT_DIR}/pyproject.toml.bak"
52-
python setup.py bdist_wheel
53-
mv "${ROOT_DIR}/pyproject.toml.bak" "${ROOT_DIR}/pyproject.toml"
54-
rm "${ROOT_DIR}/setup.py" "${ROOT_DIR}/sonar-tools"
55-
# python -m build
56-
else
57-
rm -rf "${ROOT_DIR}/build/lib/sonar" "${ROOT_DIR}/build/lib/cli" "${ROOT_DIR}"/build/scripts*/sonar-tools "${ROOT_DIR}"/dist/sonar_tools*
58-
poetry build
59-
fi
44+
rm -rf "${ROOT_DIR}/build/lib/sonar" "${ROOT_DIR}/build/lib/cli" "${ROOT_DIR}"/build/scripts*/sonar-tools "${ROOT_DIR}"/dist/sonar_tools*
45+
# python -m build
46+
poetry build
6047

6148
if [[ "${build_docs}" = "1" ]]; then
6249
echo "======= BUILDING DOCS ========="

conf/offline/setup.py

Lines changed: 0 additions & 84 deletions
This file was deleted.

conf/offline/sonar-tools

Lines changed: 0 additions & 45 deletions
This file was deleted.

sonar/app_branches.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,10 @@ def export(self) -> types.ObjectJsonRepr:
171171
:param full: Whether to do a full export including settings that can't be set, defaults to False
172172
:type full: bool, optional
173173
"""
174-
log.info("Exporting %s from %s", self, utilities.json_dump(self.sq_json))
175-
jsondata = {"name": self.name}
174+
log.info("Exporting %s from %s", self, self.sq_json)
175+
jsondata = {"projects": {b["key"]: b["branch"] if b["selected"] else utilities.DEFAULT for b in self.sq_json["projects"]}}
176176
if self.is_main():
177177
jsondata["isMain"] = True
178-
br_projects = [b for b in self.sq_json["projects"] if b.get("selected", True)]
179-
br_projects = [{"key": b["key"], "branch": None if b["isMain"] else b["branch"]} for b in br_projects]
180-
jsondata["projects"] = utilities.remove_nones(br_projects)
181178
return jsondata
182179

183180
def update(self, name: str, project_branches: list[Branch]) -> bool:

sonar/applications.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,12 @@ def export(self, export_settings: types.ConfigSettings) -> types.ObjectJsonRepr:
339339
"description": None if self._description == "" else self._description,
340340
"visibility": self.visibility(),
341341
# 'projects': self.projects(),
342-
"branches": [br.export() for br in self.branches().values()],
342+
"branches": {br.name: br.export() for br in self.branches().values()},
343343
"permissions": self.permissions().export(export_settings=export_settings),
344344
"tags": self.get_tags(),
345345
}
346346
)
347-
json_data = util.filter_export(json_data, _IMPORTABLE_PROPERTIES, export_settings.get("FULL_EXPORT", False))
348-
return util.clean_data(json_data)
347+
return util.filter_export(json_data, _IMPORTABLE_PROPERTIES, export_settings.get("FULL_EXPORT", False))
349348

350349
def set_permissions(self, data: types.JsonPermissions) -> application_permissions.ApplicationPermissions:
351350
"""Sets an application permissions
@@ -526,9 +525,11 @@ def export(endpoint: pf.Platform, export_settings: types.ConfigSettings, **kwarg
526525
app_json = app.export(export_settings)
527526
if write_q:
528527
write_q.put(app_json)
529-
apps_settings[k] = app_json
528+
else:
529+
app_json.pop("key")
530+
apps_settings[k] = app_json
530531
write_q and write_q.put(util.WRITE_END)
531-
return dict(sorted(app_json.items())).values()
532+
return apps_settings
532533

533534

534535
def audit(endpoint: pf.Platform, audit_settings: types.ConfigSettings, **kwargs) -> list[problem.Problem]:
@@ -601,4 +602,12 @@ def search_by_name(endpoint: pf.Platform, name: str) -> dict[str, Application]:
601602

602603
def convert_for_yaml(original_json: types.ObjectJsonRepr) -> types.ObjectJsonRepr:
603604
"""Convert the original JSON defined for JSON export into a JSON format more adapted for YAML export"""
604-
return original_json
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/devops.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,12 @@ def export(endpoint: platform.Platform, export_settings: types.ConfigSettings) -
239239
:meta private:
240240
"""
241241
log.info("Exporting DevOps integration settings")
242-
devops_list = {s.key: s.to_json(export_settings) for s in get_list(endpoint).values()}
243-
return list(dict(sorted(devops_list.items())).values())
242+
json_data = {}
243+
for s in get_list(endpoint).values():
244+
export_data = s.to_json(export_settings)
245+
key = export_data.pop("key")
246+
json_data[key] = export_data
247+
return json_data
244248

245249

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

sonar/groups.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,11 @@ def export(endpoint: pf.Platform, export_settings: types.ConfigSettings, **kwarg
345345
"""
346346

347347
log.info("Exporting groups")
348-
g_list = []
348+
g_list = {}
349349
for g_name, g_obj in get_list(endpoint=endpoint).items():
350350
if not export_settings.get("FULL_EXPORT", False) and g_obj.is_default():
351351
continue
352-
g_list.append({"name": g_name, "description": g_obj.description})
353-
util.clean_data(g_list, remove_empty=False)
352+
g_list[g_name] = "" if g_obj.description is None else g_obj.description
354353
log.info("%s groups to export", len(g_list))
355354
if write_q := kwargs.get("write_q", None):
356355
write_q.put(g_list)

0 commit comments

Comments
 (0)