|
| 1 | +#!/usr/local/bin/python3 |
| 2 | +# |
| 3 | +# sonar-tools |
| 4 | +# Copyright (C) 2022-2024 Olivier Korach |
| 5 | +# mailto:olivier.korach AT gmail DOT com |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU Lesser General Public |
| 9 | +# License as published by the Free Software Foundation; either |
| 10 | +# version 3 of the License, or (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +# Lesser General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser General Public License |
| 18 | +# along with this program; if not, write to the Free Software Foundation, |
| 19 | +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | +# |
| 21 | +""" |
| 22 | + Exports SonarQube platform configuration as JSON |
| 23 | +""" |
| 24 | +import sys |
| 25 | +import json |
| 26 | +import yaml |
| 27 | + |
| 28 | +from cli import options |
| 29 | +from sonar import exceptions, errcodes, utilities |
| 30 | +import sonar.logging as log |
| 31 | +from sonar import platform, rules, qualityprofiles, qualitygates, users, groups |
| 32 | +from sonar import projects, portfolios, applications |
| 33 | + |
| 34 | +_EVERYTHING = [ |
| 35 | + options.WHAT_SETTINGS, |
| 36 | + options.WHAT_USERS, |
| 37 | + options.WHAT_GROUPS, |
| 38 | + options.WHAT_GATES, |
| 39 | + options.WHAT_RULES, |
| 40 | + options.WHAT_PROFILES, |
| 41 | + options.WHAT_PROJECTS, |
| 42 | + options.WHAT_APPS, |
| 43 | + options.WHAT_PORTFOLIOS, |
| 44 | +] |
| 45 | + |
| 46 | +__JSON_KEY_PLATFORM = "platform" |
| 47 | + |
| 48 | +__JSON_KEY_SETTINGS = "globalSettings" |
| 49 | +__JSON_KEY_USERS = "users" |
| 50 | +__JSON_KEY_GROUPS = "groups" |
| 51 | +__JSON_KEY_GATES = "qualityGates" |
| 52 | +__JSON_KEY_RULES = "rules" |
| 53 | +__JSON_KEY_PROFILES = "qualityProfiles" |
| 54 | +__JSON_KEY_PROJECTS = "projects" |
| 55 | +__JSON_KEY_APPS = "applications" |
| 56 | +__JSON_KEY_PORTFOLIOS = "portfolios" |
| 57 | + |
| 58 | +__MAP = { |
| 59 | + options.WHAT_SETTINGS: __JSON_KEY_SETTINGS, |
| 60 | + options.WHAT_USERS: __JSON_KEY_USERS, |
| 61 | + options.WHAT_GROUPS: __JSON_KEY_GROUPS, |
| 62 | + options.WHAT_GATES: __JSON_KEY_GATES, |
| 63 | + options.WHAT_RULES: __JSON_KEY_RULES, |
| 64 | + options.WHAT_PROFILES: __JSON_KEY_PROFILES, |
| 65 | + options.WHAT_PROJECTS: __JSON_KEY_PROJECTS, |
| 66 | + options.WHAT_APPS: __JSON_KEY_APPS, |
| 67 | + options.WHAT_PORTFOLIOS: __JSON_KEY_PORTFOLIOS, |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +def __parse_args(desc): |
| 72 | + parser = options.set_common_args(desc) |
| 73 | + parser = options.set_key_arg(parser) |
| 74 | + parser = options.set_output_file_args(parser, allowed_formats=("json",)) |
| 75 | + parser = options.add_thread_arg(parser, "migration export") |
| 76 | + parser = options.set_what(parser, what_list=_EVERYTHING, operation="export") |
| 77 | + parser = options.add_import_export_arg(parser, "migration") |
| 78 | + parser.add_argument( |
| 79 | + "--exportDefaults", |
| 80 | + required=False, |
| 81 | + default=False, |
| 82 | + action="store_true", |
| 83 | + help="Also exports settings values that are the platform defaults. " |
| 84 | + f"By default the export will show the value as '{utilities.DEFAULT}' " |
| 85 | + "and the setting will not be imported at import time", |
| 86 | + ) |
| 87 | + args = options.parse_and_check(parser=parser, logger_name="sonar-migration") |
| 88 | + return args |
| 89 | + |
| 90 | + |
| 91 | +def __write_export(config: dict[str, str], file: str) -> None: |
| 92 | + """Writes the configuration in file""" |
| 93 | + with utilities.open_file(file) as fd: |
| 94 | + print(utilities.json_dump(config), file=fd) |
| 95 | + |
| 96 | + |
| 97 | +def __export_config(endpoint: platform.Platform, what: list[str], **kwargs) -> None: |
| 98 | + """Exports a platform configuration in a JSON file""" |
| 99 | + export_settings = { |
| 100 | + "INLINE_LISTS": False, |
| 101 | + "EXPORT_DEFAULTS": True, |
| 102 | + # "FULL_EXPORT": kwargs["fullExport"], |
| 103 | + "FULL_EXPORT": False, |
| 104 | + "MODE": "MIGRATION", |
| 105 | + "THREADS": kwargs[options.NBR_THREADS], |
| 106 | + } |
| 107 | + if "projects" in what and kwargs[options.KEYS]: |
| 108 | + non_existing_projects = [key for key in kwargs[options.KEYS] if not projects.exists(key, endpoint)] |
| 109 | + if len(non_existing_projects) > 0: |
| 110 | + utilities.exit_fatal(f"Project key(s) '{','.join(non_existing_projects)}' do(es) not exist", errcodes.NO_SUCH_KEY) |
| 111 | + |
| 112 | + calls = { |
| 113 | + options.WHAT_SETTINGS: [__JSON_KEY_SETTINGS, platform.export], |
| 114 | + options.WHAT_RULES: [__JSON_KEY_RULES, rules.export], |
| 115 | + options.WHAT_PROFILES: [__JSON_KEY_PROFILES, qualityprofiles.export], |
| 116 | + options.WHAT_GATES: [__JSON_KEY_GATES, qualitygates.export], |
| 117 | + options.WHAT_PROJECTS: [__JSON_KEY_PROJECTS, projects.export], |
| 118 | + options.WHAT_APPS: [__JSON_KEY_APPS, applications.export], |
| 119 | + options.WHAT_PORTFOLIOS: [__JSON_KEY_PORTFOLIOS, portfolios.export], |
| 120 | + options.WHAT_USERS: [__JSON_KEY_USERS, users.export], |
| 121 | + options.WHAT_GROUPS: [__JSON_KEY_GROUPS, groups.export], |
| 122 | + } |
| 123 | + |
| 124 | + log.info("Exporting configuration from %s", kwargs[options.URL]) |
| 125 | + key_list = kwargs[options.KEYS] |
| 126 | + sq_settings = {__JSON_KEY_PLATFORM: endpoint.basics()} |
| 127 | + for what_item, call_data in calls.items(): |
| 128 | + if what_item not in what: |
| 129 | + continue |
| 130 | + ndx, func = call_data |
| 131 | + try: |
| 132 | + sq_settings[ndx] = func(endpoint, export_settings=export_settings, key_list=key_list) |
| 133 | + except exceptions.UnsupportedOperation as e: |
| 134 | + log.warning(e.message) |
| 135 | + sq_settings = utilities.remove_empties(sq_settings) |
| 136 | + # if not kwargs.get("dontInlineLists", False): |
| 137 | + # sq_settings = utilities.inline_lists(sq_settings, exceptions=("conditions",)) |
| 138 | + __write_export(sq_settings, kwargs[options.REPORT_FILE]) |
| 139 | + |
| 140 | + |
| 141 | +def main() -> None: |
| 142 | + """Main entry point for sonar-config""" |
| 143 | + start_time = utilities.start_clock() |
| 144 | + try: |
| 145 | + kwargs = utilities.convert_args(__parse_args("Extract SonarQube platform configuration")) |
| 146 | + endpoint = platform.Platform(**kwargs) |
| 147 | + endpoint.verify_connection() |
| 148 | + except (options.ArgumentsError, exceptions.ObjectNotFound) as e: |
| 149 | + utilities.exit_fatal(e.message, e.errcode) |
| 150 | + |
| 151 | + what = utilities.check_what(kwargs.pop(options.WHAT, None), _EVERYTHING, "exported") |
| 152 | + if options.WHAT_PROFILES in what and options.WHAT_RULES not in what: |
| 153 | + what.append(options.WHAT_RULES) |
| 154 | + kwargs[options.FORMAT] = "json" |
| 155 | + if kwargs[options.REPORT_FILE] is None: |
| 156 | + kwargs[options.REPORT_FILE] = f"sonar-migration.{endpoint.server_id()}.json" |
| 157 | + try: |
| 158 | + __export_config(endpoint, what, **kwargs) |
| 159 | + except exceptions.ObjectNotFound as e: |
| 160 | + utilities.exit_fatal(e.message, errcodes.NO_SUCH_KEY) |
| 161 | + except (PermissionError, FileNotFoundError) as e: |
| 162 | + utilities.exit_fatal(f"OS error while exporting config: {e}", exit_code=errcodes.OS_ERROR) |
| 163 | + log.info("Exporting SQ to SC migration data from %s completed", kwargs[options.URL]) |
| 164 | + log.info("Migration file '%s' created", kwargs[options.REPORT_FILE]) |
| 165 | + utilities.stop_clock(start_time) |
| 166 | + sys.exit(0) |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == "__main__": |
| 170 | + main() |
0 commit comments