From 8998ff18a53cdaa539a084ec0173f021fbb60098 Mon Sep 17 00:00:00 2001 From: Ryan Lerch Date: Jul 30 2020 10:52:00 +0000 Subject: add repoupdatev1 message Signed-off-by: Ryan Lerch --- diff --git a/mdapi_messages/__init__.py b/mdapi_messages/__init__.py index da98b34..da8ee8a 100644 --- a/mdapi_messages/__init__.py +++ b/mdapi_messages/__init__.py @@ -14,4 +14,4 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -from .thing import NewThingV1 # noqa: F401 +from .messages import RepoUpdateV1 # noqa: F401 diff --git a/mdapi_messages/base.py b/mdapi_messages/base.py index af7a42c..d968ad2 100644 --- a/mdapi_messages/base.py +++ b/mdapi_messages/base.py @@ -15,22 +15,9 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. from fedora_messaging import message -from fedora_messaging.schema_utils import user_avatar_url - SCHEMA_URL = "http://fedoraproject.org/message-schema/" -THING_SCHEMA = { - "type": "object", - "properties": { - "id": {"type": "number"}, - "name": {"type": "string"}, - "foobar": {"type": ["string", "null"]}, - "url": {"type": "string", "format": "uri"}, - }, - "required": ["id", "name"], -} - class mdapiMessage(message.Message): """ @@ -45,22 +32,3 @@ class mdapiMessage(message.Message): @property def app_icon(self): return "https://apps.fedoraproject.org/img/icons/mdapi.png" - - @property - def agent(self): - return self.body.get("agent") - - @property - def agent_avatar(self): - return user_avatar_url(self.agent) - - @property - def usernames(self): - return [self.agent] - - @property - def url(self): - try: - return self.body["thing"]["url"] - except KeyError: - return None diff --git a/mdapi_messages/messages.py b/mdapi_messages/messages.py new file mode 100644 index 0000000..43f49d4 --- /dev/null +++ b/mdapi_messages/messages.py @@ -0,0 +1,54 @@ +# Copyright (C) 2020 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +from .base import SCHEMA_URL, mdapiMessage + + +class RepoUpdateV1(mdapiMessage): + """ + A sub-class of a Fedora message that defines a message schema for messages + published by mdapi when a repo's info is updated. + """ + + topic = "mdapi.repo.update" + + body_schema = { + "id": SCHEMA_URL + topic, + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Schema for messages sent when a repo is updated", + "type": "object", + "properties": { + "name": {"type": "string"}, + "packages": {"type": "array", "contains": {"type": "string"}}, + "url": {"type": "string", "format": "uri"}, + }, + "required": ["name", "packages"], + } + + def __str__(self): + """Return a complete human-readable representation of the message.""" + return ( + f"MDAPI updated repo {self.body['name']} with new details for the packages: " + f"{', '.join(p for p in self.body['packages'])}" + ) + + @property + def summary(self): + """Return a summary of the message.""" + return ( + f"MDAPI updated repo {self.body['name']} with new details for " + f"{len(self.body['packages'])} packages" + ) diff --git a/mdapi_messages/tests/test_common.py b/mdapi_messages/tests/test_common.py index 808c8a2..d9c1024 100644 --- a/mdapi_messages/tests/test_common.py +++ b/mdapi_messages/tests/test_common.py @@ -16,27 +16,14 @@ """Unit tests for common properties of the message schemas.""" -from ..thing import NewThingV1 -from .utils import DUMMY_THING +from ..messages import RepoUpdateV1 +from .utils import DUMMY_UPDATE def test_properties(): """Assert some properties are correct.""" - body = { - "agent": "dummy-user", - "thing": DUMMY_THING, - } - message = NewThingV1(body=body) + body = DUMMY_UPDATE + message = RepoUpdateV1(body=body) assert message.app_name == "mdapi" - assert ( - message.app_icon - == "https://apps.fedoraproject.org/img/icons/mdapi.png" - ) - assert message.agent == "dummy-user" - assert message.agent_avatar == ( - "https://seccdn.libravatar.org/avatar/" - "18e8268125372e35f95ef082fd124e9274d46916efe2277417fa5fecfee31af1" - "?s=64&d=retro" - ) - assert message.usernames == ["dummy-user"] + assert message.app_icon == "https://apps.fedoraproject.org/img/icons/mdapi.png" diff --git a/mdapi_messages/tests/test_messages.py b/mdapi_messages/tests/test_messages.py new file mode 100644 index 0000000..4a0a6f6 --- /dev/null +++ b/mdapi_messages/tests/test_messages.py @@ -0,0 +1,59 @@ +# Copyright (C) 2020 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Unit tests for the message schema.""" + +import pytest + +from jsonschema import ValidationError +from ..messages import RepoUpdateV1 +from .utils import DUMMY_UPDATE + + +def test_repo_update_v1(): + """ + Assert the message schema validates a message with the required fields. + """ + body = DUMMY_UPDATE + message = RepoUpdateV1(body=body) + message.validate() + + +def test_missing_fields(): + """Assert an exception is actually raised on validation failure.""" + minimal_message = {} + message = RepoUpdateV1(body=minimal_message) + with pytest.raises(ValidationError): + message.validate() + + +def test_str(): + """Assert __str__ produces a human-readable message.""" + body = DUMMY_UPDATE + expected_str = ( + "MDAPI updated repo rawhide with new details for the packages: kernel, inkscape" + ) + message = RepoUpdateV1(body=body) + message.validate() + assert expected_str == str(message) + + +def test_summary(): + """Assert the summary is correct.""" + body = DUMMY_UPDATE + expected_summary = "MDAPI updated repo rawhide with new details for 2 packages" + message = RepoUpdateV1(body=body) + assert expected_summary == message.summary diff --git a/mdapi_messages/tests/test_thing.py b/mdapi_messages/tests/test_thing.py deleted file mode 100644 index 5537fce..0000000 --- a/mdapi_messages/tests/test_thing.py +++ /dev/null @@ -1,85 +0,0 @@ -# Copyright (C) 2020 Red Hat, Inc. -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along -# with this program; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -"""Unit tests for the message schema.""" - -import pytest - -from jsonschema import ValidationError -from ..thing import NewThingV1 -from .utils import DUMMY_THING - - -def test_minimal(): - """ - Assert the message schema validates a message with the required fields. - """ - body = { - "agent": "dummy-user", - "thing": DUMMY_THING, - } - message = NewThingV1(body=body) - message.validate() - assert message.url is None - - -def test_full(): - """ - Assert the message schema validates a message with the required fields. - """ - thing = DUMMY_THING.copy() - thing["url"] = "http://localhost/thing" - body = { - "agent": "dummy-user", - "thing": thing, - } - message = NewThingV1(body=body) - message.validate() - assert message.url == "http://localhost/thing" - - -def test_missing_fields(): - """Assert an exception is actually raised on validation failure.""" - minimal_message = { - "agent": "dummy-user", - "thing": {"id": 1}, - } - message = NewThingV1(body=minimal_message) - with pytest.raises(ValidationError): - message.validate() - - -def test_str(): - """Assert __str__ produces a human-readable message.""" - body = { - "agent": "dummy-user", - "thing": DUMMY_THING, - } - expected_str = "New Thing: dummy\nBy: dummy-user\n" - message = NewThingV1(body=body) - message.validate() - assert expected_str == str(message) - - -def test_summary(): - """Assert the summary is correct.""" - body = { - "agent": "dummy-user", - "thing": DUMMY_THING, - } - expected_summary = 'dummy-user created thing "dummy" (1)' - message = NewThingV1(body=body) - assert expected_summary == message.summary diff --git a/mdapi_messages/tests/utils.py b/mdapi_messages/tests/utils.py index b4056fa..413a7ef 100644 --- a/mdapi_messages/tests/utils.py +++ b/mdapi_messages/tests/utils.py @@ -17,7 +17,8 @@ """Utilities for the unit tests.""" -DUMMY_THING = { - "id": 1, - "name": "dummy", +DUMMY_UPDATE = { + "name": "rawhide", + "packages": ["kernel", "inkscape"], + "url": "https://dl.fedoraproject.org/pub/fedora/linux/development/rawhide/", } diff --git a/mdapi_messages/thing.py b/mdapi_messages/thing.py deleted file mode 100644 index 47c7b5e..0000000 --- a/mdapi_messages/thing.py +++ /dev/null @@ -1,50 +0,0 @@ -# Copyright (C) 2020 Red Hat, Inc. -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along -# with this program; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -from .base import SCHEMA_URL, mdapiMessage, THING_SCHEMA - - -class NewThingV1(mdapiMessage): - """ - A sub-class of a Fedora message that defines a message schema for messages - published by mdapi when a new thing is created. - """ - - topic = "mdapi.new" - - body_schema = { - "id": SCHEMA_URL + topic, - "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Schema for messages sent when a new thing is created", - "type": "object", - "properties": {"agent": {"type": "string"}, "thing": THING_SCHEMA}, - "required": ["agent", "thing"], - } - - def __str__(self): - """Return a complete human-readable representation of the message.""" - return "New Thing: {thing}\nBy: {agent}\n".format( - thing=self.body["thing"]["name"], agent=self.body["agent"], - ) - - @property - def summary(self): - """Return a summary of the message.""" - return '{agent} created thing "{name}" ({id})'.format( - agent=self.body["agent"], - name=self.body["thing"]["name"], - id=self.body["thing"]["id"], - ) diff --git a/setup.cfg b/setup.cfg index afe7d63..3fa0cf1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -26,7 +26,7 @@ install_requires = [options.entry_points] fedora.messages = - mdapi.new=mdapi_messages.thing:NewThingV1 + mdapi.repo.update=mdapi_messages.messages:RepoUpdateV1