|
22 | 22 | Exports SonarQube platform configuration as JSON |
23 | 23 | """ |
24 | 24 |
|
25 | | -from typing import TextIO, Any |
| 25 | +from typing import TextIO |
26 | 26 | from threading import Thread |
27 | 27 | from queue import Queue |
28 | 28 |
|
@@ -122,20 +122,16 @@ def __parse_args(desc: str) -> object: |
122 | 122 | return options.parse_and_check(parser=parser, logger_name=TOOL_NAME) |
123 | 123 |
|
124 | 124 |
|
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]: |
126 | 126 | """Sorts a JSON file and optionally remove empty and none values""" |
127 | 127 | 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) |
129 | 132 | json_data = utilities.order_keys(json_data, *_SECTIONS_ORDER) |
130 | 133 | 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])} |
139 | 135 | return json_data |
140 | 136 |
|
141 | 137 |
|
@@ -175,37 +171,28 @@ def write_objects(queue: Queue[types.ObjectJsonRepr], fd: TextIO, object_type: s |
175 | 171 | """ |
176 | 172 | done = False |
177 | 173 | prefix = "" |
178 | | - objects_exported_as_lists = ("projects", "applications", "users", "portfolios") |
179 | | - objects_exported_as_whole = ("qualityGates", "groups") |
180 | 174 | 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) |
188 | 176 | while not done: |
189 | 177 | obj_json = queue.get() |
190 | 178 | if not (done := obj_json is utilities.WRITE_END): |
191 | 179 | if object_type == "groups": |
192 | 180 | obj_json = __prep_json_for_write(obj_json, {**export_settings, EXPORT_EMPTY: True}) |
193 | 181 | else: |
194 | 182 | 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) |
202 | 189 | print(f'{prefix}"{key}": {utilities.json_dump(obj_json)}', end="", file=fd) |
203 | 190 | else: |
204 | 191 | log.debug("Writing %s", object_type) |
205 | 192 | print(f"{prefix}{utilities.json_dump(obj_json)[2:-1]}", end="", file=fd) |
206 | 193 | prefix = ",\n" |
207 | 194 | queue.task_done() |
208 | | - print("\n" + stop, file=fd, end="") |
| 195 | + print("\n}", file=fd, end="") |
209 | 196 | log.info("Writing %s complete", object_type) |
210 | 197 |
|
211 | 198 |
|
@@ -270,7 +257,10 @@ def __prep_json_for_write(json_data: types.ObjectJsonRepr, export_settings: type |
270 | 257 | if export_settings.get("MODE", "CONFIG") == "MIGRATION": |
271 | 258 | return json_data |
272 | 259 | 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) |
274 | 264 | if export_settings.get("INLINE_LISTS", True): |
275 | 265 | json_data = utilities.inline_lists(json_data, exceptions=("conditions",)) |
276 | 266 | return json_data |
|
0 commit comments