Skip to content

Commit 7490b8b

Browse files
authored
Merge pull request #1350 from okorach:fix-user-agent
sonar-migration own User Agent
2 parents 4f75f87 + 280c899 commit 7490b8b

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

cli/migration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import sys
2525

2626
from cli import options
27-
from sonar import exceptions, errcodes, utilities
27+
from sonar import exceptions, errcodes, utilities, version
2828
import sonar.logging as log
2929
from sonar import platform, rules, qualityprofiles, qualitygates, users, groups
3030
from sonar import projects, portfolios, applications
@@ -143,6 +143,7 @@ def main() -> None:
143143
kwargs = utilities.convert_args(__parse_args("Extract SonarQube platform configuration"))
144144
endpoint = platform.Platform(**kwargs)
145145
endpoint.verify_connection()
146+
endpoint.set_user_agent(f"sonar-migration {version.MIGRATION_TOOL_VERSION}")
146147
except (options.ArgumentsError, exceptions.ObjectNotFound) as e:
147148
utilities.exit_fatal(e.message, e.errcode)
148149

cli/options.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ def parse_and_check(parser: ArgumentParser, logger_name: str = None, verify_toke
204204
__check_file_writeable(kwargs.get(REPORT_FILE, None))
205205
# Verify version randomly once every 10 runs
206206
if not kwargs[SKIP_VERSION_CHECK] and random.randrange(10) == 0:
207-
utilities.check_last_sonar_tools_version()
207+
if is_migration:
208+
utilities.check_last_version("https://pypi.org/simple/sonar-migration")
209+
else:
210+
utilities.check_last_version("https://pypi.org/simple/sonar-tools")
208211
kwargs.pop(SKIP_VERSION_CHECK, None)
209212
if utilities.is_sonarcloud_url(kwargs[URL]) and kwargs[ORG] is None:
210213
raise ArgumentsError(f"Organization (-{ORG_SHORT}) option is mandatory for SonarCloud")

sonar/platform.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
_NON_EXISTING_SETTING_SKIPPED = "Setting %s does not exist, skipping..."
5454
_HTTP_ERROR = "%s Error: %s HTTP status code %d - %s"
5555

56-
_SONAR_TOOLS_AGENT = {"user-agent": f"sonar-tools {version.PACKAGE_VERSION}"}
56+
_SONAR_TOOLS_AGENT = f"sonar-tools {version.PACKAGE_VERSION}"
5757
_UPDATE_CENTER = "https://raw.githubusercontent.com/SonarSource/sonar-update-center-properties/master/update-center-source.properties"
5858

5959
LTA = None
@@ -88,6 +88,7 @@ def __init__(self, url: str, token: str, org: str = None, cert_file: Optional[st
8888
self.http_timeout = int(http_timeout)
8989
self.organization = org
9090
self.__is_sonarcloud = util.is_sonarcloud_url(self.url)
91+
self._user_agent = _SONAR_TOOLS_AGENT
9192

9293
def __str__(self) -> str:
9394
"""
@@ -136,6 +137,9 @@ def user_data(self) -> types.ApiPayload:
136137
self.__user_data = json.loads(self.get("api/users/current").text)
137138
return self.__user_data
138139

140+
def set_user_agent(self, user_agent: str) -> None:
141+
self._user_agent = user_agent
142+
139143
def server_id(self) -> str:
140144
"""
141145
Returns the SonarQube instance server id
@@ -210,7 +214,7 @@ def __run_request(
210214
) -> requests.Response:
211215
"""Makes an HTTP request to SonarQube"""
212216
api = _normalize_api(api)
213-
headers = _SONAR_TOOLS_AGENT
217+
headers = {"user-agent": self._user_agent}
214218
if params is None:
215219
params = {}
216220
if self.is_sonarcloud():

sonar/utilities.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,20 @@
4545
DEFAULT = "__default__"
4646

4747

48-
def check_last_sonar_tools_version() -> None:
48+
def check_last_version(package_url: str) -> None:
4949
"""Checks last version of sonar-tools on pypi and displays a warning if the currently used version is older"""
5050
log.info("Checking latest sonar-version on pypi.org")
5151
try:
52-
r = requests.get(url="https://pypi.org/simple/sonar-tools", headers={"Accept": "application/vnd.pypi.simple.v1+json"}, timeout=10)
52+
r = requests.get(url=package_url, headers={"Accept": "application/vnd.pypi.simple.v1+json"}, timeout=10)
5353
r.raise_for_status()
5454
except (requests.RequestException, requests.exceptions.HTTPError, requests.exceptions.Timeout) as e:
5555
log.info("Can't access pypi.org, error %s", str(e))
5656
return
5757
txt_version = json.loads(r.text)["versions"][-1]
58-
log.info("Latest sonar-tools version is %s", txt_version)
58+
package_name = package_url.split("/")[-1]
59+
log.info("Latest %s version is %s", package_name, txt_version)
5960
if tuple(".".split(txt_version)) > tuple(".".split(version.PACKAGE_VERSION)):
60-
log.warning("A more recent version of sonar-tools (%s) is available, your are advised to upgrade", txt_version)
61+
log.warning("A more recent version of %s (%s) is available, your are advised to upgrade", package_name, txt_version)
6162

6263

6364
def token_type(token: str) -> str:

0 commit comments

Comments
 (0)