Skip to content

Commit ee89b91

Browse files
authored
Export global settings as lists (#2060)
* Export global settings as lists * Fix open_file type hint ot generator
1 parent de0d422 commit ee89b91

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

sonar/permissions/permission_templates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
_CREATE_API = "permissions/create_template"
4343
_UPDATE_API = "permissions/update_template"
4444

45-
_IMPORTABLE_PROPERTIES = ("name", "description", "pattern", "permissions", "defaultFor")
45+
_IMPORTABLE_PROPERTIES = ("name", "description", "pattern", "defaultFor", "permissions")
4646

4747

4848
class PermissionTemplate(sqobject.SqObject):

sonar/platform.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,21 @@ def export(self, export_settings: types.ConfigSettings, full: bool = False) -> t
503503
if not self.is_sonarcloud():
504504
json_data[settings.DEVOPS_INTEGRATION] = devops.export(self, export_settings=export_settings)
505505

506-
order = list(settings.CATEGORIES) + ["permissions", "permissionTemplates"]
507-
return {k: json_data[k] for k in order if k in json_data} | {k: v for k, v in json_data.items() if k not in order}
506+
# Convert dicts to lists
507+
special_categories = (settings.LANGUAGES_SETTINGS, settings.DEVOPS_INTEGRATION, "permissions", "permissionTemplates")
508+
for categ in [c for c in settings.CATEGORIES if c not in special_categories]:
509+
json_data[categ] = util.sort_list_by_key(util.dict_to_list(json_data[categ], "key"), "key")
510+
for k, v in json_data[settings.LANGUAGES_SETTINGS].items():
511+
json_data[settings.LANGUAGES_SETTINGS][k] = util.sort_list_by_key(util.dict_to_list(v, "key"), "key")
512+
json_data[settings.LANGUAGES_SETTINGS] = util.dict_to_list(json_data[settings.LANGUAGES_SETTINGS], "language", "settings")
513+
json_data[settings.DEVOPS_INTEGRATION] = util.dict_to_list(json_data[settings.DEVOPS_INTEGRATION], "key")
514+
json_data["permissions"] = util.perms_to_list(json_data["permissions"])
515+
for k, v in json_data["permissionTemplates"].items():
516+
if "permissions" in v:
517+
v["permissions"] = util.perms_to_list(v["permissions"])
518+
json_data["permissionTemplates"] = util.dict_to_list(json_data["permissionTemplates"], "key")
519+
520+
return util.order_dict(json_data, list(settings.CATEGORIES) + ["permissions", "permissionTemplates"])
508521

509522
def set_webhooks(self, webhooks_data: types.ObjectJsonRepr) -> bool:
510523
"""Sets global webhooks with a list of webhooks represented as JSON

sonar/utilities.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"""
2525

2626
from typing import Any, TextIO, Union, Optional
27+
from collections.abc import Generator
2728
from http import HTTPStatus
2829
import sys
2930
import os
@@ -460,7 +461,7 @@ def is_api_v2(api: str) -> bool:
460461

461462

462463
@contextlib.contextmanager
463-
def open_file(file: Optional[str] = None, mode: str = "w") -> TextIO:
464+
def open_file(file: Optional[str] = None, mode: str = "w") -> Generator[TextIO, None, None]:
464465
"""Opens a file if not None or -, otherwise stdout"""
465466
if file and file != "-":
466467
log.debug("Opening file '%s' in directory '%s'", file, os.getcwd())
@@ -850,3 +851,8 @@ def order_list(l: list[str], *key_order) -> list[str]:
850851
"""Orders elements of a list in a given order"""
851852
new_l = [k for k in key_order if k in l]
852853
return new_l + [k for k in l if k not in new_l]
854+
855+
856+
def perms_to_list(perms: dict[str, Any]) -> list[str, Any]:
857+
"""Converts permissions in dict format to list format"""
858+
return dict_to_list(perms.get("groups", {}), "group", "permissions") + dict_to_list(perms.get("users", {}), "user", "permissions")

0 commit comments

Comments
 (0)