Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions sonar/portfolios.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,9 +625,8 @@ def get_list(endpoint: pf.Platform, key_list: types.KeyList = None, use_cache: b
"""
with _CLASS_LOCK:
if key_list is None or len(key_list) == 0 or not use_cache:
log.info("Listing portfolios")
log.debug("Listing portfolios")
object_list = search(endpoint=endpoint)
log.info("List = %s", ", ".join(list(object_list.keys())))
return object_list
object_list = {}
for key in util.csv_to_list(key_list):
Expand Down
54 changes: 2 additions & 52 deletions sonar/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,10 @@ def get_object(endpoint: platform.Platform, key: str) -> Optional[Rule]:
return None


def export_all(endpoint: platform.Platform, full: bool = False) -> types.ObjectJsonRepr:
def export(endpoint: platform.Platform, export_settings: types.ConfigSettings, key_list: types.KeyList = None) -> types.ObjectJsonRepr:
"""Returns a JSON export of all rules"""
log.info("Exporting rules")
full = export_settings.get("FULL_EXPORT", False)
rule_list, other_rules, instantiated_rules, extended_rules = {}, {}, {}, {}
for rule_key, rule in get_list(endpoint=endpoint).items():
rule_export = rule.export(full)
Expand All @@ -251,57 +252,6 @@ def export_all(endpoint: platform.Platform, full: bool = False) -> types.ObjectJ
return rule_list


def export_instantiated(endpoint: platform.Platform, full: bool = False) -> Optional[types.ObjectJsonRepr]:
"""Returns a JSON of all instantiated rules"""
rule_list = {}
for template_key in get_list(endpoint=endpoint, is_template="true"):
for rule_key, rule in get_list(endpoint=endpoint, template_key=template_key).items():
rule_list[rule_key] = rule.export(full)
return rule_list if len(rule_list) > 0 else None


def export_customized(endpoint: platform.Platform, full: bool = False) -> Optional[types.ObjectJsonRepr]:
"""Returns a JSON export of all customized rules (custom tags or description added)"""
rule_list = {}
for rule_key, rule in get_list(endpoint=endpoint, is_template="false").items():
if rule.tags is None and rule.custom_desc is None:
continue
if full:
rule_list[rule_key] = rule.export(full)
continue
rule_list[rule_key] = {}
if rule.tags:
rule_list[rule_key]["tags"] = utilities.list_to_csv(rule.tags, ", ")
if rule.custom_desc:
rule_list[rule_key]["description"] = rule.custom_desc
return rule_list if len(rule_list) > 0 else None


def export_needed(endpoint: platform.Platform, instantiated: bool = True, extended: bool = True, full: bool = False) -> types.ObjectJsonRepr:
"""Returns a JSON export selected / needed rules"""
rule_list = {}
if instantiated:
rule_list["instantiated"] = export_instantiated(endpoint, full)
if extended:
rule_list["extended"] = export_customized(endpoint, full)
return utilities.remove_nones(rule_list)


def export(endpoint: platform.Platform, export_settings: types.ConfigSettings, key_list: types.KeyList = None) -> types.ObjectJsonRepr:
"""Returns a dict of rules for export
:param Platform endpoint: The SonarQube Platform object to connect to
:param ConfigSettings export_settings: parameters to export
:param KeyList key_list: Unused
:return: a dict of rules with their JSON representation
:rtype: ObjectJsonRepr
"""
log.info("Exporting rules")
# if standard:
return export_all(endpoint, export_settings["FULL_EXPORT"])
# else:
# return export_needed(endpoint, instantiated, extended, export_settings["FULL_EXPORT"])


def import_config(endpoint: platform.Platform, config_data: types.ObjectJsonRepr, key_list: types.KeyList = None) -> bool:
"""Imports a sonar-config configuration"""
if "rules" not in config_data:
Expand Down
6 changes: 3 additions & 3 deletions test/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ def test_get_rule_cache() -> None:

def test_export_not_full() -> None:
"""test_export_not_full"""
rule_list = rules.export_all(endpoint=util.SQ, full=False)
rule_list = rules.export(endpoint=util.SQ, export_settings={"FULL_EXPORT": False})
assert len(rule_list["extended"]) > 0
rule_list = rules.export_all(endpoint=util.SQ, full=True)
rule_list = rules.export(endpoint=util.SQ, export_settings={"FULL_EXPORT": True})
assert len(rule_list["extended"]) > 0


Expand All @@ -170,7 +170,7 @@ def test_get_nonexisting_rule() -> None:

def test_export_all() -> None:
"""test_export_all"""
rule_list = rules.export_all(endpoint=util.SQ, full=True)
rule_list = rules.export(endpoint=util.SQ, export_settings={"FULL_EXPORT": True})
if util.SQ.version() < (10, 0, 0) and util.SQ.edition() == "community":
assert len(rule_list.get("standard", {})) > 2800
else:
Expand Down