Skip to content

Commit 21e27df

Browse files
authored
Improve exp[ort of projects in apps (#2076)
* Fixes #2073 * Main branch exported first in apps export * Handle default branch in apps added to portfolios * Improvide export of apps and projects in portfolios * Quality pass
1 parent eb48dac commit 21e27df

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

cli/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def __prep_json_for_write(json_data: types.ObjectJsonRepr, export_settings: type
263263
json_data = utilities.remove_nones(json_data)
264264
if not export_settings.get(EXPORT_EMPTY, False):
265265
log.debug("Removing empties")
266-
json_data = utilities.clean_data(json_data, remove_empty=True)
266+
json_data = utilities.clean_data(json_data, remove_empty=False)
267267
return json_data
268268

269269

sonar/applications.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,9 +604,18 @@ def search_by_name(endpoint: pf.Platform, name: str) -> dict[str, Application]:
604604
def convert_app_json(old_app_json: dict[str, Any]) -> dict[str, Any]:
605605
"""Converts sonar-config old JSON report format to new format for a single application"""
606606
new_json = common_json_helper.convert_common_fields(old_app_json.copy())
607-
if "branches" in old_app_json:
608-
new_json["branches"] = util.dict_to_list(old_app_json["branches"], "name")
609-
return new_json
607+
if "branches" not in new_json:
608+
return new_json
609+
for br, data in new_json["branches"].items():
610+
if "projects" not in data:
611+
continue
612+
new_json["branches"][br] = util.order_dict(data, ["name", "isMain", "projects"])
613+
new_json["branches"][br]["projects"] = util.dict_to_list(new_json["branches"][br]["projects"], "key", "branch")
614+
for proj_data in new_json["branches"][br]["projects"]:
615+
if proj_data.get("branch", None) in ("__default__", c.DEFAULT_BRANCH):
616+
proj_data.pop("branch")
617+
new_json["branches"] = util.sort_list_by_key(util.dict_to_list(new_json["branches"], "name"), "name", "isMain")
618+
return util.order_dict(new_json, ["key", "name", "visibility", "tags", "branches", "permissions"])
610619

611620

612621
def convert_apps_json(old_json: dict[str, Any]) -> dict[str, Any]:

sonar/util/portfolio_helper.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from typing import Any
2323
from sonar import utilities as util
24+
from sonar.util import constants as c
2425
from sonar.util import common_json_helper
2526

2627

@@ -29,11 +30,41 @@ def convert_portfolio_json(old_json: dict[str, Any]) -> dict[str, Any]:
2930
new_json = common_json_helper.convert_common_fields(old_json.copy())
3031
if "projects" in new_json:
3132
new_json["projects"] = common_json_helper.convert_common_fields(new_json["projects"])
33+
if "manual" in new_json["projects"]:
34+
projs = {}
35+
for k, v in new_json["projects"]["manual"].items():
36+
projs[k] = {"key": k, "branches": sorted(["" if e == c.DEFAULT_BRANCH else e for e in util.csv_to_list(v)])}
37+
if projs[k]["branches"] == [""]:
38+
projs[k].pop("branches", None)
39+
new_json["projectSelection"] = {"manual": new_json["projects"]}
40+
new_json["projectSelection"]["manual"] = util.dict_to_list(projs, "key", "branches")
41+
elif "regexp" in new_json["projects"]:
42+
new_json["projectSelection"] = "regexp"
43+
new_json["projectSelection"] = {"regexp": new_json["projects"]["regexp"]}
44+
if new_json["projects"].get("branch", c.DEFAULT_BRANCH) != c.DEFAULT_BRANCH:
45+
new_json["projectSelection"]["branch"] = new_json["projects"]["branch"]
46+
elif "tags" in new_json["projects"]:
47+
new_json["projectSelection"] = {"tags": new_json["projects"]["tags"]}
48+
if new_json["projects"].get("branch", c.DEFAULT_BRANCH) != c.DEFAULT_BRANCH:
49+
new_json["projectSelection"]["branch"] = new_json["projects"]["branch"]
50+
elif "rest" in new_json["projects"]:
51+
new_json["projectSelection"] = {"rest": True}
52+
if new_json["projects"].get("branch", c.DEFAULT_BRANCH) != c.DEFAULT_BRANCH:
53+
new_json["projectSelection"]["branch"] = new_json["projects"]["branch"]
54+
new_json.pop("projects")
55+
if "applications" in new_json:
56+
for k, v in new_json["applications"].items():
57+
new_json["applications"][k] = {"key": k, "branches": sorted(["" if e == c.DEFAULT_BRANCH else e for e in util.csv_to_list(v)])}
58+
if new_json["applications"][k]["branches"] == [""]:
59+
new_json["applications"][k].pop("branches", None)
60+
new_json["applications"] = util.dict_to_list(new_json["applications"], "key", "branches")
61+
3262
for key in "children", "portfolios":
3363
if key in new_json:
3464
new_json[key] = convert_portfolios_json(new_json[key])
3565
if "branches" in old_json:
3666
new_json["branches"] = util.dict_to_list(old_json["branches"], "name")
67+
new_json = util.order_keys(new_json, "key", "name", "visibility", "projectSelection", "applications", "portfolios", "permissions")
3768
return new_json
3869

3970

0 commit comments

Comments
 (0)