Skip to content

Commit f919891

Browse files
authored
Merge pull request #1282 from okorach:yaml-export
YAML sonar-config export beta
2 parents 6fc63ae + c05cba5 commit f919891

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ dist/
1212
tmp/
1313
.coverage
1414
*.json
15+
*.yaml
16+
*.yml
1517
.venv/
1618
.DS_Store
1719

cli/config.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"""
2424
import sys
2525
import json
26+
import yaml
2627

2728
from cli import options
2829
from sonar import exceptions, errcodes, utilities
@@ -70,7 +71,7 @@
7071
def __parse_args(desc):
7172
parser = options.set_common_args(desc)
7273
parser = options.set_key_arg(parser)
73-
parser = options.set_output_file_args(parser, allowed_formats=("json",))
74+
parser = options.set_output_file_args(parser, allowed_formats=("json", "yaml"))
7475
parser = options.add_thread_arg(parser, "project export")
7576
parser = options.set_what(parser, what_list=_EVERYTHING, operation="export or import")
7677
parser = options.add_import_export_arg(parser, "configuration")
@@ -103,6 +104,15 @@ def __parse_args(desc):
103104
return args
104105

105106

107+
def __write_export(config: dict[str, str], file: str, format: str) -> None:
108+
"""Writes the configuration in file"""
109+
with utilities.open_file(file) as fd:
110+
if format == "yaml":
111+
print(yaml.dump(config), file=fd)
112+
else:
113+
print(utilities.json_dump(config), file=fd)
114+
115+
106116
def __export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> None:
107117
"""Exports a platform configuration in a JSON file"""
108118
export_settings = {
@@ -118,15 +128,12 @@ def __export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> N
118128

119129
log.info("Exporting configuration from %s", kwargs[options.URL])
120130
key_list = kwargs[options.KEYS]
121-
sq_settings = {}
122-
sq_settings[__JSON_KEY_PLATFORM] = endpoint.basics()
131+
sq_settings = {__JSON_KEY_PLATFORM: endpoint.basics()}
123132
if options.WHAT_SETTINGS in what:
124133
sq_settings[__JSON_KEY_SETTINGS] = endpoint.export(export_settings=export_settings)
125-
if options.WHAT_RULES in what:
134+
if options.WHAT_RULES in what or options.WHAT_PROFILES in what:
126135
sq_settings[__JSON_KEY_RULES] = rules.export(endpoint, export_settings=export_settings)
127136
if options.WHAT_PROFILES in what:
128-
if options.WHAT_RULES not in what:
129-
sq_settings[__JSON_KEY_RULES] = rules.export(endpoint, export_settings=export_settings)
130137
sq_settings[__JSON_KEY_PROFILES] = qualityprofiles.export(endpoint, export_settings=export_settings)
131138
if options.WHAT_GATES in what:
132139
sq_settings[__JSON_KEY_GATES] = qualitygates.export(endpoint, export_settings=export_settings)
@@ -150,8 +157,7 @@ def __export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> N
150157
sq_settings = utilities.remove_empties(sq_settings)
151158
if not kwargs["dontInlineLists"]:
152159
sq_settings = utilities.inline_lists(sq_settings, exceptions=("conditions",))
153-
with utilities.open_file(kwargs["file"]) as fd:
154-
print(utilities.json_dump(sq_settings), file=fd)
160+
__write_export(sq_settings, kwargs[options.REPORT_FILE], kwargs[options.FORMAT])
155161
log.info("Exporting configuration from %s completed", kwargs["url"])
156162

157163

@@ -193,7 +199,7 @@ def __import_config(endpoint: platform.Platform, what: list[str], **kwargs) -> N
193199
log.info("Importing configuration to %s completed", kwargs[options.URL])
194200

195201

196-
def main():
202+
def main() -> None:
197203
"""Main entry point for sonar-config"""
198204
start_time = utilities.start_clock()
199205
try:
@@ -206,6 +212,7 @@ def main():
206212
utilities.exit_fatal(f"One of --{options.EXPORT} or --{options.IMPORT} option must be chosen", exit_code=errcodes.ARGS_ERROR)
207213

208214
what = utilities.check_what(kwargs.pop(options.WHAT, None), _EVERYTHING, "exported or imported")
215+
kwargs[options.FORMAT] = utilities.deduct_format(kwargs[options.FORMAT], kwargs[options.REPORT_FILE], allowed_formats=("json", "yaml"))
209216
if kwargs[options.EXPORT]:
210217
try:
211218
__export_config(endpoint, what, **kwargs)

cli/options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def set_output_file_args(parser: ArgumentParser, help_str: str = None, allowed_f
403403
choices=allowed_formats,
404404
required=False,
405405
default=None,
406-
help="Output format for generated report.\nIf not specified, it is the output file extension if json or csv, then csv by default",
406+
help="Output format for generated report.\nIf not specified, it is the output file extension if json, csv or yaml, then csv by default",
407407
)
408408
if "csv" in allowed_formats:
409409
parser.add_argument(

sonar/utilities.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ def deduct_format(fmt: Union[str, None], filename: Union[str, None], allowed_for
569569
"""Deducts output format from CLI format and filename"""
570570
if fmt is None and filename is not None:
571571
fmt = filename.split(".").pop(-1).lower()
572+
if fmt == "yml":
573+
fmt = "yaml"
572574
if fmt not in allowed_formats:
573575
fmt = "csv"
574576
return fmt

0 commit comments

Comments
 (0)