~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/bugs/externalbugtracker/bugzilla.py

Merge from trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
375
375
class BugzillaAPI(Bugzilla):
376
376
    """An `ExternalBugTracker` to handle Bugzillas that offer an API."""
377
377
 
 
378
    implements(ISupportsCommentImport)
 
379
 
378
380
    def __init__(self, baseurl, xmlrpc_transport=None,
379
381
                 internal_xmlrpc_transport=None):
380
382
        super(BugzillaAPI, self).__init__(baseurl)
590
592
 
591
593
        return bug_products
592
594
 
 
595
    def getCommentIds(self, bug_watch):
 
596
        """See `ISupportsCommentImport`."""
 
597
        actual_bug_id = self._getActualBugId(bug_watch.remotebug)
 
598
 
 
599
        # Check that the bug exists, first.
 
600
        if actual_bug_id not in self._bugs:
 
601
            raise BugNotFound(bug_watch.remotebug)
 
602
 
 
603
        # Get only the remote comment IDs and store them in the
 
604
        # 'comments' field of the bug.
 
605
        bug_comments_dict = self.xmlrpc_proxy.Bug.comments({
 
606
            'ids': [actual_bug_id],
 
607
            'include_fields': ['id'],
 
608
            })
 
609
 
 
610
        # We need to convert bug and comment ids to strings (see bugs
 
611
        # 248662 amd 248938).
 
612
        bug_comments = bug_comments_dict['bugs'][str(actual_bug_id)]
 
613
        return [str(comment['id']) for comment in bug_comments]
 
614
 
 
615
    def fetchComments(self, bug_watch, comment_ids):
 
616
        """See `ISupportsCommentImport`."""
 
617
        actual_bug_id = self._getActualBugId(bug_watch.remotebug)
 
618
 
 
619
        # We need to cast comment_ids to integers, since
 
620
        # BugWatchUpdater.importBugComments() will pass us a list of
 
621
        # strings (see bug 248938).
 
622
        comment_ids = [int(comment_id) for comment_id in comment_ids]
 
623
 
 
624
        # Fetch the comments we want.
 
625
        return_dict = self.xmlrpc_proxy.Bug.comments({
 
626
            'comment_ids': comment_ids,
 
627
            })
 
628
        comments = return_dict['comments']
 
629
 
 
630
        # As a sanity check, drop any comments that don't belong to the
 
631
        # bug in bug_watch.
 
632
        for comment_id, comment in comments.items():
 
633
            if int(comment['bug_id']) != actual_bug_id:
 
634
                del comments[comment_id]
 
635
 
 
636
        self._bugs[actual_bug_id]['comments'] = return_dict['comments']
 
637
 
 
638
    def getPosterForComment(self, bug_watch, comment_id):
 
639
        """See `ISupportsCommentImport`."""
 
640
        actual_bug_id = self._getActualBugId(bug_watch.remotebug)
 
641
 
 
642
        # We need to cast comment_id to integers, since
 
643
        # BugWatchUpdater.importBugComments() will pass us a string (see
 
644
        # bug 248938).
 
645
        comment_id = int(comment_id)
 
646
 
 
647
        comment = self._bugs[actual_bug_id]['comments'][comment_id]
 
648
        display_name, email = parseaddr(comment['author'])
 
649
 
 
650
        # If the name is empty then we return None so that
 
651
        # IPersonSet.ensurePerson() can actually do something with it.
 
652
        if not display_name:
 
653
            display_name = None
 
654
 
 
655
        return (display_name, email)
 
656
 
 
657
    def getMessageForComment(self, bug_watch, comment_id, poster):
 
658
        """See `ISupportsCommentImport`."""
 
659
        actual_bug_id = self._getActualBugId(bug_watch.remotebug)
 
660
 
 
661
        # We need to cast comment_id to integers, since
 
662
        # BugWatchUpdater.importBugComments() will pass us a string (see
 
663
        # bug 248938).
 
664
        comment_id = int(comment_id)
 
665
        comment = self._bugs[actual_bug_id]['comments'][comment_id]
 
666
 
 
667
        # Turn the time in the comment, which is an XML-RPC datetime
 
668
        # into something more useful to us.
 
669
        # XXX 2008-08-05 gmb (bug 254999):
 
670
        #     We can remove these lines once we upgrade to python 2.5.
 
671
        comment_timestamp = time.mktime(
 
672
            time.strptime(str(comment['time']), '%Y%m%dT%H:%M:%S'))
 
673
        comment_datetime = datetime.fromtimestamp(comment_timestamp)
 
674
        comment_datetime = comment_datetime.replace(
 
675
            tzinfo=pytz.timezone('UTC'))
 
676
 
 
677
        message = getUtility(IMessageSet).fromText(
 
678
            owner=poster, subject='', content=comment['text'],
 
679
            datecreated=comment_datetime)
 
680
 
 
681
        return message
 
682
 
593
683
 
594
684
class BugzillaLPPlugin(BugzillaAPI):
595
685
    """An `ExternalBugTracker` to handle Bugzillas using the LP Plugin."""
744
834
 
745
835
        self._bugs[actual_bug_id]['comments'] = bug_comments
746
836
 
747
 
    def getPosterForComment(self, bug_watch, comment_id):
748
 
        """See `ISupportsCommentImport`."""
749
 
        actual_bug_id = self._getActualBugId(bug_watch.remotebug)
750
 
 
751
 
        # We need to cast comment_id to integers, since
752
 
        # BugWatchUpdater.importBugComments() will pass us a string (see
753
 
        # bug 248938).
754
 
        comment_id = int(comment_id)
755
 
 
756
 
        comment = self._bugs[actual_bug_id]['comments'][comment_id]
757
 
        display_name, email = parseaddr(comment['author'])
758
 
 
759
 
        # If the name is empty then we return None so that
760
 
        # IPersonSet.ensurePerson() can actually do something with it.
761
 
        if not display_name:
762
 
            display_name = None
763
 
 
764
 
        return (display_name, email)
765
 
 
766
 
    def getMessageForComment(self, bug_watch, comment_id, poster):
767
 
        """See `ISupportsCommentImport`."""
768
 
        actual_bug_id = self._getActualBugId(bug_watch.remotebug)
769
 
 
770
 
        # We need to cast comment_id to integers, since
771
 
        # BugWatchUpdater.importBugComments() will pass us a string (see
772
 
        # bug 248938).
773
 
        comment_id = int(comment_id)
774
 
        comment = self._bugs[actual_bug_id]['comments'][comment_id]
775
 
 
776
 
        # Turn the time in the comment, which is an XML-RPC datetime
777
 
        # into something more useful to us.
778
 
        # XXX 2008-08-05 gmb (bug 254999):
779
 
        #     We can remove these lines once we upgrade to python 2.5.
780
 
        comment_timestamp = time.mktime(
781
 
            time.strptime(str(comment['time']), '%Y%m%dT%H:%M:%S'))
782
 
        comment_datetime = datetime.fromtimestamp(comment_timestamp)
783
 
        comment_datetime = comment_datetime.replace(
784
 
            tzinfo=pytz.timezone('UTC'))
785
 
 
786
 
        message = getUtility(IMessageSet).fromText(
787
 
            owner=poster, subject='', content=comment['text'],
788
 
            datecreated=comment_datetime)
789
 
 
790
 
        return message
791
 
 
792
837
    @needs_authentication
793
838
    def addRemoteComment(self, remote_bug, comment_body, rfc822msgid):
794
839
        """Add a comment to the remote bugtracker.