-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapi.py
More file actions
300 lines (249 loc) · 10.5 KB
/
api.py
File metadata and controls
300 lines (249 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#
# Sonar Scanner Python
# Copyright (C) 2011-2026 SonarSource Sàrl
# mailto:info AT sonarsource DOT com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
#
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
import typing
from dataclasses import dataclass
from typing import Any, NoReturn, Optional
import requests
import requests.auth
from pysonar_scanner.configuration.properties import (
SONAR_HOST_URL,
SONAR_SCANNER_SONARCLOUD_URL,
SONAR_SCANNER_API_BASE_URL,
SONAR_REGION,
Key,
)
from pysonar_scanner.utils import remove_trailing_slash, OsStr, ArchStr
from pysonar_scanner.exceptions import (
SonarQubeApiException,
InconsistentConfiguration,
SonarQubeApiUnauthroizedException,
)
GLOBAL_SONARCLOUD_URL = "https://sonarcloud.io"
US_SONARCLOUD_URL = "https://sonarqube.us"
UNAUTHORIZED_STATUS_CODES = (401, 403)
ACCEPT_JSON = {"Accept": "application/json"}
ACCEPT_OCTET_STREAM = {"Accept": "application/octet-stream"}
@dataclass(frozen=True)
class SQVersion:
parts: list[str]
def __get_part(self, index: int) -> int:
if index >= len(self.parts):
return 0
part = self.parts[index]
if not part.isdigit():
return 0
return int(part)
def major(self) -> int:
return self.__get_part(0)
def minor(self) -> int:
return self.__get_part(1)
def does_support_bootstrapping(self) -> bool:
if len(self.parts) == 0:
return False
return self.major() > MIN_SUPPORTED_SQ_VERSION.major() or (
self.major() == MIN_SUPPORTED_SQ_VERSION.major() and self.minor() >= MIN_SUPPORTED_SQ_VERSION.minor()
)
def __str__(self) -> str:
return ".".join(self.parts)
@staticmethod
def from_str(version: str) -> "SQVersion":
return SQVersion(version.split("."))
MIN_SUPPORTED_SQ_VERSION: SQVersion = SQVersion.from_str("10.6")
@dataclass(frozen=True)
class BaseUrls:
base_url: str
api_base_url: str
is_sonar_qube_cloud: bool
def __post_init__(self):
object.__setattr__(self, "base_url", remove_trailing_slash(self.base_url))
object.__setattr__(self, "api_base_url", remove_trailing_slash(self.api_base_url))
@dataclass(frozen=True)
class JRE:
id: Optional[str]
filename: str
sha256: str
java_path: str
os: str
arch: str
download_url: Optional[str]
@staticmethod
def from_dict(dict: dict) -> "JRE":
return JRE(
id=dict.get("id", None),
filename=dict["filename"],
sha256=dict["sha256"],
java_path=dict["javaPath"],
os=dict["os"],
arch=dict["arch"],
download_url=dict.get("downloadUrl", None),
)
@dataclass(frozen=True)
class ApiConfiguration:
sonar_host_url: str
sonar_scanner_sonarcloud_url: str
sonar_scanner_api_base_url: str
sonar_region: str
def to_api_configuration(config_dict: dict[Key, Any]) -> ApiConfiguration:
return ApiConfiguration(
sonar_host_url=config_dict.get(SONAR_HOST_URL, ""),
sonar_scanner_sonarcloud_url=config_dict.get(SONAR_SCANNER_SONARCLOUD_URL, ""),
sonar_scanner_api_base_url=config_dict.get(SONAR_SCANNER_API_BASE_URL, ""),
sonar_region=config_dict.get(SONAR_REGION, ""),
)
def get_base_urls(config_dict: dict[Key, Any]) -> BaseUrls:
def is_sq_cloud_url(api_config: ApiConfiguration, sonar_host_url: str) -> bool:
sq_cloud_url = api_config.sonar_scanner_sonarcloud_url or GLOBAL_SONARCLOUD_URL
return remove_trailing_slash(sonar_host_url) in [remove_trailing_slash(sq_cloud_url), US_SONARCLOUD_URL]
def is_blank(str) -> bool:
return str.strip() == ""
api_config: ApiConfiguration = to_api_configuration(config_dict)
sonar_host_url = remove_trailing_slash(api_config.sonar_host_url)
region = api_config.sonar_region
if region and region != "us":
raise InconsistentConfiguration(
f"Invalid region '{region}'. Valid regions are: 'us'. "
"Please check the 'sonar.region' property or the 'SONAR_REGION' environment variable."
)
if is_inconsistent_configuration(region, sonar_host_url):
raise InconsistentConfiguration(
"Inconsistent values for properties 'sonar.region' and 'sonar.host.url'. "
+ "Please only specify one of the two properties."
)
if is_blank(sonar_host_url) or is_sq_cloud_url(api_config, sonar_host_url):
default_url = US_SONARCLOUD_URL if region == "us" else GLOBAL_SONARCLOUD_URL
default_api_base_url = "https://api.sonarqube.us" if region == "us" else "https://api.sonarcloud.io"
sonar_host_url = api_config.sonar_scanner_sonarcloud_url or default_url
api_base_url = api_config.sonar_scanner_api_base_url or default_api_base_url
return BaseUrls(base_url=sonar_host_url, api_base_url=api_base_url, is_sonar_qube_cloud=True)
else:
api_base_url = api_config.sonar_scanner_api_base_url or f"{sonar_host_url}/api/v2"
return BaseUrls(base_url=sonar_host_url, api_base_url=api_base_url, is_sonar_qube_cloud=False)
def is_inconsistent_configuration(region, sonar_host_url):
if not region or not sonar_host_url:
return False
if region == "us" and sonar_host_url.startswith(US_SONARCLOUD_URL):
return False
return True
class BearerAuth(requests.auth.AuthBase):
def __init__(self, token):
self.token = token
def __call__(self, r):
r.headers["Authorization"] = f"Bearer {self.token}"
return r
@dataclass(frozen=True)
class EngineInfo:
filename: str
sha256: str
download_url: Optional[str] = None
class SonarQubeApi:
def __init__(self, base_urls: BaseUrls, token: str):
self.base_urls = base_urls
self.auth = BearerAuth(token)
def __raise_exception(self, exception: Exception) -> NoReturn:
if (
isinstance(exception, requests.RequestException)
and exception.response is not None
and exception.response.status_code in UNAUTHORIZED_STATUS_CODES
):
raise SonarQubeApiUnauthroizedException.create_default(self.base_urls.base_url) from exception
else:
raise SonarQubeApiException("Error while fetching the analysis version") from exception
def is_sonar_qube_cloud(self) -> bool:
return self.base_urls.is_sonar_qube_cloud
def get_analysis_version(self) -> SQVersion:
try:
res = requests.get(f"{self.base_urls.api_base_url}/analysis/version", auth=self.auth)
if res.status_code != 200:
res = requests.get(f"{self.base_urls.base_url}/api/server/version", auth=self.auth)
res.raise_for_status()
return SQVersion.from_str(res.text)
except requests.RequestException as e:
self.__raise_exception(e)
def get_analysis_engine(self) -> EngineInfo:
try:
res = requests.get(f"{self.base_urls.api_base_url}/analysis/engine", headers=ACCEPT_JSON, auth=self.auth)
res.raise_for_status()
json = res.json()
if "filename" not in json or "sha256" not in json:
raise SonarQubeApiException("Invalid response from the server")
return EngineInfo(
filename=json["filename"], sha256=json["sha256"], download_url=json.get("downloadUrl", None)
)
except requests.RequestException as e:
self.__raise_exception(e)
def download_analysis_engine(self, handle: typing.BinaryIO) -> None:
"""
This method can raise a SonarQubeApiException if the server doesn't respond successfully.
Alternative, if the file IO fails, an IOError or OSError can be raised.
"""
try:
res = requests.get(
f"{self.base_urls.api_base_url}/analysis/engine",
headers=ACCEPT_OCTET_STREAM,
auth=self.auth,
)
self.__download_file(res, handle)
except requests.RequestException as e:
self.__raise_exception(e)
def get_analysis_jres(self, os: OsStr, arch: ArchStr) -> list[JRE]:
try:
params = {"os": os, "arch": arch}
res = requests.get(
f"{self.base_urls.api_base_url}/analysis/jres",
auth=self.auth,
headers=ACCEPT_JSON,
params=params,
)
res.raise_for_status()
json_array = res.json()
return [JRE.from_dict(jre) for jre in json_array]
except (requests.RequestException, KeyError) as e:
self.__raise_exception(e)
def download_analysis_jre(self, id: str, handle: typing.BinaryIO) -> None:
"""
This method can raise a SonarQubeApiException if the server doesn't respond successfully.
Alternative, if the file IO fails, an IOError or OSError can be raised.
"""
try:
res = requests.get(
f"{self.base_urls.api_base_url}/analysis/jres/{id}",
headers=ACCEPT_OCTET_STREAM,
auth=self.auth,
)
self.__download_file(res, handle)
except requests.RequestException as e:
self.__raise_exception(e)
def download_file_from_url(self, url: str, handle: typing.BinaryIO) -> None:
"""
This method can raise a SonarQubeApiException if the server doesn't respond successfully.
Alternative, if the file IO fails, an IOError or OSError can be raised.
"""
try:
res = requests.get(
url,
headers=ACCEPT_OCTET_STREAM,
)
self.__download_file(res, handle)
except requests.RequestException as e:
self.__raise_exception(e)
def __download_file(self, res: requests.Response, handle: typing.BinaryIO) -> None:
res.raise_for_status()
for chunk in res.iter_content(chunk_size=128):
handle.write(chunk)