diff --git a/cli/config.py b/cli/config.py index 026047f5..69b9c5ea 100644 --- a/cli/config.py +++ b/cli/config.py @@ -263,7 +263,7 @@ def __prep_json_for_write(json_data: types.ObjectJsonRepr, export_settings: type json_data = utilities.remove_nones(json_data) if not export_settings.get(EXPORT_EMPTY, False): log.debug("Removing empties") - json_data = utilities.clean_data(json_data, remove_empty=True) + json_data = utilities.clean_data(json_data, remove_empty=False) return json_data diff --git a/sonar/applications.py b/sonar/applications.py index 55eb679f..326e643e 100644 --- a/sonar/applications.py +++ b/sonar/applications.py @@ -604,9 +604,18 @@ def search_by_name(endpoint: pf.Platform, name: str) -> dict[str, Application]: def convert_app_json(old_app_json: dict[str, Any]) -> dict[str, Any]: """Converts sonar-config old JSON report format to new format for a single application""" new_json = common_json_helper.convert_common_fields(old_app_json.copy()) - if "branches" in old_app_json: - new_json["branches"] = util.dict_to_list(old_app_json["branches"], "name") - return new_json + if "branches" not in new_json: + return new_json + for br, data in new_json["branches"].items(): + if "projects" not in data: + continue + new_json["branches"][br] = util.order_dict(data, ["name", "isMain", "projects"]) + new_json["branches"][br]["projects"] = util.dict_to_list(new_json["branches"][br]["projects"], "key", "branch") + for proj_data in new_json["branches"][br]["projects"]: + if proj_data.get("branch", None) in ("__default__", c.DEFAULT_BRANCH): + proj_data.pop("branch") + new_json["branches"] = util.sort_list_by_key(util.dict_to_list(new_json["branches"], "name"), "name", "isMain") + return util.order_dict(new_json, ["key", "name", "visibility", "tags", "branches", "permissions"]) def convert_apps_json(old_json: dict[str, Any]) -> dict[str, Any]: diff --git a/sonar/util/portfolio_helper.py b/sonar/util/portfolio_helper.py index 68397d52..41317460 100644 --- a/sonar/util/portfolio_helper.py +++ b/sonar/util/portfolio_helper.py @@ -21,6 +21,7 @@ from typing import Any from sonar import utilities as util +from sonar.util import constants as c from sonar.util import common_json_helper @@ -29,11 +30,41 @@ def convert_portfolio_json(old_json: dict[str, Any]) -> dict[str, Any]: new_json = common_json_helper.convert_common_fields(old_json.copy()) if "projects" in new_json: new_json["projects"] = common_json_helper.convert_common_fields(new_json["projects"]) + if "manual" in new_json["projects"]: + projs = {} + for k, v in new_json["projects"]["manual"].items(): + projs[k] = {"key": k, "branches": sorted(["" if e == c.DEFAULT_BRANCH else e for e in util.csv_to_list(v)])} + if projs[k]["branches"] == [""]: + projs[k].pop("branches", None) + new_json["projectSelection"] = {"manual": new_json["projects"]} + new_json["projectSelection"]["manual"] = util.dict_to_list(projs, "key", "branches") + elif "regexp" in new_json["projects"]: + new_json["projectSelection"] = "regexp" + new_json["projectSelection"] = {"regexp": new_json["projects"]["regexp"]} + if new_json["projects"].get("branch", c.DEFAULT_BRANCH) != c.DEFAULT_BRANCH: + new_json["projectSelection"]["branch"] = new_json["projects"]["branch"] + elif "tags" in new_json["projects"]: + new_json["projectSelection"] = {"tags": new_json["projects"]["tags"]} + if new_json["projects"].get("branch", c.DEFAULT_BRANCH) != c.DEFAULT_BRANCH: + new_json["projectSelection"]["branch"] = new_json["projects"]["branch"] + elif "rest" in new_json["projects"]: + new_json["projectSelection"] = {"rest": True} + if new_json["projects"].get("branch", c.DEFAULT_BRANCH) != c.DEFAULT_BRANCH: + new_json["projectSelection"]["branch"] = new_json["projects"]["branch"] + new_json.pop("projects") + if "applications" in new_json: + for k, v in new_json["applications"].items(): + new_json["applications"][k] = {"key": k, "branches": sorted(["" if e == c.DEFAULT_BRANCH else e for e in util.csv_to_list(v)])} + if new_json["applications"][k]["branches"] == [""]: + new_json["applications"][k].pop("branches", None) + new_json["applications"] = util.dict_to_list(new_json["applications"], "key", "branches") + for key in "children", "portfolios": if key in new_json: new_json[key] = convert_portfolios_json(new_json[key]) if "branches" in old_json: new_json["branches"] = util.dict_to_list(old_json["branches"], "name") + new_json = util.order_keys(new_json, "key", "name", "visibility", "projectSelection", "applications", "portfolios", "permissions") return new_json