diff --git a/module_build_service/messaging.py b/module_build_service/messaging.py index fad73253..86a98c4c 100644 --- a/module_build_service/messaging.py +++ b/module_build_service/messaging.py @@ -32,6 +32,7 @@ try: except ImportError: from funcsigs import signature +from rpmUtils.miscutils import splitFilename from module_build_service import log @@ -160,7 +161,7 @@ class BaseMessage(object): regex_pattern = re.compile( (r'(?P' + categories_re + r')(?:\.)' r'(?Pbuild|repo|module)(?:(?:\.)' - r'(?Pstate))?(?:\.)(?Pchange|done)$')) + r'(?Pstate|build))?(?:\.)(?Pchange|done|end)$')) regex_results = re.search(regex_pattern, topic) if regex_results: @@ -214,6 +215,13 @@ class BaseMessage(object): msg_obj = MBSModule( msg_id, msg_inner_msg.get('id'), msg_inner_msg.get('state')) + elif category == 'copr' and object == 'build': + build = msg_inner_msg.get('build') + status = msg_inner_msg.get('status') + pkg = msg_inner_msg.get('pkg') + what = msg_inner_msg.get('what') + msg_obj = CoprBuildEnd(msg_id, build, status, pkg, what) + # If the message matched the regex and is important to the app, # it will be returned if msg_obj: @@ -263,6 +271,31 @@ class KojiRepoChange(BaseMessage): self.repo_tag = repo_tag +class CoprBuildEnd(object): + """ A wrapper class that transforms copr message attributes + to a KojiBuildChange message object + :param msg_id: the id of the msg (e.g. 2016-SomeGUID) + :param build_id: the id of the build (e.g. 264382) + :param status: the new build state + (see http://copr-backend.readthedocs.io/package/constants.html#backend.constants.BuildStatus ) + :param pkg: the full name of what is being built + (e.g. mutt-kz-1.5.23.1-1.20150203.git.c8504a8a.fc21) + :param state_reason: the optional reason as to why the state changed + """ + def __new__(cls, msg_id, build_id, status, pkg, what=None): + name, version, release, epoch, arch = splitFilename(pkg) + return KojiBuildChange( + msg_id=msg_id, + build_id=build_id, + task_id=build_id, + build_new_state=status, + build_name=name, + build_version=version, + build_release=".".join(s for s in [release, epoch, arch] if s), + state_reason=what, + ) + + class MBSModule(BaseMessage): """ A class that inherits from BaseMessage to provide a message object for a module event generated by module_build_service @@ -377,7 +410,7 @@ def _amq_publish(topic, msg, conf, service): _messaging_backends = { 'fedmsg': { 'publish': _fedmsg_publish, - 'services': ['buildsys', 'mbs'] + 'services': ['buildsys', 'mbs', 'copr'] }, 'amq': { 'publish': _amq_publish, diff --git a/tests/test_messaging.py b/tests/test_messaging.py index 1a959173..c02a9962 100644 --- a/tests/test_messaging.py +++ b/tests/test_messaging.py @@ -52,3 +52,38 @@ class TestFedmsgMessaging(unittest.TestCase): self.assertEqual(msg.build_id, 614503) self.assertEqual(msg.build_new_state, 1) + + def test_copr_build_end(self): + # http://fedora-fedmsg.readthedocs.io/en/latest/topics.html#copr-build-end + copr_build_end_msg = { + 'msg': { + 'build': 100, + 'chroot': 'fedora-20-x86_64', + 'copr': 'mutt-kz', + 'ip': '172.16.3.3', + 'pid': 12010, + 'pkg': 'mutt-kz-1.5.23.1-1.20150203.git.c8504a8a.fc21', + 'status': 1, + 'user': 'fatka', + 'version': '1.5.23.1-1.20150203.git.c8504a8a.fc21', + 'what': 'build end: user:fatka copr:mutt-kz build:100 ip:172.16.3.3 pid:12010 status:1', + 'who': 'worker-2' + }, + 'msg_id': '2013-b05a323d-37ee-4396-9635-7b5dfaf5441b', + 'timestamp': 1383956707.634, + 'topic': 'org.fedoraproject.prod.copr.build.end', + 'username': 'copr' + } + + topic = 'org.fedoraproject.prod.copr.build.end' + msg = messaging.BaseMessage.from_fedmsg(topic, copr_build_end_msg) + self.assertIsInstance(msg, messaging.KojiBuildChange) + self.assertEqual(msg.msg_id, '2013-b05a323d-37ee-4396-9635-7b5dfaf5441b') + self.assertEqual(msg.build_id, 100) + self.assertEqual(msg.task_id, 100) + self.assertEqual(msg.build_new_state, 1) + self.assertEqual(msg.build_name, 'mutt-kz') + self.assertEqual(msg.build_version, '1.5.23.1') + self.assertEqual(msg.build_release, '1.20150203.git.c8504a8a.fc21') + self.assertEqual(msg.state_reason, + 'build end: user:fatka copr:mutt-kz build:100 ip:172.16.3.3 pid:12010 status:1')