Skip to content

Commit 096e925

Browse files
authored
SCANPY-150 Introduce constants for property names (#152)
1 parent e8d52dd commit 096e925

File tree

8 files changed

+256
-152
lines changed

8 files changed

+256
-152
lines changed

src/pysonar_scanner/api.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
import requests
2525
import requests.auth
2626

27+
from pysonar_scanner.configuration.properties import (
28+
SONAR_HOST_URL,
29+
SONAR_SCANNER_SONARCLOUD_URL,
30+
SONAR_SCANNER_API_BASE_URL,
31+
SONAR_REGION,
32+
Key,
33+
)
2734
from pysonar_scanner.exceptions import MissingKeyException, SonarQubeApiException
2835
from pysonar_scanner.utils import Arch, Os, remove_trailing_slash
2936

@@ -104,22 +111,22 @@ def from_dict(dict: dict) -> "JRE":
104111

105112
ApiConfiguration = TypedDict(
106113
"ApiConfiguration",
107-
{"sonar.host.url": str, "sonar.scanner.sonarcloudUrl": str, "sonar.scanner.apiBaseUrl": str, "sonar.region": str},
114+
{SONAR_HOST_URL: str, SONAR_SCANNER_SONARCLOUD_URL: str, SONAR_SCANNER_API_BASE_URL: str, SONAR_REGION: str},
108115
)
109116

110117

111-
def to_api_configuration(config_dict: dict[str, any]) -> ApiConfiguration:
118+
def to_api_configuration(config_dict: dict[Key, any]) -> ApiConfiguration:
112119
return {
113-
"sonar.host.url": config_dict.get("sonar.host.url", ""),
114-
"sonar.scanner.sonarcloudUrl": config_dict.get("sonar.scanner.sonarcloudUrl", ""),
115-
"sonar.scanner.apiBaseUrl": config_dict.get("sonar.scanner.apiBaseUrl", ""),
116-
"sonar.region": config_dict.get("sonar.region", ""),
120+
SONAR_HOST_URL: config_dict.get(SONAR_HOST_URL, ""),
121+
SONAR_SCANNER_SONARCLOUD_URL: config_dict.get(SONAR_SCANNER_SONARCLOUD_URL, ""),
122+
SONAR_SCANNER_API_BASE_URL: config_dict.get(SONAR_SCANNER_API_BASE_URL, ""),
123+
SONAR_REGION: config_dict.get(SONAR_REGION, ""),
117124
}
118125

119126

120-
def get_base_urls(config_dict: dict[str, any]) -> BaseUrls:
127+
def get_base_urls(config_dict: dict[Key, any]) -> BaseUrls:
121128
def is_sq_cloud_url(api_config: ApiConfiguration, sonar_host_url: str) -> bool:
122-
sq_cloud_url = api_config["sonar.scanner.sonarcloudUrl"] or "https://sonarcloud.io"
129+
sq_cloud_url = api_config[SONAR_SCANNER_SONARCLOUD_URL] or "https://sonarcloud.io"
123130
return remove_trailing_slash(sonar_host_url) == remove_trailing_slash(sq_cloud_url)
124131

125132
def is_blank(str) -> bool:
@@ -130,14 +137,14 @@ def region_with_dot(region: str) -> str:
130137

131138
api_config: ApiConfiguration = to_api_configuration(config_dict)
132139

133-
sonar_host_url = remove_trailing_slash(api_config["sonar.host.url"])
140+
sonar_host_url = remove_trailing_slash(api_config[SONAR_HOST_URL])
134141
if is_blank(sonar_host_url) or is_sq_cloud_url(api_config, sonar_host_url):
135-
region = region_with_dot(api_config["sonar.region"])
136-
sonar_host_url = api_config["sonar.scanner.sonarcloudUrl"] or f"https://{region}sonarcloud.io"
137-
api_base_url = api_config["sonar.scanner.apiBaseUrl"] or f"https://api.{region}sonarcloud.io"
142+
region = region_with_dot(api_config[SONAR_REGION])
143+
sonar_host_url = api_config[SONAR_SCANNER_SONARCLOUD_URL] or f"https://{region}sonarcloud.io"
144+
api_base_url = api_config[SONAR_SCANNER_API_BASE_URL] or f"https://api.{region}sonarcloud.io"
138145
return BaseUrls(base_url=sonar_host_url, api_base_url=api_base_url, is_sonar_qube_cloud=True)
139146
else:
140-
api_base_url = api_config["sonar.scanner.apiBaseUrl"] or f"{sonar_host_url}/api/v2"
147+
api_base_url = api_config[SONAR_SCANNER_API_BASE_URL] or f"{sonar_host_url}/api/v2"
141148
return BaseUrls(base_url=sonar_host_url, api_base_url=api_base_url, is_sonar_qube_cloud=False)
142149

143150

src/pysonar_scanner/configuration/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@
2020

2121
from pysonar_scanner.configuration import properties
2222
from pysonar_scanner.configuration.cli import CliConfigurationLoader
23+
from pysonar_scanner.configuration.properties import SONAR_TOKEN, Key
2324

2425
from pysonar_scanner.exceptions import MissingKeyException
2526

2627

27-
def get_static_default_properties() -> dict[str, any]:
28+
def get_static_default_properties() -> dict[Key, any]:
2829
return {prop.name: prop.default_value for prop in properties.PROPERTIES if prop.default_value is not None}
2930

3031

3132
class ConfigurationLoader:
3233
@staticmethod
33-
def load() -> dict[str, any]:
34+
def load() -> dict[Key, any]:
3435
# each property loader is required to return NO default values.
3536
# E.g. if no property has been set, an empty dict must be returned.
3637
# Default values should be set through the get_static_default_properties() method
@@ -41,7 +42,7 @@ def load() -> dict[str, any]:
4142
}
4243

4344

44-
def get_token(config: dict[str, any]) -> str:
45-
if "sonar.token" not in config:
46-
raise MissingKeyException('Missing property "sonar.token"')
47-
return config["sonar.token"]
45+
def get_token(config: dict[Key, any]) -> str:
46+
if SONAR_TOKEN not in config:
47+
raise MissingKeyException(f'Missing property "{SONAR_TOKEN}"')
48+
return config[SONAR_TOKEN]

src/pysonar_scanner/configuration/properties.py

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,45 @@
2222
from dataclasses import dataclass
2323
from typing import Callable, Optional
2424

25+
Key = str
26+
27+
SONAR_HOST_URL: Key = "sonar.host.url"
28+
SONAR_SCANNER_SONARCLOUD_URL: Key = "sonar.scanner.sonarcloudUrl"
29+
SONAR_SCANNER_API_BASE_URL: Key = "sonar.scanner.apiBaseUrl"
30+
SONAR_REGION: Key = "sonar.region"
31+
SONAR_SCANNER_APP: Key = "sonar.scanner.app"
32+
SONAR_SCANNER_APP_VERSION: Key = "sonar.scanner.appVersion"
33+
SONAR_SCANNER_BOOTSTRAP_START_TIME: Key = "sonar.scanner.bootstrapStartTime"
34+
SONAR_SCANNER_WAS_JRE_CACHE_HIT: Key = "sonar.scanner.wasJreCacheHit"
35+
SONAR_SCANNER_WAS_ENGINE_CACHE_HIT: Key = "sonar.scanner.wasEngineCacheHit"
36+
SONAR_VERBOSE: Key = "sonar.verbose"
37+
SONAR_TOKEN: Key = "sonar.token"
38+
SONAR_SCANNER_OS: Key = "sonar.scanner.os"
39+
SONAR_SCANNER_ARCH: Key = "sonar.scanner.arch"
40+
SONAR_SCANNER_SKIP_JRE_PROVISIONING: Key = "sonar.scanner.skipJreProvisioning"
41+
SONAR_USER_HOME: Key = "sonar.userHome"
42+
SONAR_SCANNER_JAVA_EXE_PATH: Key = "sonar.scanner.javaExePath"
43+
SONAR_SCANNER_INTERNAL_DUMP_TO_FILE: Key = "sonar.scanner.internal.dumpToFile"
44+
SONAR_SCANNER_INTERNAL_SQ_VERSION: Key = "sonar.scanner.internal.sqVersion"
45+
SONAR_SCANNER_CONNECT_TIMEOUT: Key = "sonar.scanner.connectTimeout"
46+
SONAR_SCANNER_SOCKET_TIMEOUT: Key = "sonar.scanner.socketTimeout"
47+
SONAR_SCANNER_RESPONSE_TIMEOUT: Key = "sonar.scanner.responseTimeout"
48+
SONAR_SCANNER_TRUSTSTORE_PATH: Key = "sonar.scanner.truststorePath"
49+
SONAR_SCANNER_TRUSTSTORE_PASSWORD: Key = "sonar.scanner.truststorePassword"
50+
SONAR_SCANNER_KEYSTORE_PATH: Key = "sonar.scanner.keystorePath"
51+
SONAR_SCANNER_KEYSTORE_PASSWORD: Key = "sonar.scanner.keystorePassword"
52+
SONAR_SCANNER_PROXY_HOST: Key = "sonar.scanner.proxyHost"
53+
SONAR_SCANNER_PROXY_PORT: Key = "sonar.scanner.proxyPort"
54+
SONAR_SCANNER_PROXY_USER: Key = "sonar.scanner.proxyUser"
55+
SONAR_SCANNER_PROXY_PASSWORD: Key = "sonar.scanner.proxyPassword"
56+
SONAR_PROJECT_BASE_DIR: Key = "sonar.projectBaseDir"
57+
SONAR_SCANNER_JAVA_OPTS: Key = "sonar.scanner.javaOpts"
58+
SONAR_PROJECT_KEY: Key = "sonar.projectKey"
59+
2560

2661
@dataclass
2762
class Property:
28-
name: str
63+
name: Key
2964
"""name in the format of `sonar.scanner.appVersion`"""
3065

3166
default_value: Optional[any]
@@ -38,162 +73,162 @@ class Property:
3873
# fmt: off
3974
PROPERTIES: list[Property] = [
4075
Property(
41-
name="sonar.scanner.app",
76+
name=SONAR_SCANNER_APP,
4277
default_value="python",
4378
cli_getter=None
4479
),
4580
Property(
46-
name="sonar.scanner.appVersion",
81+
name=SONAR_SCANNER_APP_VERSION,
4782
default_value="1.0",
4883
cli_getter=None
4984
),
5085
Property(
51-
name="sonar.scanner.bootstrapStartTime",
86+
name=SONAR_SCANNER_BOOTSTRAP_START_TIME,
5287
default_value=int(time.time() * 1000),
5388
cli_getter=None
5489
),
5590
Property(
56-
name="sonar.scanner.wasJreCacheHit",
91+
name=SONAR_SCANNER_WAS_JRE_CACHE_HIT,
5792
default_value=None,
5893
cli_getter=None
5994
),
6095
Property(
61-
name="sonar.scanner.wasEngineCacheHit",
96+
name=SONAR_SCANNER_WAS_ENGINE_CACHE_HIT,
6297
default_value=None,
6398
cli_getter=None
6499
),
65100
Property(
66-
name="sonar.verbose",
101+
name=SONAR_VERBOSE,
67102
default_value=False,
68103
cli_getter=lambda args: args.verbose
69104
),
70105
Property(
71-
name="sonar.host.url",
106+
name=SONAR_HOST_URL,
72107
default_value=None,
73108
cli_getter=lambda args: args.sonar_host_url
74109
),
75110
Property(
76-
name="sonar.region",
111+
name=SONAR_REGION,
77112
default_value=None,
78113
cli_getter=lambda args: args.sonar_region
79114
),
80115
Property(
81-
name="sonar.scanner.sonarcloudUrl",
116+
name=SONAR_SCANNER_SONARCLOUD_URL,
82117
default_value=None,
83118
cli_getter=lambda args: args.sonar_scanner_cloud_url
84119
),
85120
Property(
86-
name="sonar.scanner.apiBaseUrl",
121+
name=SONAR_SCANNER_API_BASE_URL,
87122
default_value=None,
88123
cli_getter=lambda args: args.sonar_scanner_api_url
89124
),
90125
Property(
91-
name="sonar.token",
126+
name=SONAR_TOKEN,
92127
default_value=None,
93128
cli_getter=lambda args: args.token
94129
),
95130
Property(
96-
name="sonar.scanner.os",
131+
name=SONAR_SCANNER_OS,
97132
default_value=None,
98133
cli_getter=lambda args: args.sonar_scanner_os
99134
),
100135
Property(
101-
name="sonar.scanner.arch",
136+
name=SONAR_SCANNER_ARCH,
102137
default_value=None,
103138
cli_getter=lambda args: args.sonar_scanner_arch
104139
),
105140
Property(
106-
name="sonar.scanner.skipJreProvisioning",
141+
name=SONAR_SCANNER_SKIP_JRE_PROVISIONING,
107142
default_value=False,
108143
cli_getter=lambda args: args.skip_jre_provisioning
109144
),
110145
Property(
111-
name="sonar.userHome",
146+
name=SONAR_USER_HOME,
112147
default_value="~/.sonar",
113148
cli_getter=lambda args: args.sonar_user_home
114149
),
115150
Property(
116-
name="sonar.scanner.javaExePath",
151+
name=SONAR_SCANNER_JAVA_EXE_PATH,
117152
default_value=None,
118153
cli_getter=lambda args: args.sonar_scanner_java_exe_path
119154
),
120155
Property(
121-
name="sonar.scanner.internal.dumpToFile",
156+
name=SONAR_SCANNER_INTERNAL_DUMP_TO_FILE,
122157
default_value=None,
123158
cli_getter=lambda args: args.sonar_scanner_internal_dump_to_file
124159
),
125160
Property(
126-
name="sonar.scanner.internal.sqVersion",
161+
name=SONAR_SCANNER_INTERNAL_SQ_VERSION,
127162
default_value=None,
128163
cli_getter=lambda args: args.sonar_scanner_internal_sq_version
129164
),
130165
Property(
131-
name="sonar.scanner.connectTimeout",
166+
name=SONAR_SCANNER_CONNECT_TIMEOUT,
132167
default_value=5,
133168
cli_getter=lambda args: args.sonar_scanner_connect_timeout
134169
),
135170
Property(
136-
name="sonar.scanner.socketTimeout",
171+
name=SONAR_SCANNER_SOCKET_TIMEOUT,
137172
default_value=60,
138173
cli_getter=lambda args: args.sonar_scanner_socket_timeout
139174
),
140175
Property(
141-
name="sonar.scanner.responseTimeout",
176+
name=SONAR_SCANNER_RESPONSE_TIMEOUT,
142177
default_value=0,
143178
cli_getter=lambda args: args.sonar_scanner_response_timeout
144179
),
145180
Property(
146-
name="sonar.scanner.truststorePath",
181+
name=SONAR_SCANNER_TRUSTSTORE_PATH,
147182
default_value=None,
148183
cli_getter=lambda args: args.sonar_scanner_truststore_path
149184
),
150185
Property(
151-
name="sonar.scanner.truststorePassword",
186+
name=SONAR_SCANNER_TRUSTSTORE_PASSWORD,
152187
default_value="changeit",
153188
cli_getter=lambda args: args.sonar_scanner_truststore_password
154189
),
155190
Property(
156-
name="sonar.scanner.keystorePath",
191+
name=SONAR_SCANNER_KEYSTORE_PATH,
157192
default_value=None,
158193
cli_getter=lambda args: args.sonar_scanner_keystore_path
159194
),
160195
Property(
161-
name="sonar.scanner.keystorePassword",
196+
name=SONAR_SCANNER_KEYSTORE_PASSWORD,
162197
default_value="changeit",
163198
cli_getter=lambda args: args.sonar_scanner_keystore_password
164199
),
165200
Property(
166-
name="sonar.scanner.proxyHost",
201+
name=SONAR_SCANNER_PROXY_HOST,
167202
default_value=None,
168203
cli_getter=lambda args: args.sonar_scanner_proxy_host
169204
),
170205
Property(
171-
name="sonar.scanner.proxyPort",
206+
name=SONAR_SCANNER_PROXY_PORT,
172207
default_value=None,
173208
cli_getter=lambda args: args.sonar_scanner_proxy_port
174209
),
175210
Property(
176-
name="sonar.scanner.proxyUser",
211+
name=SONAR_SCANNER_PROXY_USER,
177212
default_value=None,
178213
cli_getter=lambda args: args.sonar_scanner_proxy_user
179214
),
180215
Property(
181-
name="sonar.scanner.proxyPassword",
216+
name=SONAR_SCANNER_PROXY_PASSWORD,
182217
default_value=None,
183218
cli_getter=lambda args: args.sonar_scanner_proxy_password
184219
),
185220
Property(
186-
name="sonar.projectBaseDir",
221+
name=SONAR_PROJECT_BASE_DIR,
187222
default_value=None,
188223
cli_getter=lambda args: args.sonar_project_base_dir
189224
),
190225
Property(
191-
name="sonar.scanner.javaOpts",
226+
name=SONAR_SCANNER_JAVA_OPTS,
192227
default_value=None,
193228
cli_getter=lambda args: args.sonar_scanner_java_opts
194229
),
195230
Property(
196-
name="sonar.projectKey",
231+
name=SONAR_PROJECT_KEY,
197232
default_value=None,
198233
cli_getter=lambda args: args.sonar_project_key
199234
),

src/pysonar_scanner/jre.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
UnsupportedArchiveFormat,
3434
)
3535
from pysonar_scanner.exceptions import JreProvisioningException
36+
from pysonar_scanner.configuration.properties import (
37+
SONAR_SCANNER_JAVA_EXE_PATH,
38+
SONAR_SCANNER_SKIP_JRE_PROVISIONING,
39+
SONAR_SCANNER_OS,
40+
Key,
41+
)
3642

3743

3844
@dataclass(frozen=True)
@@ -130,11 +136,11 @@ class JREResolverConfiguration:
130136
sonar_scanner_os: Optional[str]
131137

132138
@staticmethod
133-
def from_dict(config_dict: dict[str, any]) -> "JREResolverConfiguration":
139+
def from_dict(config_dict: dict[Key, any]) -> "JREResolverConfiguration":
134140
return JREResolverConfiguration(
135-
sonar_scanner_java_exe_path=config_dict.get("sonar.scanner.javaExePath", None),
136-
sonar_scanner_skip_jre_provisioning=config_dict.get("sonar.scanner.skipJreProvisioning", False),
137-
sonar_scanner_os=config_dict.get("sonar.scanner.os", None),
141+
sonar_scanner_java_exe_path=config_dict.get(SONAR_SCANNER_JAVA_EXE_PATH, None),
142+
sonar_scanner_skip_jre_provisioning=config_dict.get(SONAR_SCANNER_SKIP_JRE_PROVISIONING, False),
143+
sonar_scanner_os=config_dict.get(SONAR_SCANNER_OS, None),
138144
)
139145

140146

0 commit comments

Comments
 (0)