diff --git a/sonar/users.py b/sonar/users.py index fd115ecaf..339e69316 100644 --- a/sonar/users.py +++ b/sonar/users.py @@ -373,7 +373,7 @@ def audit(self, settings: types.ConfigSettings = None) -> list[Problem]: problems.append(Problem(get_rule(RuleId.USER_UNUSED), self, str(self), age)) return problems - def to_json(self, full: bool = False) -> types.ObjectJsonRepr: + def to_json(self, export_settings: types.ConfigSettings) -> types.ObjectJsonRepr: """Exports the user data (login, email, groups, SCM accounts local or not) as dict :return: User data @@ -382,13 +382,15 @@ def to_json(self, full: bool = False) -> types.ObjectJsonRepr: json_data = self._json.copy() scm = self.scm_accounts json_data["scmAccounts"] = util.list_to_csv(scm) if scm else None - my_groups = self.groups().copy() - if "sonar-users" in my_groups: - my_groups.remove("sonar-users") - json_data["groups"] = util.list_to_csv(my_groups, ", ", True) - if not self.endpoint.is_sonarcloud() and not full and not json_data["local"]: + json_data["groups"] = self.groups().copy() + if export_settings.get("MODE", "") == "MIGRATION": + return json_data + if "sonar-users" in json_data["groups"]: + json_data["groups"].remove("sonar-users") + + if not self.endpoint.is_sonarcloud() and not export_settings["FULL_EXPORT"] and not json_data["local"]: json_data.pop("local") - return util.remove_nones(util.filter_export(json_data, SETTABLE_PROPERTIES, full)) + return util.remove_nones(util.filter_export(json_data, SETTABLE_PROPERTIES, export_settings["FULL_EXPORT"])) def search(endpoint: pf.Platform, params: types.ApiParams = None) -> dict[str, User]: @@ -415,7 +417,7 @@ def export(endpoint: pf.Platform, export_settings: types.ConfigSettings, key_lis log.info("Exporting users") u_list = {} for u_login, u_obj in sorted(search(endpoint=endpoint).items()): - u_list[u_login] = u_obj.to_json(export_settings["FULL_EXPORT"]) + u_list[u_login] = u_obj.to_json(export_settings) u_list[u_login].pop("login", None) return u_list diff --git a/test/test_migration.py b/test/test_migration.py index c825f5ca2..05d4b01c8 100644 --- a/test/test_migration.py +++ b/test/test_migration.py @@ -36,18 +36,6 @@ OPTS = [CMD] + util.STD_OPTS + [f"-{opt.REPORT_FILE_SHORT}", util.JSON_FILE] -def __test_config_cmd(arguments: list[str]) -> None: - """Runs a test command""" - outputfile = arguments[arguments.index(f"-{opt.REPORT_FILE_SHORT}") + 1] - util.clean(outputfile) - with pytest.raises(SystemExit) as e: - with patch.object(sys, "argv", arguments): - migration.main() - assert int(str(e.value)) == errcodes.OK - assert util.file_not_empty(outputfile) - util.clean(outputfile) - - def test_migration_help() -> None: """test_migration_help""" util.clean(util.JSON_FILE) @@ -58,6 +46,47 @@ def test_migration_help() -> None: assert not os.path.isfile(util.JSON_FILE) -def test_migration_basic() -> None: +def test_migration() -> None: """test_config_export""" - __test_config_cmd(OPTS) + util.clean(util.JSON_FILE) + with pytest.raises(SystemExit) as e: + with patch.object(sys, "argv", OPTS): + migration.main() + assert int(str(e.value)) == errcodes.OK + assert util.file_not_empty(util.JSON_FILE) + with open(file=util.JSON_FILE, mode="r", encoding="utf-8") as fh: + json_config = json.loads(fh.read()) + + u = json_config["users"]["admin"] + assert "sonar-users" in u["groups"] + assert u["local"] and u["active"] + assert "sonarQubeLastConnectionDate" in u + assert "sonarLintLastConnectionDate" in u + assert json_config["users"]["olivier"]["externalProvider"] == "sonarqube" + + u = json_config["users"]["olivier-korach65532"] + assert u["externalProvider"] == "github" + assert u["name"] == "Olivier Korach" + assert not u["local"] + assert u["externalLogin"] == "okorach" + assert u["email"] == "olivier.korach@gmail.com" + + p = json_config["projects"]["okorach_sonar-tools"] + assert "lastTaskScannerContext" in p["backgroundTasks"] + for elem in "detectedCi", "lastAnalysis", "revision": + assert elem in p + assert p["ncloc"]["py"] > 0 + assert p["ncloc"]["total"] > 0 + + iss = p["branches"]["master"]["issues"] + assert iss["accepted"] > 0 + assert iss["fslePositives"] > 0 + assert iss["thirdParty"] == 0 + + p = json_config["projects"]["checkstyle-issues"] + assert len(p["branches"]["issues"]["thirdParty"]) > 0 + + assert json_config["projects"]["demo:gitlab-ci-maven"]["detectedCi"] == "GitLab CI" + assert json_config["projects"]["demo:gitlab-actions-cli"]["detectedCi"] == "Github Actions" + + util.clean(util.JSON_FILE)