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")