19
19
#
20
20
import typing
21
21
from dataclasses import dataclass
22
- from typing import Optional
22
+ from typing import Optional , TypedDict
23
23
24
24
import requests
25
25
import requests .auth
26
26
27
- from pysonar_scanner .configuration import Configuration
28
27
from pysonar_scanner .exceptions import MissingKeyException , SonarQubeApiException
29
28
from pysonar_scanner .utils import Arch , Os , remove_trailing_slash
30
29
@@ -103,9 +102,24 @@ def from_dict(dict: dict) -> "JRE":
103
102
raise MissingKeyException (f"Missing key in dictionary { dict } " ) from e
104
103
105
104
106
- def get_base_urls (config : Configuration ) -> BaseUrls :
107
- def is_sq_cloud_url (sonar_host_url : str ) -> bool :
108
- sq_cloud_url = config .sonar .scanner .sonarcloud_url .strip () or "https://sonarcloud.io"
105
+ ApiConfiguration = TypedDict (
106
+ "ApiConfiguration" ,
107
+ {"sonar.host.url" : str , "sonar.scanner.sonarcloudUrl" : str , "sonar.scanner.apiBaseUrl" : str , "sonar.region" : str },
108
+ )
109
+
110
+
111
+ def to_api_configuration (config_dict : dict [str , any ]) -> ApiConfiguration :
112
+ 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" , "" ),
117
+ }
118
+
119
+
120
+ def get_base_urls (config_dict : dict [str , any ]) -> BaseUrls :
121
+ 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"
109
123
return remove_trailing_slash (sonar_host_url ) == remove_trailing_slash (sq_cloud_url )
110
124
111
125
def is_blank (str ) -> bool :
@@ -114,14 +128,16 @@ def is_blank(str) -> bool:
114
128
def region_with_dot (region : str ) -> str :
115
129
return region + "." if not is_blank (region ) else ""
116
130
117
- sonar_host_url = remove_trailing_slash (config .sonar .host_url )
118
- if is_blank (sonar_host_url ) or is_sq_cloud_url (sonar_host_url ):
119
- region = region_with_dot (config .sonar .region )
120
- sonar_host_url = config .sonar .scanner .sonarcloud_url or f"https://{ region } sonarcloud.io"
121
- api_base_url = config .sonar .scanner .api_base_url or f"https://api.{ region } sonarcloud.io"
131
+ api_config : ApiConfiguration = to_api_configuration (config_dict )
132
+
133
+ sonar_host_url = remove_trailing_slash (api_config ["sonar.host.url" ])
134
+ 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"
122
138
return BaseUrls (base_url = sonar_host_url , api_base_url = api_base_url , is_sonar_qube_cloud = True )
123
139
else :
124
- api_base_url = config . sonar .scanner .api_base_url or f"{ sonar_host_url } /api/v2"
140
+ api_base_url = api_config [ " sonar.scanner.apiBaseUrl" ] or f"{ sonar_host_url } /api/v2"
125
141
return BaseUrls (base_url = sonar_host_url , api_base_url = api_base_url , is_sonar_qube_cloud = False )
126
142
127
143
0 commit comments