Skip to content

Commit

Permalink
Merge pull request #72 from sarah256/bugzilla
Browse files Browse the repository at this point in the history
Create a handler for Bugzilla bug messages
  • Loading branch information
sarah256 committed Jul 26, 2019
2 parents 162b376 + 77de985 commit 698dfae
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 1 deletion.
3 changes: 2 additions & 1 deletion estuary_updater/handlers/__init__.py
Expand Up @@ -6,7 +6,8 @@
from estuary_updater.handlers.distgit import DistGitHandler
from estuary_updater.handlers.errata import ErrataHandler
from estuary_updater.handlers.koji import KojiHandler
from estuary_updater.handlers.bugzilla import BugzillaHandler


# All handler classes are added here
all_handlers = [DistGitHandler, FreshmakerHandler, ErrataHandler, KojiHandler]
all_handlers = [DistGitHandler, FreshmakerHandler, ErrataHandler, KojiHandler, BugzillaHandler]
79 changes: 79 additions & 0 deletions estuary_updater/handlers/bugzilla.py
@@ -0,0 +1,79 @@
# SPDX-License-Identifier: GPL-3.0+

from __future__ import unicode_literals, absolute_import

from estuary.utils.general import timestamp_to_datetime
from estuary.models.bugzilla import BugzillaBug
from estuary.models.user import User

from estuary_updater.handlers.base import BaseHandler


class BugzillaHandler(BaseHandler):
"""A handler for Bugzilla bug related messages."""

@staticmethod
def can_handle(msg):
"""
Determine if this handler can handle this message.
:param dict msg: a message to be analyzed
:return: a bool based on if the handler can handle this kind of message
:rtype: bool
"""
supported_topics = [
'/topic/VirtualTopic.eng.bugzilla.bug.modify',
'/topic/VirtualTopic.eng.bugzilla.bug.create',
]
return msg['topic'] in supported_topics

def handle(self, msg):
"""
Handle a Bugzilla bug message and update Neo4j if necessary.
:param dict msg: a message to be processed
"""
if self.can_handle(msg):
self.bug_handler(msg)
else:
raise RuntimeError('This message is unable to be handled: {0}'.format(msg))

def bug_handler(self, msg):
"""
Handle a modified or created Bugzilla bug and update Neo4j if necessary.
:param dict msg: a message to be processed
"""
bug_data = msg['body']['msg']['bug']
bug_params = {
'id_': str(bug_data['id']),
'classification': bug_data['classification'],
'creation_time': timestamp_to_datetime(bug_data['creation_time']),
'modified_time': timestamp_to_datetime(bug_data['last_change_time']),
'priority': bug_data['priority'],
'product_name': bug_data['product']['name'],
'product_version': bug_data['version']['name'],
'resolution': bug_data['resolution'],
'severity': bug_data['severity'],
'short_description': bug_data['summary'],
'status': bug_data['status']['name'],
'target_milestone': bug_data['target_milestone']['name'],
}
assignee = User.create_or_update({
'username': bug_data['assigned_to']['login'].split('@')[0],
'email': bug_data['assigned_to']['login']
})[0]
qa_contact = User.create_or_update({
'username': bug_data['qa_contact']['login'].split('@')[0],
'email': bug_data['qa_contact']['login']
})[0]
reporter = User.create_or_update({
'username': bug_data['reporter']['login'].split('@')[0],
'email': bug_data['reporter']['login']
})[0]

bug = BugzillaBug.create_or_update(bug_params)[0]

bug.conditional_connect(bug.assignee, assignee)
bug.conditional_connect(bug.qa_contact, qa_contact)
bug.conditional_connect(bug.reporter, reporter)
48 changes: 48 additions & 0 deletions tests/handlers/test_bugzilla.py
@@ -0,0 +1,48 @@
# SPDX-License-Identifier: GPL-3.0+

from __future__ import unicode_literals, absolute_import

import json
from os import path
from datetime import datetime

import pytz

from tests import message_dir
from estuary.models.bugzilla import BugzillaBug
from estuary_updater import config
from estuary.models.user import User
from estuary_updater.handlers.bugzilla import BugzillaHandler


def test_bugzilla_bug():
"""Test the Bugzilla handler when it receives a new bug message."""
with open(path.join(message_dir, 'bugzilla', 'bug_created.json'), 'r') as f:
msg = json.load(f)

assert BugzillaHandler.can_handle(msg) is True
handler = BugzillaHandler(config)
handler.handle(msg)

bug = BugzillaBug.nodes.get_or_none(id_='1732519')
assert bug is not None
assert bug.id_ == '1732519'
assert bug.classification == 'Red Hat'
assert bug.creation_time == datetime(2019, 7, 23, 14, 36, 18, tzinfo=pytz.utc)
assert bug.modified_time == datetime(2019, 7, 23, 14, 36, 18, tzinfo=pytz.utc)
assert bug.priority == 'unspecified'
assert bug.product_name == 'Red Hat Satellite 6'
assert bug.product_version == '6.5.0'
assert bug.resolution == ''
assert bug.severity == 'low'
assert bug.short_description == 'Adding Manifest file: Menus don\'t match documentation'
assert bug.status == 'NEW'
assert bug.target_milestone == 'Unspecified'

assignee = User.nodes.get_or_none(username='satellite-doc-list')
qa_contact = User.nodes.get_or_none(username='satellite-doc-list')
reporter = User.nodes.get_or_none(username='sarah')

assert bug.assignee.is_connected(assignee)
assert bug.qa_contact.is_connected(qa_contact)
assert bug.reporter.is_connected(reporter)
132 changes: 132 additions & 0 deletions tests/messages/bugzilla/bug_created.json
@@ -0,0 +1,132 @@
{
"body":{
"i": 0,
"msg_id": "ID:messaging.redhat.com",
"topic": "/topic/VirtualTopic.eng.bugzilla.bug.create",
"timestamp": 1563892582.0,
"certificate": null,
"signature": null,
"username": null,
"crypto": null,
"msg": {
"bug": {
"alias": [],
"assigned_to": {
"id": 389671,
"login": "satellite-doc-list@redhat.com",
"real_name": ""
},
"cf_atomic": "",
"cf_category": "",
"cf_clone_of": "",
"cf_cloudforms_team": "",
"cf_crm": "",
"cf_doc_type": "",
"cf_documentation_action": "",
"cf_environment": "",
"cf_fixed_in": "",
"cf_last_closed": null,
"cf_mount_type": "",
"cf_ovirt_team": "",
"cf_regression_status": "",
"cf_release_notes": "",
"cf_story_points": "",
"cf_type": "Bug",
"classification": "Red Hat",
"component": {
"id": 109115,
"name": "Documentation"
},
"creation_time": "2019-07-23T14:36:18",
"flags": [],
"id": 1732519,
"is_private": false,
"keywords": [],
"last_change_time": "2019-07-23T14:36:18",
"operating_system": "Unspecified",
"platform": "Unspecified",
"priority": "unspecified",
"product": {
"id": 254,
"name": "Red Hat Satellite 6"
},
"qa_contact": {
"id": 389671,
"login": "satellite-doc-list@redhat.com",
"real_name": ""
},
"reporter": {
"id": 437935,
"login": "sarah@redhat.com",
"real_name": "Sarah"
},
"resolution": "",
"severity": "low",
"status": {
"id": 1,
"name": "NEW"
},
"summary": "Adding Manifest file: Menus don't match documentation",
"target_milestone": {
"id": 786,
"name": "Unspecified"
},
"url": "",
"version": {
"id": 5645,
"name": "6.5.0"
},
"whiteboard": ""
},
"event": {
"action": "create",
"bug_id": 1732519,
"change_set": "70126.1563892578.37052",
"routing_key": "bug.create",
"target": "bug",
"time": "2019-07-23T14:36:18",
"user": {
"id": 437935,
"login": "sarah@redhat.com",
"real_name": "Sarah"
}
}
},
"headers": {
"JMSXUserID": "msg-client-bugzilla",
"amq6100_destination": "queue://Consumer.client-datanommer.upshift-prod.VirtualTopic.eng.>",
"amq6100_originalDestination": "topic://VirtualTopic.eng.bugzilla.bug.create",
"content-length": "1404",
"correlation-id": "74620fe5-4cac-45fd-8852-3d02ea8ee042",
"destination": "/topic/VirtualTopic.eng.bugzilla.bug.create",
"esbMessageType": "bugzillaNotification",
"esbSourceSystem": "bugzilla",
"expires": "1563978982760",
"message-id": "ID:messaging.redhat.com",
"original-destination": "/topic/VirtualTopic.eng.bugzilla.bug.create",
"priority": "4",
"subscription": "/queue/Consumer.client-datanommer.upshift-prod.VirtualTopic.eng.>",
"timestamp": "1563892582760"
},
"source_name": "datanommer",
"source_version": "0.9.1"
},
"topic": "/topic/VirtualTopic.eng.bugzilla.bug.create",
"headers": {
"JMSXUserID": "msg-client-bugzilla",
"amq6100_destination": "queue://Consumer.client-datanommer.upshift-prod.VirtualTopic.eng.>",
"amq6100_originalDestination": "topic://VirtualTopic.eng.bugzilla.bug.create",
"content-length": "1404",
"correlation-id": "74620fe5-4cac-45fd-8852-3d02ea8ee042",
"destination": "/topic/VirtualTopic.eng.bugzilla.bug.create",
"esbMessageType": "bugzillaNotification",
"esbSourceSystem": "bugzilla",
"expires": "1563978982760",
"message-id": "ID:messaging.redhat.com",
"original-destination": "/topic/VirtualTopic.eng.bugzilla.bug.create",
"priority": "4",
"subscription": "/queue/Consumer.client-datanommer.upshift-prod.VirtualTopic.eng.>",
"timestamp": "1563892582760"
}

}

0 comments on commit 698dfae

Please sign in to comment.