Skip to content

Commit ad2fe8f

Browse files
committed
Export users as list
1 parent 9a78a09 commit ad2fe8f

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

cli/config.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,15 @@ def __parse_args(desc: str) -> object:
124124

125125
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"""
127+
SORT_FIELDS = {"users": "login"}
127128
log.info("Normalizing JSON - remove empty = %s, remove nones = %s", str(remove_empty), str(remove_none))
128129
json_data = utilities.clean_data(json_data, remove_none=remove_none, remove_empty=remove_empty)
129130
json_data = utilities.order_keys(json_data, *_SECTIONS_ORDER)
130131
for key in [k for k in _SECTIONS_TO_SORT if k in json_data]:
131-
json_data[key] = {k: json_data[key][k] for k in sorted(json_data[key])}
132+
if isinstance(json_data[key], dict):
133+
json_data[key] = {k: json_data[key][k] for k in sorted(json_data[key])}
134+
else:
135+
json_data[key] = utilities.sort_list_by_key(json_data[key], SORT_FIELDS.get(key, "key"))
132136
return json_data
133137

134138

@@ -169,27 +173,37 @@ def write_objects(queue: Queue[types.ObjectJsonRepr], fd: TextIO, object_type: s
169173
done = False
170174
prefix = ""
171175
log.info("Waiting %s to write...", object_type)
172-
print(f'"{object_type}": ' + "{", file=fd)
176+
objects_exported_as_lists = ("projects", "applications", "users", "portfolios")
177+
objects_exported_as_whole = ("qualityGates", "groups")
178+
log.info("Waiting %s to write...", object_type)
179+
if object_type in objects_exported_as_lists:
180+
start, stop = ("[", "]")
181+
elif object_type in objects_exported_as_whole:
182+
start, stop = ("", "")
183+
else:
184+
start, stop = ("{", "}")
185+
print(f'"{object_type}": ' + start, file=fd)
173186
while not done:
174187
obj_json = queue.get()
175188
if not (done := obj_json is utilities.WRITE_END):
176189
if object_type == "groups":
177190
obj_json = __prep_json_for_write(obj_json, {**export_settings, EXPORT_EMPTY: True})
178191
else:
179192
obj_json = __prep_json_for_write(obj_json, export_settings)
180-
if object_type in ("projects", "applications", "portfolios", "users"):
181-
if object_type == "users":
182-
key = obj_json.pop("login", None)
183-
else:
184-
key = obj_json.pop("key", None)
185-
log.debug("Writing %s key '%s'", object_type[:-1], key)
193+
key = "" if isinstance(obj_json, list) else obj_json.get("key", obj_json.get("login", obj_json.get("name", "unknown")))
194+
log.debug("Writing %s key '%s'", object_type[:-1], key)
195+
if object_type in objects_exported_as_lists:
196+
print(f"{prefix}{utilities.json_dump(obj_json)}", end="", file=fd)
197+
elif object_type in objects_exported_as_whole:
198+
print(f"{prefix}{utilities.json_dump(obj_json)}", end="", file=fd)
199+
elif object_type in ("applications", "portfolios", "users"):
186200
print(f'{prefix}"{key}": {utilities.json_dump(obj_json)}', end="", file=fd)
187201
else:
188202
log.debug("Writing %s", object_type)
189203
print(f"{prefix}{utilities.json_dump(obj_json)[2:-1]}", end="", file=fd)
190204
prefix = ",\n"
191205
queue.task_done()
192-
print("\n}", file=fd, end="")
206+
print("\n" + stop, file=fd, end="")
193207
log.info("Writing %s complete", object_type)
194208

195209

sonar/users.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,13 +483,12 @@ def export(endpoint: pf.Platform, export_settings: types.ConfigSettings, **kwarg
483483
"""
484484
log.info("Exporting users")
485485
write_q = kwargs.get("write_q", None)
486-
u_list = {}
487-
for u_login, u_obj in sorted(search(endpoint=endpoint).items()):
488-
u_list[u_login] = u_obj.to_json(export_settings)
486+
u_list = []
487+
for _, u_obj in sorted(search(endpoint=endpoint).items()):
488+
u_data = u_obj.to_json(export_settings)
489+
u_list.append(u_data)
489490
if write_q:
490-
write_q.put(u_list[u_login])
491-
else:
492-
u_list[u_login].pop("login", None)
491+
write_q.put(u_data)
493492
write_q and write_q.put(util.WRITE_END)
494493
return u_list
495494

0 commit comments

Comments
 (0)