From 1c4a0854071d93f9a6d8b7b4b5d72551acb7059e Mon Sep 17 00:00:00 2001 From: Lukas Holecek Date: Jun 07 2021 12:16:24 +0000 Subject: Allow product version matching by subject ID JIRA: RHELWF-3480 --- diff --git a/conf/subject_types/compose.yaml b/conf/subject_types/compose.yaml index 2ea31a3..f0a619c 100644 --- a/conf/subject_types/compose.yaml +++ b/conf/subject_types/compose.yaml @@ -4,3 +4,6 @@ item_key: "productmd.compose.id" item_dict: # {"productmd.compose.id": ITEM} item_key: "productmd.compose.id" +product_version_match: + - match: '^(RHEL-\d*)\..*' + product_version: '\1' diff --git a/greenwave/subjects/subject.py b/greenwave/subjects/subject.py index e6cf893..209952c 100644 --- a/greenwave/subjects/subject.py +++ b/greenwave/subjects/subject.py @@ -1,5 +1,7 @@ # SPDX-License-Identifier: GPL-2.0+ +import re + def _to_dict(format_dict, item): result = {} @@ -61,6 +63,11 @@ class Subject: @property def product_version(self): + for pv_match in self._type.product_version_match: + pv = re.sub(pv_match['match'], pv_match['product_version'], self.item) + if pv and pv != self.item: + return pv.lower() + return self._type.product_version @property diff --git a/greenwave/subjects/subject_type.py b/greenwave/subjects/subject_type.py index cd0fff5..da649f9 100644 --- a/greenwave/subjects/subject_type.py +++ b/greenwave/subjects/subject_type.py @@ -40,6 +40,14 @@ class SubjectType(SafeYAMLObject): # Omit responding with HTTP 404 if there is no applicable policy. 'ignore_missing_policy': SafeYAMLBool(optional=True, default=False), + # List of dicts. Each dict must have: + # - 'match' field containing regular expression to match subject ID + # - 'product_version' field containing product version (can contain + # '\1', '\2' etc, expanded to matched groups) + 'product_version_match': SafeYAMLList(item_type=dict, optional=True), + + # Fixed product version for the subject type used if + # product_version_match is undefined or does not match subject ID. 'product_version': SafeYAMLString(optional=True), # Serialization dict for decision. diff --git a/greenwave/tests/test_subjects.py b/greenwave/tests/test_subjects.py index 808e026..6e46be9 100644 --- a/greenwave/tests/test_subjects.py +++ b/greenwave/tests/test_subjects.py @@ -65,3 +65,14 @@ def test_subject_to_repr(app): def test_subject_to_repr_generic(app): subject = create_subject('some_type', 'some_nvr') assert repr(subject) == "Subject(, 'some_nvr')" + + +@pytest.mark.parametrize('compose, expected_product_version', ( + ('RHEL-8.5.0-20210101.d.1', 'rhel-8'), + ('RHEL-9.0.0-20210101.d.2', 'rhel-9'), + ('FEDORA-2021-35759ad8d3', None), +)) +def test_subject_product_version_match(compose, expected_product_version, app): + subject = create_subject('compose', compose) + assert subject._type.product_version_match + assert subject.product_version == expected_product_version, compose