From 64ea2a957171534c78ea69ffe5c159ec96db306e Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Sep 10 2020 08:58:19 +0000 Subject: Print the list of affected SSTs by the compose change. --- diff --git a/cccc/compose.py b/cccc/compose.py index 8776839..209c3dd 100644 --- a/cccc/compose.py +++ b/cccc/compose.py @@ -6,6 +6,8 @@ import json import os import tempfile import shutil +import kobo.rpmlib +import requests import odcs.client.odcs @@ -132,6 +134,64 @@ def wait_for_compose(conf, compose_id): return compose_url +def print_affected_ssts(conf, diff_rpms_brief_path): + """ + Prints the list of affected SSTs by the difference between two + composes. The mapping between component and SST is obtained from + bugzilla. + """ + if not conf["bugzilla_api_key_file"]: + return + + with open(diff_rpms_brief_path, "r") as f: + data = json.loads(f.read()) + + srpm_names = set() + for rpms in data.values(): + for rpm_data in rpms.values(): + srpm_name = kobo.rpmlib.parse_nvra(rpm_data["srpm_nevra"])["name"] + srpm_names.add(srpm_name) + + with open(conf["bugzilla_api_key_file"], "r") as f: + bugzilla_api_key = f.readline().strip() + + bugzilla_request = { + "jsonrpc": "1.0", + "id": "1", + "method": "ManageComponents.get_components", + "params": [{ + "product": conf["bugzilla_product"], + "show_nested": 1, + "api_key": bugzilla_api_key, + }] + } + r = requests.post(conf["bugzilla_jsonrpc_url"], json=bugzilla_request) + r.raise_for_status() + components = {} + for component in r.json()["result"]["components"]: + if not component["default_pool"]: + continue + name = component["name"] + if "" in name: + s = name.split("") + name = s[1] + "-" + s[0] + + components[name] = component.get("default_pool", {}).get("team", {}).get("name") + + print("") + print("Affected SSTs:") + + affected_ssts = set() + for srpm_name in srpm_names: + if srpm_name not in components or not components[srpm_name]: + print(" - WARNING: SST for component %s is unknown" % srpm_name) + continue + affected_ssts.add(components[srpm_name]) + + for sst in sorted(affected_ssts): + print(" - %s" % sst) + + def compare_composes(conf, old, new): """ Compare compose data from old (default) to new (updated) composes. @@ -171,6 +231,8 @@ def compare_composes(conf, old, new): with open(compare_file, "r") as f: for line in f: print("%s" % line.rstrip()) + + print_affected_ssts(conf, os.path.join(output_dir, "diff-rpms-brief.json")) finally: if output_dir is not None: shutil.rmtree(output_dir) diff --git a/cccc/config.py b/cccc/config.py index a78ff0e..c451271 100644 --- a/cccc/config.py +++ b/cccc/config.py @@ -20,6 +20,9 @@ _DefaultConfig = { "odcs_raw_config_name": "odcs_cccc", "odcs_server_url": "https://127.0.0.1/", "verbose": False, + "bugzilla_jsonrpc_url": "https://bugzilla.redhat.com/jsonrpc.cgi", + "bugzilla_product": "", + "bugzilla_api_key_file": "", } _RHELConfiguration = { diff --git a/requirements.txt b/requirements.txt index bf2f1aa..53704c7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,7 @@ kobo odcs[client] openidc_client +kobo +koji +rpm # compose-utils diff --git a/tests/data/utils/bugzilla_api_key b/tests/data/utils/bugzilla_api_key new file mode 100644 index 0000000..323fae0 --- /dev/null +++ b/tests/data/utils/bugzilla_api_key @@ -0,0 +1 @@ +foobar diff --git a/tests/test_compose.py b/tests/test_compose.py index 0c560b3..09b7284 100644 --- a/tests/test_compose.py +++ b/tests/test_compose.py @@ -17,6 +17,9 @@ except ImportError: CONFIG_DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), "data", "config") COMPOSES_DATA_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), "data", "composes") +BUGZILLA_API_KEY_FILE = os.path.join( + os.path.abspath(os.path.dirname(__file__)), "data", "utils", "bugzilla_api_key" +) class TestCompose(unittest.TestCase): @@ -47,21 +50,52 @@ class TestCompose(unittest.TestCase): compose_url) @patch('sys.stdout', new=StringIO()) - def test_compare_composes(self): + @patch("cccc.compose.requests.post") + def test_compare_composes(self, requests_post): """ Tests that composes can be compared. """ conf = cccc.config.init_config() + conf["bugzilla_api_key_file"] = BUGZILLA_API_KEY_FILE + conf["bugzilla_product"] = "Fedora 32" webserver = LocalWebServer(COMPOSES_DATA_DIR) default_compose = webserver.base_url() + "/odcs-fake01/compose" updated_compose = webserver.base_url() + "/odcs-fake02/compose" + response = requests_post.return_value + response.json.return_value = { + "error": None, + "id": "1", + "result": { + "components": [ + {"name": "zsh", "default_pool": {"team": {"name": "sst_1"}}}, + {"name": "gawk", "default_pool": {"team": {"name": "sst_2"}}}, + ] + } + } + cccc.compose.compare_composes(conf, default_compose, updated_compose) output = sys.stdout.getvalue().strip() + bugzilla_request = { + 'jsonrpc': '1.0', + 'id': '1', + 'method': 'ManageComponents.get_components', + 'params': [ + { + 'product': 'Fedora 32', + 'show_nested': 1, + 'api_key': 'foobar' + } + ] + } + requests_post.assert_called_once_with( + 'https://bugzilla.redhat.com/jsonrpc.cgi', json=bugzilla_request + ) + expected_output = ( "OLD: Fedora-ELN-Rawhide-20200601.t.0\n" "NEW: Fedora-ELN-Rawhide-20200601.t.1\n" @@ -84,6 +118,10 @@ class TestCompose(unittest.TestCase): "- zsh-debugsource.aarch64\n" "- zsh-html.aarch64\n" "- zsh.aarch64\n" - "- zsh.src" + "- zsh.src\n" + "\n" + "Affected SSTs:\n" + " - sst_1\n" + " - sst_2" ) self.assertEqual(expected_output, output) diff --git a/tests/test_config.py b/tests/test_config.py index a8d6280..263c709 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -25,6 +25,9 @@ DEFAULT_DEFAULTS = { "odcs_raw_config_name": "odcs_cccc", "odcs_server_url": "https://127.0.0.1/", "verbose": False, + "bugzilla_jsonrpc_url": "https://bugzilla.redhat.com/jsonrpc.cgi", + "bugzilla_product": "", + "bugzilla_api_key_file": "", }