From 043fe7e519eab2704a6a44bbac26bbc85097b540 Mon Sep 17 00:00:00 2001 From: Rohan McGovern Date: Tue, 1 Jun 2021 11:23:35 +1000 Subject: [PATCH] errata: convert and validate 'type' field This field was documented as having values either "bugfix", "security" or "enhancement", but there were a couple of issues with it: - this was not enforced, so actually any values could appear there - some ancient errata found in the wild use other values here, like "RHBA" and "RHSA" Fix both of these by requiring that valid values are used, and automatically converting known legacy values when data is loaded. --- CHANGELOG.md | 5 +++++ src/pushsource/_impl/model/conv.py | 1 + src/pushsource/_impl/model/erratum.py | 17 ++++++++++++++++- tests/model/test_errata_type.py | 26 ++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/model/test_errata_type.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f82c954..9a9b02f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix usage of `errata` source when the an Errata Tool URL includes a path component. +### Changed +- On `ErratumPushItem`, the `type` attribute will now be automatically converted + from legacy values found in the wild such as "RHBA", "RHSA". Values are + now also validated. + ## [2.6.0] - 2021-05-27 ### Added diff --git a/src/pushsource/_impl/model/conv.py b/src/pushsource/_impl/model/conv.py index 7c9be182..a5ac30fb 100644 --- a/src/pushsource/_impl/model/conv.py +++ b/src/pushsource/_impl/model/conv.py @@ -106,6 +106,7 @@ def int2str(value): return value +in_ = attr.validators.in_ instance_of = attr.validators.instance_of instance_of_str = instance_of(six.string_types) optional = attr.validators.optional diff --git a/src/pushsource/_impl/model/erratum.py b/src/pushsource/_impl/model/erratum.py index 6bc89662..98f8a15c 100644 --- a/src/pushsource/_impl/model/erratum.py +++ b/src/pushsource/_impl/model/erratum.py @@ -3,6 +3,7 @@ from .base import PushItem from .. import compat_attr as attr from .conv import ( + in_, int2str, md5str, sha1str, @@ -201,6 +202,15 @@ def _from_data(cls, data): ) +def errata_type_converter(value): + # For 'type' field, ancient versions of Errata Tool produced values + # like RHBA, RHEA, RHSA, so these can be found in the wild; but all + # other sources (including our own model) prefer strings like "bugfix". + # We'll convert up from legacy values automatically. + typemap = {"RHBA": "bugfix", "RHEA": "enhancement", "RHSA": "security"} + return typemap.get(value, value) + + @attr.s() class ErratumPushItem(PushItem): """A :class:`~pushsource.PushItem` representing a single erratum (also known as "advisory"). @@ -210,7 +220,12 @@ class ErratumPushItem(PushItem): examples). """ - type = attr.ib(type=str, default="bugfix", validator=instance_of_str) + type = attr.ib( + type=str, + default="bugfix", + validator=in_(["bugfix", "security", "enhancement"]), + converter=errata_type_converter, + ) """'bugfix', 'security' or 'enhancement'.""" release = attr.ib( diff --git a/tests/model/test_errata_type.py b/tests/model/test_errata_type.py new file mode 100644 index 00000000..4fd03bb3 --- /dev/null +++ b/tests/model/test_errata_type.py @@ -0,0 +1,26 @@ +import pytest + +from pushsource import ErratumPushItem + + +@pytest.mark.parametrize( + "in_type,out_type", + [ + ("RHBA", "bugfix"), + ("RHEA", "enhancement"), + ("RHSA", "security"), + ("bugfix", "bugfix"), + ("enhancement", "enhancement"), + ("security", "security"), + ], +) +def test_type_converted(in_type, out_type): + """ErratumPushItem converts values of 'type' field to one of the expected.""" + item = ErratumPushItem(name="TEST-123", type=in_type) + assert item.type == out_type + + +def test_type_enforced(): + """ErratumPushItem complains on invalid values for 'type'""" + with pytest.raises(ValueError): + ErratumPushItem(name="TEST-123", type="oops-bad-type")