Skip to content

Commit 8aeaf9a

Browse files
SCANPY-141 Add the mandatory sonar.projectKey key (#143)
1 parent d51c4a9 commit 8aeaf9a

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/pysonar_scanner/configuration.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class Sonar:
8282
user_home: Optional[str] = None
8383

8484
token: str = ""
85+
project_key: str = ""
8586
host_url: str = ""
8687
region: str = ""
8788

@@ -98,6 +99,7 @@ def __to_dict(self) -> Dict:
9899
{"key": "sonar.scanner.app", "value": scanner.app},
99100
{"key": "sonar.scanner.appVersion", "value": scanner.app_version},
100101
{"key": "sonar.token", "value": sonar.token},
102+
{"key": "sonar.projectKey", "value": sonar.project_key},
101103
]
102104

103105
optional_properties = [
@@ -171,6 +173,7 @@ def initialize_configuration(cls) -> Configuration:
171173
project_base_dir=args.sonar_project_base_dir,
172174
user_home=args.sonar_user_home,
173175
token=args.token,
176+
project_key=args.sonar_project_key,
174177
host_url=args.sonar_host_url,
175178
region=args.sonar_region,
176179
)
@@ -189,6 +192,13 @@ def __parse_cli_args(cls) -> argparse.Namespace:
189192
required=True,
190193
help="Token used to authenticate against the SonarQube Server or SonarQube cloud",
191194
)
195+
parser.add_argument(
196+
"--sonar-project-key",
197+
type=str,
198+
required=True,
199+
help="Key of the project that usually corresponds to the project name in SonarQube",
200+
)
201+
192202
parser.add_argument(
193203
"-v", "--verbose", "--sonar-verbose", action="store_true", default=False, help="Increase output verbosity"
194204
)

tests/test_configuration.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,35 @@ def test_missing_cli_args(self):
3737
error_output = mock_stderr.getvalue()
3838
self.assertIn("the following arguments are required: -t/--token", error_output)
3939

40-
@patch("sys.argv", ["myscript.py", "--token", "myToken"])
40+
@patch("sys.argv", ["myscript.py", "--token", "myToken", "--sonar-project-key", "myProjectKey"])
4141
def test_minimal_cli_args(self):
4242
configuration = ConfigurationLoader.initialize_configuration()
4343
expected_internal = Internal()
4444
expected_scanner = Scanner(internal=expected_internal)
45-
expected_sonar = Sonar(scanner=expected_scanner, token="myToken")
45+
expected_sonar = Sonar(scanner=expected_scanner, token="myToken", project_key="myProjectKey")
4646
expected_configuration = Configuration(sonar=expected_sonar)
4747
self.assertEqual(configuration, expected_configuration)
4848

49-
@patch("sys.argv", ["myscript.py", "-t", "myToken", "-v"])
5049
def test_alternative_cli_args(self):
51-
alternatives = [["-t", "myToken", "-v"], ["--sonar-token", "myToken", "--sonar-verbose"]]
50+
alternatives = [
51+
["-t", "myToken", "-v", "--sonar-project-key", "myProjectKey"],
52+
["--sonar-token", "myToken", "--sonar-verbose", "--sonar-project-key", "myProjectKey"],
53+
]
5254
for alternative in alternatives:
5355
with patch("sys.argv", ["myscript.py", *alternative]), patch("sys.stderr", new=StringIO()):
5456
configuration = ConfigurationLoader.initialize_configuration()
5557
expected_internal = Internal()
5658
expected_scanner = Scanner(internal=expected_internal)
57-
expected_sonar = Sonar(scanner=expected_scanner, token="myToken", verbose=True)
59+
expected_sonar = Sonar(
60+
scanner=expected_scanner, token="myToken", project_key="myProjectKey", verbose=True
61+
)
5862
expected_configuration = Configuration(sonar=expected_sonar)
5963
self.assertEqual(configuration, expected_configuration)
6064

61-
@patch("sys.argv", ["myscript.py", "-t", "myToken", "--sonar-scanner-os", "windows2"])
65+
@patch(
66+
"sys.argv",
67+
["myscript.py", "-t", "myToken", "--sonar-project-key", "myProjectKey", "--sonar-scanner-os", "windows2"],
68+
)
6269
def test_impossible_os_choice(self):
6370
with patch("sys.stderr", new=StringIO()) as mock_stderr:
6471
with self.assertRaises(SystemExit):
@@ -73,6 +80,8 @@ def test_impossible_os_choice(self):
7380
"myscript.py",
7481
"-t",
7582
"myToken",
83+
"--sonar-project-key",
84+
"myProjectKey",
7685
"-v",
7786
"--sonar-host-url",
7887
"mySonarHostUrl",
@@ -154,6 +163,7 @@ def test_all_cli_args(self):
154163
expected_sonar = Sonar(
155164
scanner=expected_scanner,
156165
token="myToken",
166+
project_key="myProjectKey",
157167
verbose=True,
158168
host_url="mySonarHostUrl",
159169
region="us",
@@ -165,7 +175,7 @@ def test_all_cli_args(self):
165175
self.assertEqual(configuration, expected_configuration)
166176

167177
def test_minimal_json(self):
168-
minimal_json = Configuration(sonar=Sonar(token="MyToken")).to_json()
178+
minimal_json = Configuration(sonar=Sonar(token="myToken", project_key="myProjectKey")).to_json()
169179

170180
minimal_dict = json.loads(minimal_json)
171181
self.assertIn("scannerProperties", minimal_dict)
@@ -184,7 +194,8 @@ def test_minimal_json(self):
184194
"scannerProperties": [
185195
{"key": "sonar.scanner.app", "value": "python"},
186196
{"key": "sonar.scanner.appVersion", "value": "1.0"},
187-
{"key": "sonar.token", "value": "MyToken"},
197+
{"key": "sonar.token", "value": "myToken"},
198+
{"key": "sonar.projectKey", "value": "myProjectKey"},
188199
{"key": "sonar.verbose", "value": False},
189200
{"key": "sonar.scanner.bootstrapStartTime", "value": bootstrap_start_time},
190201
]
@@ -224,7 +235,8 @@ def test_full_json(self):
224235

225236
sonar = Sonar(
226237
scanner=scanner,
227-
token="MyToken",
238+
token="myToken",
239+
project_key="myProjectKey",
228240
verbose=True,
229241
host_url="mySonarHostUrl",
230242
region="us",
@@ -252,7 +264,8 @@ def test_full_json(self):
252264
"scannerProperties": [
253265
{"key": "sonar.scanner.app", "value": "python"},
254266
{"key": "sonar.scanner.appVersion", "value": "1.0"},
255-
{"key": "sonar.token", "value": "MyToken"},
267+
{"key": "sonar.token", "value": "myToken"},
268+
{"key": "sonar.projectKey", "value": "myProjectKey"},
256269
{"key": "sonar.region", "value": "us"},
257270
{"key": "sonar.host.url", "value": "mySonarHostUrl"},
258271
{"key": "sonar.projectBaseDir", "value": "mySonarProjectBaseDir"},

0 commit comments

Comments
 (0)