Skip to content

Commit 9b6112f

Browse files
authored
Merge pull request #1338 from okorach/release-3.4.x
Merge Release 3.4.x in master
2 parents c4a01ea + 48444ed commit 9b6112f

File tree

2 files changed

+53
-22
lines changed

2 files changed

+53
-22
lines changed

sonar/users.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ def audit(self, settings: types.ConfigSettings = None) -> list[Problem]:
373373
problems.append(Problem(get_rule(RuleId.USER_UNUSED), self, str(self), age))
374374
return problems
375375

376-
def to_json(self, full: bool = False) -> types.ObjectJsonRepr:
376+
def to_json(self, export_settings: types.ConfigSettings) -> types.ObjectJsonRepr:
377377
"""Exports the user data (login, email, groups, SCM accounts local or not) as dict
378378
379379
:return: User data
@@ -382,13 +382,15 @@ def to_json(self, full: bool = False) -> types.ObjectJsonRepr:
382382
json_data = self._json.copy()
383383
scm = self.scm_accounts
384384
json_data["scmAccounts"] = util.list_to_csv(scm) if scm else None
385-
my_groups = self.groups().copy()
386-
if "sonar-users" in my_groups:
387-
my_groups.remove("sonar-users")
388-
json_data["groups"] = util.list_to_csv(my_groups, ", ", True)
389-
if not self.endpoint.is_sonarcloud() and not full and not json_data["local"]:
385+
json_data["groups"] = self.groups().copy()
386+
if export_settings.get("MODE", "") == "MIGRATION":
387+
return json_data
388+
if "sonar-users" in json_data["groups"]:
389+
json_data["groups"].remove("sonar-users")
390+
391+
if not self.endpoint.is_sonarcloud() and not export_settings["FULL_EXPORT"] and not json_data["local"]:
390392
json_data.pop("local")
391-
return util.remove_nones(util.filter_export(json_data, SETTABLE_PROPERTIES, full))
393+
return util.remove_nones(util.filter_export(json_data, SETTABLE_PROPERTIES, export_settings["FULL_EXPORT"]))
392394

393395

394396
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
415417
log.info("Exporting users")
416418
u_list = {}
417419
for u_login, u_obj in sorted(search(endpoint=endpoint).items()):
418-
u_list[u_login] = u_obj.to_json(export_settings["FULL_EXPORT"])
420+
u_list[u_login] = u_obj.to_json(export_settings)
419421
u_list[u_login].pop("login", None)
420422
return u_list
421423

test/test_migration.py

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,6 @@
3636
OPTS = [CMD] + util.STD_OPTS + [f"-{opt.REPORT_FILE_SHORT}", util.JSON_FILE]
3737

3838

39-
def __test_config_cmd(arguments: list[str]) -> None:
40-
"""Runs a test command"""
41-
outputfile = arguments[arguments.index(f"-{opt.REPORT_FILE_SHORT}") + 1]
42-
util.clean(outputfile)
43-
with pytest.raises(SystemExit) as e:
44-
with patch.object(sys, "argv", arguments):
45-
migration.main()
46-
assert int(str(e.value)) == errcodes.OK
47-
assert util.file_not_empty(outputfile)
48-
util.clean(outputfile)
49-
50-
5139
def test_migration_help() -> None:
5240
"""test_migration_help"""
5341
util.clean(util.JSON_FILE)
@@ -58,6 +46,47 @@ def test_migration_help() -> None:
5846
assert not os.path.isfile(util.JSON_FILE)
5947

6048

61-
def test_migration_basic() -> None:
49+
def test_migration() -> None:
6250
"""test_config_export"""
63-
__test_config_cmd(OPTS)
51+
util.clean(util.JSON_FILE)
52+
with pytest.raises(SystemExit) as e:
53+
with patch.object(sys, "argv", OPTS):
54+
migration.main()
55+
assert int(str(e.value)) == errcodes.OK
56+
assert util.file_not_empty(util.JSON_FILE)
57+
with open(file=util.JSON_FILE, mode="r", encoding="utf-8") as fh:
58+
json_config = json.loads(fh.read())
59+
60+
u = json_config["users"]["admin"]
61+
assert "sonar-users" in u["groups"]
62+
assert u["local"] and u["active"]
63+
assert "sonarQubeLastConnectionDate" in u
64+
assert "sonarLintLastConnectionDate" in u
65+
assert json_config["users"]["olivier"]["externalProvider"] == "sonarqube"
66+
67+
u = json_config["users"]["olivier-korach65532"]
68+
assert u["externalProvider"] == "github"
69+
assert u["name"] == "Olivier Korach"
70+
assert not u["local"]
71+
assert u["externalLogin"] == "okorach"
72+
assert u["email"] == "[email protected]"
73+
74+
p = json_config["projects"]["okorach_sonar-tools"]
75+
assert "lastTaskScannerContext" in p["backgroundTasks"]
76+
for elem in "detectedCi", "lastAnalysis", "revision":
77+
assert elem in p
78+
assert p["ncloc"]["py"] > 0
79+
assert p["ncloc"]["total"] > 0
80+
81+
iss = p["branches"]["master"]["issues"]
82+
assert iss["accepted"] > 0
83+
assert iss["fslePositives"] > 0
84+
assert iss["thirdParty"] == 0
85+
86+
p = json_config["projects"]["checkstyle-issues"]
87+
assert len(p["branches"]["issues"]["thirdParty"]) > 0
88+
89+
assert json_config["projects"]["demo:gitlab-ci-maven"]["detectedCi"] == "GitLab CI"
90+
assert json_config["projects"]["demo:gitlab-actions-cli"]["detectedCi"] == "Github Actions"
91+
92+
util.clean(util.JSON_FILE)

0 commit comments

Comments
 (0)