mozreview: fix and customise the bug info panel for BMO (
bug 1241397); r=mcote
This is mostly a clone of reviewboard/hostingsvcs/bugzilla.py with changes that
fixes the display of strings as raw unicode python, display the resolution for
resolved bugs, and optimise slightly the requests to BMO.
MozReview-Commit-ID: JdGipVfEea0
--- a/pylib/mozreview/mozreview/extension.py
+++ b/pylib/mozreview/mozreview/extension.py
@@ -52,16 +52,19 @@ from mozreview.fields import (
TryField,
)
from mozreview.file_diff_reviewer.resources import (
file_diff_reviewer_resource,
)
from mozreview.hooks import (
MozReviewApprovalHook,
)
+from mozreview.hostingservice.bmo_bugtracker import (
+ BMOBugTracker,
+)
from mozreview.hostingservice.hmo_repository import (
HMORepository,
)
from mozreview.ldap.resources import (
ldap_association_resource,
)
from mozreview.middleware import (
MozReviewCacheDisableMiddleware,
@@ -303,16 +306,17 @@ class MozReviewExtension(Extension):
# Use a custom method to calculate a review approval state.
MozReviewApprovalHook(self)
# Instantiate the various signal handlers
initialize_signal_handlers(self)
HostingServiceHook(self, HMORepository)
+ HostingServiceHook(self, BMOBugTracker)
def shutdown(self):
# Restore the built-in opcode generator.
set_diff_opcode_generator_class(self.original_opcode_generator)
# We have to put the TestingDone field back before we shut down
# in order to get the instance back to its original state.
main_fieldset = get_review_request_fieldset('main')
new file mode 100644
--- /dev/null
+++ b/pylib/mozreview/mozreview/hostingservice/bmo_bugtracker.py
@@ -0,0 +1,67 @@
+from __future__ import unicode_literals
+
+import logging
+
+from django import forms
+from django.utils import six
+from django.utils.translation import ugettext_lazy as _
+
+from reviewboard.admin.validation import validate_bug_tracker_base_hosting_url
+from reviewboard.hostingsvcs.bugtracker import BugTracker
+from reviewboard.hostingsvcs.forms import HostingServiceForm
+from reviewboard.hostingsvcs.service import HostingService
+
+
+class BMOForm(HostingServiceForm):
+ bmo_url = forms.CharField(
+ label=_('Bugzilla URL'),
+ max_length=64,
+ required=True,
+ widget=forms.TextInput(attrs={'size': '60'}),
+ validators=[validate_bug_tracker_base_hosting_url])
+
+ def clean_bmo_url(self):
+ return self.cleaned_data['bmo_url'].rstrip('/')
+
+
+class BMOBugTracker(HostingService, BugTracker):
+ name = 'BMO'
+ form = BMOForm
+ bug_tracker_field = '%(bmo_url)s/show_bug.cgi?id=%%s'
+ supports_bug_trackers = True
+
+ def get_bug_info_uncached(self, repository, bug_id):
+ """Get the bug info from the server."""
+ bug_id = six.text_type(bug_id)
+
+ result = {
+ 'summary': '',
+ 'description': '',
+ 'status': '',
+ }
+
+ try:
+ # TODO investigate if it makes more sense to display BMO's
+ # user-story field, if set, instead of the description.
+ url = '%s/rest/bug/%s?include_fields=summary,status,resolution' % (
+ repository.extra_data['bug_tracker-bmo_url'], bug_id)
+ rsp, headers = self.client.json_get(url)
+ result['summary'] = str(rsp['bugs'][0]['summary'])
+ result['status'] = str(rsp['bugs'][0]['status'])
+ if result['status'] == 'RESOLVED':
+ result['status'] += ' ' + rsp['bugs'][0]['resolution']
+ except Exception as e:
+ logging.warning('Unable to fetch BMO data from %s: %s',
+ url, e, exc_info=1)
+
+ try:
+ url = '%s/rest/bug/%s/comment?include_fields=text' % (
+ repository.extra_data['bug_tracker-bmo_url'], bug_id)
+ rsp, headers = self.client.json_get(url)
+ result['description'] = str(rsp['bugs'][bug_id]['comments'][0][
+ 'text'])
+ except Exception as e:
+ logging.warning('Unable to fetch BMO data from %s: %s',
+ url, e, exc_info=1)
+
+ return result