Skip to content

Commit

Permalink
Initial message schema for Anitya
Browse files Browse the repository at this point in the history
This defines message schema using json-schema for a subset of messages
sent by Anitya.

Signed-off-by: Jeremy Cline <jcline@redhat.com>
  • Loading branch information
jeremycline committed Aug 1, 2018
1 parent 74ea662 commit 832e387
Show file tree
Hide file tree
Showing 9 changed files with 786 additions and 0 deletions.
339 changes: 339 additions & 0 deletions message_schema/LICENSE

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions message_schema/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
JSON schema definitions for messages published by Anitya.

See http://json-schema.org/ for documentation on the schema format. See
https://fedora-messaging.readthedocs.io/en/latest/messages.html for
documentation on fedora-messaging.
15 changes: 15 additions & 0 deletions message_schema/anitya_schema/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (C) 2018 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.
139 changes: 139 additions & 0 deletions message_schema/anitya_schema/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Copyright (C) 2018 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.
"""The schema for messages sent by Anitya."""

from fedora_messaging import message


class DistroCreatedV1(message.Message):
"""
Message sent by Anitya to the "anitya.distro.add" topic when a new
distribution is added.
"""

body_schema = {
'id': 'https://fedoraproject.org/jsonschema/anitya_distro_createdv1.json',
'$schema': 'http://json-schema.org/draft-04/schema#',
'description': 'Message sent when a new distro is created in Anitya',
'type': 'object',
'required': ['msg'],
'properties': {
'msg': {
'type': 'object',
'description': 'The actual message',
'properties': {
'project': {'type': 'null'},
'message': {
'type': 'object',
'properties': {
'agent': {'type': 'string'},
'distro': {'type': 'string'},
},
'required': ['agent', 'distro'],
},
'distro': {
'type': 'object',
'properties': {
'name': {'type': 'string'},
},
'required': ['name'],
},
},
'required': ['project', 'message', 'distro'],
},
},
}

def __str__(self):
"""Return a complete human-readable representation of the message"""
return 'A new distribution, {}, was added to Anitya.'.format(
self.body['msg']['distro']['name'])

def summary(self):
"""Return a short summary of the message."""
return str(self)


class ProjectCreatedV1(message.Message):
"""
The message sent when a new project is created in Anitya. The message topic
is currently '<prefix>.{stg,prod}.anitya.project.add'
"""

body_schema = {
'id': 'https://fedoraproject.org/jsonschema/anitya_project_createdv1.json',
'$schema': 'http://json-schema.org/draft-04/schema#',
'description': 'Message sent when a new project is created in Anitya',
'type': 'object',
'required': ['msg'],
'properties': {
'msg': {
'type': 'object',
'description': 'The actual message',
'required': ['project', 'message', 'distro'],
'properties': {
'distro': {'type': 'null'},
'message': {
'type': 'object',
'properties': {
'agent': {'type': 'string'},
'project': {'type': 'string'},
},
'required': ['agent', 'project'],
},
'project': {
'type': 'object',
'properties': {
'backend': {'type': 'string'},
'created_on': {'type': 'number'},
'homepage': {'type': 'string'},
'id': {'type': 'integer'},
'name': {'type': 'string'},
'regex': {'anyOf': [{'type': 'string'}, {'type': 'null'}]},
'updated_on': {'type': 'number'},
'version': {'anyOf': [{'type': 'string'}, {'type': 'null'}]},
'version_url': {'anyOf': [{'type': 'string'}, {'type': 'null'}]},
'versions': {'type': 'array'},
},
'required': [
'backend',
'created_on',
'homepage',
'id',
'name',
'regex',
'updated_on',
'version',
'version_url',
'versions',
],
},
},
},
},
}

def __str__(self):
"""
Return a complete human-readable representation of the message, which
in this case is equivalent to the summary.
"""
return 'A new project, {}, was added to Anitya.'.format(
self.body['msg']['project']['name'])

def summary(self):
"""Return a summary of the message."""
return str(self)
15 changes: 15 additions & 0 deletions message_schema/anitya_schema/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (C) 2018 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.
151 changes: 151 additions & 0 deletions message_schema/anitya_schema/tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# Copyright (C) 2018 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 unittest

from jsonschema import ValidationError
from .. import schema


class DistroCreatedV1Tests(unittest.TestCase):
"""A set of unit tests to ensure the schema works as expected."""

def test_message(self):
"""
Assert the message schema validates a message with the minimal number
of required fields.
"""
body = {
'msg': {
'project': None,
'message': {
'agent': 'Bond',
'distro': 'Fedora',
},
'distro': {'name': 'Fedora'},
}
}
message = schema.DistroCreatedV1(body=body)

message.validate()

def test_missing_field(self):
"""Assert missing the 'distro' key causes a validation error."""
body = {
'msg': {
'project': None,
'message': {
'agent': 'Bond',
'distro': 'Fedora',
},
}
}
message = schema.DistroCreatedV1(body=body)

self.assertRaises(ValidationError, message.validate)

def test_str(self):
"""Assert __str__ produces a human-readable message."""
body = {
'msg': {
'project': None,
'message': {
'agent': 'Bond',
'distro': 'Fedora',
},
'distro': {'name': 'Fedora'},
}
}
message = schema.DistroCreatedV1(body=body)
expected_str = 'A new distribution, Fedora, was added to Anitya.'

message.validate()
self.assertEqual(expected_str, str(message))

def test_summary(self):
"""Assert the summary function is implemented."""
body = {
'msg': {
'project': None,
'message': {
'agent': 'Bond',
'distro': 'Fedora',
},
'distro': {'name': 'Fedora'},
}
}
message = schema.DistroCreatedV1(body=body)
expected_str = 'A new distribution, Fedora, was added to Anitya.'

message.validate()
self.assertEqual(expected_str, message.summary())


class ProjectCreatedV1Tests(unittest.TestCase):
"""A set of unit tests to ensure the schema works as expected."""

def setUp(self):
self.body = {
'msg': {
'project': {
'backend': 'PyPI',
'created_on': 0,
'homepage': 'http://example.com',
'id': 1,
'name': 'arrow',
'regex': None,
'updated_on': 0,
'version': None,
'version_url': 'https://gitlab.example.com/arrow',
'versions': [],
},
'message': {
'agent': 'Bond',
'project': 'arrow',
},
'distro': None,
},
}

def test_message(self):
"""
Assert the message schema validates a message with the minimal number
of required fields.
"""
message = schema.ProjectCreatedV1(body=self.body)
message.validate()

def test_missing_field(self):
"""Assert missing the 'distro' key causes a validation error."""
del self.body['msg']['project']
message = schema.ProjectCreatedV1(body=self.body)
self.assertRaises(ValidationError, message.validate)

def test_str(self):
"""Assert __str__ produces a human-readable message."""
message = schema.ProjectCreatedV1(body=self.body)
expected_str = 'A new project, arrow, was added to Anitya.'

message.validate()
self.assertEqual(expected_str, str(message))

def test_summary(self):
"""Assert the summary function is implemented."""
message = schema.ProjectCreatedV1(body=self.body)
expected_str = 'A new project, arrow, was added to Anitya.'

message.validate()
self.assertEqual(expected_str, message.summary())
2 changes: 2 additions & 0 deletions message_schema/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bdist_wheel]
universal = 1
62 changes: 62 additions & 0 deletions message_schema/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python
#
# Copyright (C) 2018 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.
import os

from setuptools import setup, find_packages


here = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(here, 'README')) as fd:
README = fd.read()


setup(
name='anitya_schema',
version='1.0.0',
description='JSON schema definitions for messages published by Anitya',
long_description=README,
url='https://github.com/release-monitoring/anitya/',
# Possible options are at https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=[
'License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)',
'Operating System :: POSIX :: Linux',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],
license='GPLv2+',
maintainer='Fedora Infrastructure Team',
maintainer_email='infrastructure@lists.fedoraproject.org',
platforms=['Fedora', 'GNU/Linux'],
keywords='fedora',
packages=find_packages(
exclude=('anitya_schema.tests', 'anitya_schema.tests.*')),
include_package_data=True,
zip_safe=False,
install_requires=['fedora_messaging'],
test_suite='anitya_schema.tests',
entry_points={
'fedora.messages': [
'anitya_distro_createdv1=anitya_schema.schema:DistroCreatedV1',
'anitya_project_createdv1=anitya_schema.schema:ProjectCreatedV1',
]
}
)
Loading

0 comments on commit 832e387

Please sign in to comment.