Skip to content

Commit b6223a0

Browse files
authored
Own UA for each sonar-tool
1 parent f006c9a commit b6223a0

File tree

11 files changed

+57
-38
lines changed

11 files changed

+57
-38
lines changed

cli/audit.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
import json
2828
import csv
2929
from typing import TextIO
30-
from threading import Thread, Lock
30+
from threading import Thread
3131
from queue import Queue
3232

3333
from cli import options
3434

35-
from sonar import errcodes, exceptions
35+
from sonar import errcodes, exceptions, version
3636
from sonar.util import types
3737
import sonar.logging as log
3838
from sonar import platform, users, groups, qualityprofiles, qualitygates, sif, portfolios, applications, projects
@@ -50,7 +50,7 @@
5050
options.WHAT_PORTFOLIOS,
5151
]
5252

53-
_WRITE_LOCK = Lock()
53+
TOOL_NAME = "sonar-audit"
5454

5555

5656
def _audit_sif(sysinfo, audit_settings):
@@ -170,7 +170,7 @@ def __parser_args(desc):
170170
action="store_true",
171171
help="Creates the $HOME/.sonar-audit.properties configuration file, if not already present or outputs to stdout if it already exist",
172172
)
173-
args = options.parse_and_check(parser=parser, logger_name="sonar-audit", verify_token=False)
173+
args = options.parse_and_check(parser=parser, logger_name=TOOL_NAME, verify_token=False)
174174
if args.sif is None and args.config is None:
175175
util.check_token(args.token)
176176
return args
@@ -183,7 +183,7 @@ def main():
183183
except options.ArgumentsError as e:
184184
util.exit_fatal(e.message, e.errcode)
185185

186-
settings = config.load("sonar-audit")
186+
settings = config.load(TOOL_NAME)
187187
settings["threads"] = kwargs[options.NBR_THREADS]
188188
if kwargs.get("config", False):
189189
config.configure()
@@ -211,6 +211,7 @@ def main():
211211
try:
212212
sq = platform.Platform(**kwargs)
213213
sq.verify_connection()
214+
sq.set_user_agent(f"{TOOL_NAME} {version.PACKAGE_VERSION}")
214215
except exceptions.ConnectionError as e:
215216
util.exit_fatal(e.message, e.errcode)
216217
server_id = sq.server_id()

cli/config.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
"""
2424
import sys
2525
from typing import TextIO
26-
from threading import Thread, Lock
26+
from threading import Thread
2727
from queue import Queue
2828

2929
import json
3030
import yaml
3131

3232
from cli import options
33-
from sonar import exceptions, errcodes, utilities
33+
from sonar import exceptions, errcodes, utilities, version
3434
from sonar.util import types
3535
import sonar.logging as log
3636
from sonar import platform, rules, qualityprofiles, qualitygates, users, groups
@@ -50,8 +50,9 @@
5050
options.WHAT_PORTFOLIOS,
5151
]
5252

53-
__JSON_KEY_PLATFORM = "platform"
53+
TOOL_NAME = "sonar-config"
5454

55+
__JSON_KEY_PLATFORM = "platform"
5556
__JSON_KEY_SETTINGS = "globalSettings"
5657
__JSON_KEY_USERS = "users"
5758
__JSON_KEY_GROUPS = "groups"
@@ -74,8 +75,6 @@
7475
options.WHAT_PORTFOLIOS: __JSON_KEY_PORTFOLIOS,
7576
}
7677

77-
_WRITE_LOCK = Lock()
78-
7978

8079
_EXPORT_CALLS = {
8180
"platform": [__JSON_KEY_PLATFORM, platform.basics],
@@ -123,8 +122,7 @@ def __parse_args(desc):
123122
help="By default, sonar-config exports multi-valued settings as comma separated strings instead of arrays (if there is not comma in values). "
124123
"Set this flag if you want to force export multi valued settings as arrays",
125124
)
126-
args = options.parse_and_check(parser=parser, logger_name="sonar-config")
127-
return args
125+
return options.parse_and_check(parser=parser, logger_name=TOOL_NAME)
128126

129127

130128
def __write_export(config: dict[str, str], file: str, format: str) -> None:
@@ -320,6 +318,7 @@ def main() -> None:
320318
kwargs = utilities.convert_args(__parse_args("Extract SonarQube platform configuration"))
321319
endpoint = platform.Platform(**kwargs)
322320
endpoint.verify_connection()
321+
endpoint.set_user_agent(f"{TOOL_NAME} {version.PACKAGE_VERSION}")
323322
except (options.ArgumentsError, exceptions.ObjectNotFound) as e:
324323
utilities.exit_fatal(e.message, e.errcode)
325324
if not kwargs[options.EXPORT] and not kwargs[options.IMPORT]:

cli/cust_measures.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929

3030
from cli import options
3131
import sonar.logging as log
32-
from sonar import custom_measures, platform, utilities, errcodes, exceptions
32+
from sonar import custom_measures, platform, utilities, errcodes, exceptions, version
33+
34+
TOOL_NAME = "sonar-custom-measures"
3335

3436

3537
def parse_args(desc):
@@ -38,7 +40,7 @@ def parse_args(desc):
3840
parser.add_argument("-m", "--metricKey", required=True, help="What custom metric to work on")
3941
parser.add_argument("--value", required=False, help="Updates the value of the metric")
4042
parser.add_argument("--description", required=False, help="Updates the description of the metric")
41-
return options.parse_and_check(parser, logger_name="sonar-custom-measures")
43+
return options.parse_and_check(parser, logger_name=TOOL_NAME)
4244

4345

4446
def main():
@@ -47,6 +49,7 @@ def main():
4749
kwargs = utilities.convert_args(parse_args("Manipulate custom metrics"))
4850
sqenv = platform.Platform(**kwargs)
4951
sqenv.verify_connection()
52+
sqenv.set_user_agent(f"{TOOL_NAME} {version.PACKAGE_VERSION}")
5053
except (options.ArgumentsError, exceptions.ObjectNotFound) as e:
5154
utilities.exit_fatal(e.message, e.errcode)
5255
if sqenv.version() >= (9, 0, 0):

cli/findings_export.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,17 @@
3232
from queue import Queue
3333
import threading
3434
from threading import Thread
35-
from requests import HTTPError, RequestException
35+
from requests import RequestException
3636

3737
from cli import options
3838
from sonar.util.types import ConfigSettings
3939
import sonar.logging as log
40-
from sonar import platform, exceptions, errcodes
40+
from sonar import platform, exceptions, errcodes, version
4141
from sonar import issues, hotspots, findings
4242
from sonar import projects, applications, portfolios
4343
import sonar.utilities as util
4444

45+
TOOL_NAME = "sonar-findings"
4546
WRITE_END = object()
4647
TOTAL_FINDINGS = 0
4748
IS_FIRST = True
@@ -140,8 +141,7 @@ def parse_args(desc):
140141
options.add_url_arg(parser)
141142
options.add_dateformat_arg(parser)
142143
options.add_language_arg(parser, "findings")
143-
args = options.parse_and_check(parser=parser, logger_name="sonar-findings-export")
144-
return args
144+
return options.parse_and_check(parser=parser, logger_name=TOOL_NAME)
145145

146146

147147
def __write_header(**kwargs) -> None:
@@ -402,6 +402,7 @@ def main():
402402
kwargs = util.convert_args(parse_args("Sonar findings export"))
403403
sqenv = platform.Platform(**kwargs)
404404
sqenv.verify_connection()
405+
sqenv.set_user_agent(f"{TOOL_NAME} {version.PACKAGE_VERSION}")
405406
except (options.ArgumentsError, exceptions.ObjectNotFound) as e:
406407
util.exit_fatal(e.message, e.errcode)
407408
del kwargs[options.TOKEN]

cli/findings_sync.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333

3434
from cli import options
3535
import sonar.logging as log
36-
from sonar import platform, syncer, exceptions, projects, branches, errcodes
36+
from sonar import platform, syncer, exceptions, projects, branches, errcodes, version
3737
import sonar.utilities as util
3838

39-
_WITH_COMMENTS = {"additionalFields": "comments"}
39+
TOOL_NAME = "sonar-findings-sync"
4040

4141

4242
def __parse_args(desc):
@@ -88,8 +88,7 @@ def __parse_args(desc):
8888
help="If specified, will not add a link to source issue in the target issue comments",
8989
)
9090

91-
args = options.parse_and_check(parser=parser, logger_name="sonar-findings-sync")
92-
return args
91+
return options.parse_and_check(parser=parser, logger_name=TOOL_NAME)
9392

9493

9594
def __dump_report(report, file):
@@ -114,6 +113,7 @@ def main() -> int:
114113
params = util.convert_args(args)
115114
source_env = platform.Platform(**params)
116115
source_env.verify_connection()
116+
source_env.set_user_agent(f"{TOOL_NAME} {version.PACKAGE_VERSION}")
117117
except (options.ArgumentsError, exceptions.ObjectNotFound) as e:
118118
util.exit_fatal(e.message, e.errcode)
119119

@@ -135,6 +135,7 @@ def main() -> int:
135135
target_params = util.convert_args(args, second_platform=True)
136136
target_env = platform.Platform(**target_params)
137137
target_env.verify_connection()
138+
target_env.set_user_agent(f"{TOOL_NAME} {version.PACKAGE_VERSION}")
138139
except (options.ArgumentsError, exceptions.ObjectNotFound) as e:
139140
util.exit_fatal(e.message, e.errcode)
140141

@@ -159,6 +160,7 @@ def main() -> int:
159160
}
160161

161162
report = []
163+
counters = {}
162164
try:
163165
if not projects.exists(source_key, endpoint=source_env):
164166
raise exceptions.ObjectNotFound(source_key, f"Project key '{source_key}' does not exist")

cli/housekeeper.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131

3232
from cli import options
3333
import sonar.logging as log
34-
from sonar import platform, tokens, users, projects, branches, pull_requests
34+
from sonar import platform, tokens, users, projects, branches, pull_requests, version
3535
import sonar.utilities as util
3636
import sonar.exceptions as ex
3737
from sonar.audit import config, problem
3838

39+
TOOL_NAME = "sonar-housekeeper"
40+
3941

4042
def get_project_problems(max_days_proj, max_days_branch, max_days_pr, nb_threads, endpoint):
4143
problems = []
@@ -153,8 +155,7 @@ def _parse_arguments():
153155
default=_DEFAULT_TOKEN_OBSOLESCENCE,
154156
help=f"Deletes user tokens older than a certain number of days, by default {_DEFAULT_TOKEN_OBSOLESCENCE} days",
155157
)
156-
args = options.parse_and_check(parser=parser, logger_name="sonar-housekeeper")
157-
return args
158+
return options.parse_and_check(parser=parser, logger_name=TOOL_NAME)
158159

159160

160161
def _delete_objects(problems, mode):
@@ -207,6 +208,7 @@ def main():
207208
kwargs = util.convert_args(_parse_arguments())
208209
sq = platform.Platform(**kwargs)
209210
sq.verify_connection()
211+
sq.set_user_agent(f"{TOOL_NAME} {version.PACKAGE_VERSION}")
210212
except (options.ArgumentsError, ex.ObjectNotFound) as e:
211213
util.exit_fatal(e.message, e.errcode)
212214

cli/loc.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828

2929
from cli import options
3030
import sonar.logging as log
31-
from sonar import platform, portfolios, applications, projects, errcodes, exceptions
31+
from sonar import platform, portfolios, applications, projects, errcodes, exceptions, version
3232
import sonar.utilities as util
3333

34+
TOOL_NAME = "sonar-loc"
35+
3436

3537
def __get_csv_header_list(**kwargs) -> list[str]:
3638
"""Returns CSV header"""
@@ -194,7 +196,7 @@ def __parse_args(desc):
194196
action="store_true",
195197
help="Extracts only toplevel portfolios LoCs, not sub-portfolios",
196198
)
197-
args = options.parse_and_check(parser=parser, logger_name="sonar-loc")
199+
args = options.parse_and_check(parser=parser, logger_name=TOOL_NAME)
198200
return args
199201

200202

@@ -220,6 +222,7 @@ def main() -> None:
220222
)
221223
endpoint = platform.Platform(**kwargs)
222224
endpoint.verify_connection()
225+
endpoint.set_user_agent(f"{TOOL_NAME} {version.PACKAGE_VERSION}")
223226
except (options.ArgumentsError, exceptions.ConnectionError) as e:
224227
util.exit_fatal(e.message, e.errcode)
225228

cli/measures_export.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@
3333
from sonar.util import types
3434
from cli import options
3535
import sonar.logging as log
36-
from sonar import metrics, platform, exceptions, errcodes
36+
from sonar import metrics, platform, exceptions, errcodes, version
3737
from sonar import projects, applications, portfolios
3838
import sonar.utilities as util
3939

40+
TOOL_NAME = "sonar-measures"
4041
RATINGS = "letters"
4142
PERCENTS = "float"
4243
DATEFMT = "datetime"
@@ -140,7 +141,7 @@ def __parse_args(desc):
140141
)
141142
options.add_dateformat_arg(parser)
142143
options.add_url_arg(parser)
143-
args = options.parse_and_check(parser=parser, logger_name="sonar-measures-export")
144+
args = options.parse_and_check(parser=parser, logger_name=TOOL_NAME)
144145
if args.ratingsAsNumbers:
145146
CONVERT_OPTIONS["ratings"] = "numbers"
146147
if args.percentsAsString:
@@ -295,6 +296,7 @@ def main() -> None:
295296
kwargs = util.convert_args(__parse_args("Extract measures of projects"))
296297
endpoint = platform.Platform(**kwargs)
297298
endpoint.verify_connection()
299+
endpoint.set_user_agent(f"{TOOL_NAME} {version.PACKAGE_VERSION}")
298300
except (options.ArgumentsError, exceptions.ObjectNotFound) as e:
299301
util.exit_fatal(e.message, e.errcode)
300302

cli/projects_cli.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929

3030
from cli import options
3131
import sonar.logging as log
32-
from sonar import errcodes, exceptions, utilities
32+
from sonar import errcodes, exceptions, utilities, version
3333
from sonar import platform, projects
3434

35+
TOOL_NAME = "sonar-projects"
36+
3537

3638
def __export_projects(endpoint: platform.Platform, **kwargs) -> None:
3739
"""Exports a list (or all) of projects into zip files"""
@@ -126,9 +128,10 @@ def main() -> None:
126128
help="Maximum wait time for export of 1 project",
127129
)
128130
try:
129-
kwargs = utilities.convert_args(options.parse_and_check(parser=parser, logger_name="sonar-projects"))
131+
kwargs = utilities.convert_args(options.parse_and_check(parser=parser, logger_name=TOOL_NAME))
130132
sq = platform.Platform(**kwargs)
131133
sq.verify_connection()
134+
sq.set_user_agent(f"{TOOL_NAME} {version.PACKAGE_VERSION}")
132135
except (options.ArgumentsError, exceptions.ObjectNotFound) as e:
133136
utilities.exit_fatal(e.message, e.errcode)
134137

cli/rules_cli.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@
2626

2727
from cli import options
2828
import sonar.logging as log
29-
from sonar import rules, platform, exceptions, errcodes
29+
from sonar import rules, platform, exceptions, errcodes, version
3030
import sonar.utilities as util
3131

32+
TOOL_NAME = "sonar-rules"
33+
3234

3335
def __parse_args(desc: str) -> object:
3436
"""Sets and parses CLI arguments"""
3537
parser = options.set_common_args(desc)
3638
parser = options.set_output_file_args(parser, allowed_formats=("json", "csv"))
3739
parser = options.add_language_arg(parser, "rules")
3840
parser = options.add_import_export_arg(parser, "rules", import_opt=False)
39-
args = options.parse_and_check(parser=parser, logger_name="sonar-rules")
41+
args = options.parse_and_check(parser=parser, logger_name=TOOL_NAME)
4042
return args
4143

4244

@@ -68,6 +70,7 @@ def main() -> int:
6870
kwargs = util.convert_args(__parse_args("Extract rules"))
6971
endpoint = platform.Platform(**kwargs)
7072
endpoint.verify_connection()
73+
endpoint.set_user_agent(f"{TOOL_NAME} {version.PACKAGE_VERSION}")
7174
except (options.ArgumentsError, exceptions.ObjectNotFound) as e:
7275
util.exit_fatal(e.message, e.errcode)
7376
file = kwargs[options.REPORT_FILE]

0 commit comments

Comments
 (0)