← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~wgrant/launchpad/bugbranch-hide into lp:launchpad

 

William Grant has proposed merging lp:~wgrant/launchpad/bugbranch-hide into lp:launchpad.

Commit message:
Preparatory cleanup for BugBranch XRefification.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~wgrant/launchpad/bugbranch-hide/+merge/285134

Preparatory cleanup for BugBranch XRefification.

It'll have to remain as a wrapper for API/UI compatibility, but good chunks of the related code can be fixed to no longer exist.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~wgrant/launchpad/bugbranch-hide into lp:launchpad.
=== modified file 'lib/lp/app/browser/tales.py'
--- lib/lp/app/browser/tales.py	2015-10-01 02:59:28 +0000
+++ lib/lp/app/browser/tales.py	2016-02-05 00:02:25 +0000
@@ -953,7 +953,7 @@
 
     def _hasBugBranch(self):
         """Return whether the bug has a branch linked to it."""
-        return not self._context.bug.linked_branches.is_empty()
+        return not self._context.bug.linked_bugbranches.is_empty()
 
     def _hasSpecification(self):
         """Return whether the bug is linked to a specification."""

=== modified file 'lib/lp/bugs/configure.zcml'
--- lib/lp/bugs/configure.zcml	2015-09-25 09:59:27 +0000
+++ lib/lp/bugs/configure.zcml	2016-02-05 00:02:25 +0000
@@ -77,18 +77,15 @@
         for="lp.bugs.interfaces.bugattachment.IBugAttachment                 lazr.lifecycle.interfaces.IObjectModifiedEvent"
         handler="lp.bugs.subscribers.buglastupdated.update_bug_date_last_updated"/>
     <subscriber
-        for="lp.bugs.interfaces.bugbranch.IBugBranch                 lazr.lifecycle.interfaces.IObjectCreatedEvent"
-        handler="lp.bugs.subscribers.buglastupdated.update_bug_date_last_updated"/>
-    <subscriber
-        for="lp.bugs.interfaces.bugbranch.IBugBranch                 lazr.lifecycle.interfaces.IObjectModifiedEvent"
-        handler="lp.bugs.subscribers.buglastupdated.update_bug_date_last_updated"/>
-    <subscriber
         for="lp.bugs.interfaces.bug.IBug lp.bugs.interfaces.buglink.IObjectLinkedEvent"
         handler="lp.bugs.subscribers.buglastupdated.update_bug_date_last_updated"/>
     <subscriber
         for="lp.bugs.interfaces.bug.IBug lp.bugs.interfaces.buglink.IObjectLinkedEvent"
         handler="lp.bugs.subscribers.karma.cve_added"/>
     <subscriber
+        for="lp.bugs.interfaces.bug.IBug lp.bugs.interfaces.buglink.IObjectLinkedEvent"
+        handler="lp.bugs.subscribers.karma.branch_linked"/>
+    <subscriber
         for="lp.bugs.interfaces.bugmessage.IBugMessage                 lazr.lifecycle.interfaces.IObjectCreatedEvent"
         handler="lp.bugs.subscribers.bug.notify_bug_comment_added"/>
     <subscriber
@@ -527,18 +524,8 @@
         class="lp.bugs.model.bugbranch.BugBranch">
         <allow
             interface="lp.bugs.interfaces.bugbranch.IBugBranch"/>
-        <require
-            permission="launchpad.Edit"
-            attributes="
-                destroySelf"/>
     </class>
 
-    <!-- hierarchy -->
-
-    <subscriber
-        for="lp.bugs.interfaces.bugbranch.IBugBranch                                                lazr.lifecycle.interfaces.IObjectCreatedEvent"
-        handler="lp.bugs.subscribers.karma.bug_branch_created"/>
-
     <!-- BugBranchSet -->
 
     <class
@@ -719,6 +706,7 @@
                 interface="lp.bugs.interfaces.bug.IBugView"
                 attributes="
                     linked_branches
+                    linked_bugbranches
                     getVisibleLinkedBranches"/>
             <require
                 permission="launchpad.Edit"

=== modified file 'lib/lp/bugs/interfaces/bug.py'
--- lib/lp/bugs/interfaces/bug.py	2015-09-30 01:51:52 +0000
+++ lib/lp/bugs/interfaces/bug.py	2016-02-05 00:02:25 +0000
@@ -399,9 +399,6 @@
             None to prevent lazy evaluation triggering database lookups.
         """
 
-    def hasBranch(branch):
-        """Is this branch linked to this bug?"""
-
     def isSubscribed(person):
         """Is person subscribed to this bug?
 
@@ -999,14 +996,15 @@
     """The core bug entry."""
     export_as_webservice_entry()
 
-    linked_branches = exported(
+    linked_bugbranches = exported(
         CollectionField(
             title=_("Branches associated with this bug, usually "
             "branches on which this bug is being fixed."),
             value_type=Reference(schema=IBugBranch),
-            readonly=True))
+            readonly=True),
+        exported_as='linked_branches')
 
-    @accessor_for(linked_branches)
+    @accessor_for(linked_bugbranches)
     @call_with(user=REQUEST_USER)
     @export_read_operation()
     @operation_for_version('beta')

=== modified file 'lib/lp/bugs/interfaces/bugbranch.py'
--- lib/lp/bugs/interfaces/bugbranch.py	2013-01-07 02:40:55 +0000
+++ lib/lp/bugs/interfaces/bugbranch.py	2016-02-05 00:02:25 +0000
@@ -16,46 +16,29 @@
     )
 from lazr.restful.fields import ReferenceChoice
 from zope.interface import Interface
-from zope.schema import (
-    Int,
-    Object,
-    TextLine,
-    )
+from zope.schema import Object
 
 from lp import _
 from lp.app.interfaces.launchpad import IHasDateCreated
-from lp.bugs.interfaces.bugtask import IBugTask
 from lp.bugs.interfaces.hasbug import IHasBug
 from lp.code.interfaces.branch import IBranch
-from lp.code.interfaces.branchtarget import IHasBranchTarget
 from lp.registry.interfaces.person import IPerson
 from lp.services.fields import BugField
 
 
-class IBugBranch(IHasDateCreated, IHasBug, IHasBranchTarget):
+class IBugBranch(IHasDateCreated, IHasBug):
     """A branch linked to a bug."""
 
     export_as_webservice_entry()
 
-    id = Int(title=_("Bug Branch #"))
     bug = exported(
         BugField(
             title=_("Bug #"),
             required=True, readonly=True))
-    branch_id = Int(title=_("Branch ID"), required=True, readonly=True)
     branch = exported(
         ReferenceChoice(
             title=_("Branch"), schema=IBranch,
             vocabulary="Branch", required=True))
-    revision_hint = TextLine(title=_("Revision Hint"))
-
-    bug_task = Object(
-        schema=IBugTask, title=_("The bug task that the branch fixes"),
-        description=_(
-            "the bug task reported against this branch's product or the "
-            "first bug task (in case where there is no task reported "
-            "against the branch's product)."),
-        readonly=True)
 
     registrant = Object(
         schema=IPerson, readonly=True, required=True,
@@ -64,12 +47,6 @@
 
 class IBugBranchSet(Interface):
 
-    def getBugBranch(bug, branch):
-        """Return the BugBranch for the given bug and branch.
-
-        Return None if there is no such link.
-        """
-
     def getBranchesWithVisibleBugs(branches, user):
         """Find which of `branches` are for bugs that `user` can see.
 
@@ -79,7 +56,3 @@
             found in `branches`, but limited to branches that are
             visible to `user`.
         """
-
-    def getBugBranchesForBugTasks(tasks):
-        """Return a sequence of IBugBranch instances associated with
-        the bugs for the given tasks."""

=== modified file 'lib/lp/bugs/model/bug.py'
--- lib/lp/bugs/model/bug.py	2016-01-26 15:47:37 +0000
+++ lib/lp/bugs/model/bug.py	2016-02-05 00:02:25 +0000
@@ -28,7 +28,6 @@
 
 from lazr.lifecycle.event import (
     ObjectCreatedEvent,
-    ObjectDeletedEvent,
     ObjectModifiedEvent,
     )
 from lazr.lifecycle.snapshot import Snapshot
@@ -148,6 +147,10 @@
 from lp.bugs.model.bugactivity import BugActivity
 from lp.bugs.model.bugattachment import BugAttachment
 from lp.bugs.model.bugbranch import BugBranch
+from lp.bugs.model.buglinktarget import (
+    ObjectLinkedEvent,
+    ObjectUnlinkedEvent,
+    )
 from lp.bugs.model.bugmessage import BugMessage
 from lp.bugs.model.bugnomination import BugNomination
 from lp.bugs.model.bugnotification import BugNotification
@@ -361,7 +364,7 @@
     watches = SQLMultipleJoin(
         'BugWatch', joinColumn='bug', orderBy=['bugtracker', 'remotebug'])
     duplicates = SQLMultipleJoin('Bug', joinColumn='duplicateof', orderBy='id')
-    linked_branches = SQLMultipleJoin(
+    linked_bugbranches = SQLMultipleJoin(
         'BugBranch', joinColumn='bug', orderBy='id')
     date_last_message = UtcDateTimeCol(default=None)
     number_of_duplicates = IntCol(notNull=True, default=0)
@@ -373,6 +376,10 @@
     latest_patch_uploaded = UtcDateTimeCol(default=None)
 
     @property
+    def linked_branches(self):
+        return [link.branch for link in self.linked_bugbranches]
+
+    @property
     def cves(self):
         from lp.bugs.model.cve import Cve
         xref_cve_sequences = [
@@ -1351,32 +1358,26 @@
             title=title, message=message,
             send_notifications=send_notifications)
 
-    def hasBranch(self, branch):
-        """See `IBug`."""
-        return BugBranch.selectOneBy(branch=branch, bug=self) is not None
-
     def linkBranch(self, branch, registrant):
         """See `IBug`."""
-        for bug_branch in shortlist(self.linked_branches):
-            if bug_branch.branch == branch:
-                return bug_branch
+        if branch in self.linked_branches:
+            return
 
-        bug_branch = BugBranch(
-            branch=branch, bug=self, registrant=registrant)
+        BugBranch(branch=branch, bug=self, registrant=registrant)
         branch.date_last_modified = UTC_NOW
 
         self.addChange(BranchLinkedToBug(UTC_NOW, registrant, branch, self))
-        notify(ObjectCreatedEvent(bug_branch))
-
-        return bug_branch
+        notify(ObjectLinkedEvent(branch, self, user=registrant))
+        notify(ObjectLinkedEvent(self, branch, user=registrant))
 
     def unlinkBranch(self, branch, user):
         """See `IBug`."""
         bug_branch = BugBranch.selectOneBy(bug=self, branch=branch)
         if bug_branch is not None:
             self.addChange(BranchUnlinkedFromBug(UTC_NOW, user, branch, self))
-            notify(ObjectDeletedEvent(bug_branch, user=user))
-            bug_branch.destroySelf()
+            notify(ObjectUnlinkedEvent(branch, self, user=user))
+            notify(ObjectUnlinkedEvent(self, branch, user=user))
+            Store.of(bug_branch).remove(bug_branch)
 
     def getVisibleLinkedBranches(self, user, eager_load=False):
         """Return all the branches linked to the bug that `user` can see."""

=== modified file 'lib/lp/bugs/model/bugbranch.py'
--- lib/lp/bugs/model/bugbranch.py	2015-07-08 16:05:11 +0000
+++ lib/lp/bugs/model/bugbranch.py	2016-02-05 00:02:25 +0000
@@ -10,9 +10,7 @@
 
 from sqlobject import (
     ForeignKey,
-    IN,
     IntCol,
-    StringCol,
     )
 from zope.interface import implementer
 
@@ -20,7 +18,6 @@
     IBugBranch,
     IBugBranchSet,
     )
-from lp.code.interfaces.branchtarget import IHasBranchTarget
 from lp.registry.interfaces.person import validate_public_person
 from lp.services.database.constants import UTC_NOW
 from lp.services.database.datetimecol import UtcDateTimeCol
@@ -28,7 +25,7 @@
 from lp.services.database.sqlbase import SQLBase
 
 
-@implementer(IBugBranch, IHasBranchTarget)
+@implementer(IBugBranch)
 class BugBranch(SQLBase):
     """See `IBugBranch`."""
 
@@ -36,34 +33,15 @@
     bug = ForeignKey(dbName="bug", foreignKey="Bug", notNull=True)
     branch_id = IntCol(dbName="branch", notNull=True)
     branch = ForeignKey(dbName="branch", foreignKey="Branch", notNull=True)
-    revision_hint = StringCol(default=None)
 
     registrant = ForeignKey(
         dbName='registrant', foreignKey='Person',
         storm_validator=validate_public_person, notNull=True)
 
-    @property
-    def target(self):
-        """See `IHasBranchTarget`."""
-        return self.branch.target
-
-    @property
-    def bug_task(self):
-        """See `IBugBranch`."""
-        task = self.bug.getBugTask(self.branch.product)
-        if task is None:
-            # Just choose the first task for the bug.
-            task = self.bug.bugtasks[0]
-        return task
-
 
 @implementer(IBugBranchSet)
 class BugBranchSet:
 
-    def getBugBranch(self, bug, branch):
-        """See `IBugBranchSet`."""
-        return BugBranch.selectOneBy(bugID=bug.id, branchID=branch.id)
-
     def getBranchesWithVisibleBugs(self, branches, user):
         """See `IBugBranchSet`."""
         # Avoid circular imports.
@@ -80,13 +58,3 @@
             BugBranch.branch_id.is_in(branch_ids),
             BugTaskFlat.bug_id == BugBranch.bugID,
             visible).config(distinct=True)
-
-    def getBugBranchesForBugTasks(self, tasks):
-        """See `IBugBranchSet`."""
-        bug_ids = [task.bugID for task in tasks]
-        if not bug_ids:
-            return []
-        bugbranches = BugBranch.select(IN(BugBranch.q.bugID, bug_ids),
-                                       orderBy=['branch'])
-        return bugbranches.prejoin(
-            ['branch', 'branch.owner', 'branch.product'])

=== modified file 'lib/lp/bugs/security.py'
--- lib/lp/bugs/security.py	2016-01-26 15:47:37 +0000
+++ lib/lp/bugs/security.py	2016-02-05 00:02:25 +0000
@@ -13,7 +13,6 @@
     )
 from lp.bugs.interfaces.bug import IBug
 from lp.bugs.interfaces.bugattachment import IBugAttachment
-from lp.bugs.interfaces.bugbranch import IBugBranch
 from lp.bugs.interfaces.bugnomination import IBugNomination
 from lp.bugs.interfaces.bugsubscription import IBugSubscription
 from lp.bugs.interfaces.bugsubscriptionfilter import IBugSubscriptionFilter
@@ -131,16 +130,6 @@
         return not self.obj.private
 
 
-class EditBugBranch(EditPublicByLoggedInUserAndPrivateByExplicitSubscribers):
-    permission = 'launchpad.Edit'
-    usedfor = IBugBranch
-
-    def __init__(self, bug_branch):
-        # The same permissions as for the BugBranch's bug should apply
-        # to the BugBranch itself.
-        super(EditBugBranch, self).__init__(bug_branch.bug)
-
-
 class ViewBugAttachment(DelegatedAuthorization):
     """Security adapter for viewing a bug attachment.
 

=== modified file 'lib/lp/bugs/subscribers/karma.py'
--- lib/lp/bugs/subscribers/karma.py	2015-09-25 08:50:34 +0000
+++ lib/lp/bugs/subscribers/karma.py	2016-02-05 00:02:25 +0000
@@ -121,7 +121,10 @@
 
 
 @block_implicit_flushes
-def bug_branch_created(bug_branch, event):
+def branch_linked(bug, event):
     """Assign karma to the user who linked the bug to the branch."""
-    bug_branch.branch.target.assignKarma(
-        bug_branch.registrant, 'bugbranchcreated')
+    from lp.code.interfaces.branch import IBranch
+    if not IBranch.providedBy(event.other_object):
+        return
+    event.other_object.target.assignKarma(
+        IPerson(event.user), 'bugbranchcreated')

=== modified file 'lib/lp/bugs/templates/bug-branch.pt'
--- lib/lp/bugs/templates/bug-branch.pt	2011-02-03 05:14:54 +0000
+++ lib/lp/bugs/templates/bug-branch.pt	2016-02-05 00:02:25 +0000
@@ -8,8 +8,7 @@
                   branch bug_branch/branch;
                   bug bug_branch/bug;
                   show_edit bug_branch/required:launchpad.Edit"
-      tal:condition="branch/required:launchpad.View"
-      tal:attributes="id string:bug-branch-${bug_branch/id}">
+      tal:condition="branch/required:launchpad.View">
 
   <tal:branch-ref replace="structure branch/fmt:link"/>
   <tal:branch-status condition="view/show_branch_status">
@@ -20,9 +19,7 @@
   <a title="Remove link"
      class="bugbranch-delete"
      tal:condition="show_edit"
-     tal:attributes="
-       href string:${branch/fmt:url}/+bug/${bug/id}/+delete;
-       id string:bugbranch-${bug_branch/id}-delete;">
+     tal:attributes="href string:${branch/fmt:url}/+bug/${bug/id}/+delete">
     <img src="/@@/remove" alt="Remove"/>
   </a>
   <div tal:repeat="proposal view/merge_proposals" class="reviews">

=== modified file 'lib/lp/bugs/tests/test_bugbranch.py'
--- lib/lp/bugs/tests/test_bugbranch.py	2012-09-18 18:36:09 +0000
+++ lib/lp/bugs/tests/test_bugbranch.py	2016-02-05 00:02:25 +0000
@@ -143,20 +143,6 @@
         self.assertContentEqual(
             [branch.id], utility.getBranchesWithVisibleBugs([branch], admin))
 
-    def test_getBugBranchesForBugTasks(self):
-        # IBugBranchSet.getBugBranchesForBugTasks returns all of the BugBranch
-        # objects associated with the given bug tasks.
-        bug_a = self.factory.makeBug()
-        bug_b = self.factory.makeBug()
-        bugtasks = bug_a.bugtasks + bug_b.bugtasks
-        branch = self.factory.makeBranch()
-        self.factory.loginAsAnyone()
-        link_1 = bug_a.linkBranch(branch, self.factory.makePerson())
-        link_2 = bug_b.linkBranch(branch, self.factory.makePerson())
-        found_links = getUtility(IBugBranchSet).getBugBranchesForBugTasks(
-            bugtasks)
-        self.assertEqual(set([link_1, link_2]), set(found_links))
-
 
 class TestBugBranch(TestCaseWithFactory):
 
@@ -174,16 +160,6 @@
             registrant=self.factory.makePerson())
         self.assertProvides(bug_branch, IBugBranch)
 
-    def test_linkBranch_returns_IBugBranch(self):
-        # Bug.linkBranch returns an IBugBranch linking the bug to the branch.
-        bug = self.factory.makeBug()
-        branch = self.factory.makeBranch()
-        registrant = self.factory.makePerson()
-        bug_branch = bug.linkBranch(branch, registrant)
-        self.assertEqual(branch, bug_branch.branch)
-        self.assertEqual(bug, bug_branch.bug)
-        self.assertEqual(registrant, bug_branch.registrant)
-
     def test_bug_start_with_no_linked_branches(self):
         # Bugs have a linked_branches attribute which is initially an empty
         # collection.
@@ -195,8 +171,9 @@
         # BugBranch object.
         bug = self.factory.makeBug()
         branch = self.factory.makeBranch()
-        bug_branch = bug.linkBranch(branch, self.factory.makePerson())
-        self.assertEqual([bug_branch], list(bug.linked_branches))
+        self.assertContentEqual([], list(bug.linked_branches))
+        bug.linkBranch(branch, self.factory.makePerson())
+        self.assertContentEqual([branch], list(bug.linked_branches))
 
     def test_linking_branch_twice_returns_same_IBugBranch(self):
         # Calling Bug.linkBranch twice with the same parameters returns the
@@ -217,18 +194,6 @@
         bug_branch_2 = bug.linkBranch(branch, self.factory.makePerson())
         self.assertEqual(bug_branch, bug_branch_2)
 
-    def test_bug_has_no_branches(self):
-        # Bug.hasBranch returns False for any branch that it is not linked to.
-        bug = self.factory.makeBug()
-        self.assertFalse(bug.hasBranch(self.factory.makeBranch()))
-
-    def test_bug_has_branch(self):
-        # Bug.hasBranch returns False for any branch that it is linked to.
-        bug = self.factory.makeBug()
-        branch = self.factory.makeBranch()
-        bug.linkBranch(branch, self.factory.makePerson())
-        self.assertTrue(bug.hasBranch(branch))
-
     def test_unlink_branch(self):
         # Bug.unlinkBranch removes the bug<->branch link.
         bug = self.factory.makeBug()
@@ -236,7 +201,6 @@
         bug.linkBranch(branch, self.factory.makePerson())
         bug.unlinkBranch(branch, self.factory.makePerson())
         self.assertEqual([], list(bug.linked_branches))
-        self.assertFalse(bug.hasBranch(branch))
 
     def test_unlink_not_linked_branch(self):
         # When unlinkBranch is called with a branch that isn't already linked,
@@ -245,7 +209,6 @@
         branch = self.factory.makeBranch()
         bug.unlinkBranch(branch, self.factory.makePerson())
         self.assertEqual([], list(bug.linked_branches))
-        self.assertFalse(bug.hasBranch(branch))
 
     def test_the_unwashed_cannot_link_branch_to_private_bug(self):
         # Those who cannot see a bug are forbidden to link a branch to it.

=== modified file 'lib/lp/code/browser/branch.py'
--- lib/lp/code/browser/branch.py	2015-10-07 16:14:42 +0000
+++ lib/lp/code/browser/branch.py	2016-02-05 00:02:25 +0000
@@ -190,7 +190,7 @@
         """Traverses to an `IBugBranch`."""
         bug = getUtility(IBugSet).get(bugid)
 
-        for bug_branch in bug.linked_branches:
+        for bug_branch in bug.linked_bugbranches:
             if bug_branch.branch == self.context:
                 return bug_branch
 

=== modified file 'lib/lp/code/doc/branch-xmlrpc.txt'
--- lib/lp/code/doc/branch-xmlrpc.txt	2012-12-10 13:43:47 +0000
+++ lib/lp/code/doc/branch-xmlrpc.txt	2016-02-05 00:02:25 +0000
@@ -291,8 +291,8 @@
 
     >>> from lp.bugs.interfaces.bug import IBugSet
     >>> bug_one = getUtility(IBugSet).get(1)
-    >>> for bug_branch in bug_one.linked_branches:
-    ...     print bug_branch.branch.url
+    >>> for branch in bug_one.linked_branches:
+    ...     print branch.url
     http://foo.com/other_branch
 
 We get an error if we try to specify a non-existant branch or bug:

=== modified file 'lib/lp/code/model/tests/test_branch.py'
--- lib/lp/code/model/tests/test_branch.py	2015-10-12 12:58:32 +0000
+++ lib/lp/code/model/tests/test_branch.py	2016-02-05 00:02:25 +0000
@@ -1582,7 +1582,7 @@
         """break_links allows deleting a branch with a bug."""
         bug1 = self.factory.makeBug()
         bug1.linkBranch(self.branch, self.branch.owner)
-        bug_branch1 = bug1.linked_branches[0]
+        bug_branch1 = bug1.linked_bugbranches[0]
         bug_branch1_id = bug_branch1.id
         self.branch.destroySelf(break_references=True)
         self.assertRaises(SQLObjectNotFound, BugBranch.get, bug_branch1_id)

=== modified file 'lib/lp/code/model/tests/test_branchjob.py'
--- lib/lp/code/model/tests/test_branchjob.py	2015-09-28 17:38:45 +0000
+++ lib/lp/code/model/tests/test_branchjob.py	2016-02-05 00:02:25 +0000
@@ -202,7 +202,7 @@
         with dbuser("branchscanner"):
             job.run()
         self.assertEqual(db_branch.revision_count, 1)
-        self.assertTrue(private_bug.hasBranch(db_branch))
+        self.assertIn(db_branch, private_bug.linked_branches)
 
 
 class TestBranchUpgradeJob(TestCaseWithFactory):

=== modified file 'lib/lp/codehosting/scanner/tests/test_buglinks.py'
--- lib/lp/codehosting/scanner/tests/test_buglinks.py	2013-07-04 07:58:00 +0000
+++ lib/lp/codehosting/scanner/tests/test_buglinks.py	2016-02-05 00:02:25 +0000
@@ -11,7 +11,6 @@
 
 from lp.app.errors import NotFoundError
 from lp.bugs.interfaces.bug import IBugSet
-from lp.bugs.interfaces.bugbranch import IBugBranchSet
 from lp.code.interfaces.revision import IRevisionSet
 from lp.codehosting.scanner import events
 from lp.codehosting.scanner.buglinks import BugBranchLinker
@@ -141,9 +140,7 @@
 
         Raises an assertion error if there's no such bug.
         """
-        bug_branch = getUtility(IBugBranchSet).getBugBranch(bug, branch)
-        if bug_branch is None:
-            self.fail('No BugBranch found for %r, %r' % (bug, branch))
+        self.assertIn(branch, bug.linked_branches)
 
     def test_newMainlineRevisionAddsBugBranch(self):
         """New mainline revisions with bugs properties create BugBranches."""
@@ -187,11 +184,7 @@
         self.syncBazaarBranchToDatabase(self.bzr_branch, self.db_branch)
         # Create a new DB branch to sync with.
         self.syncBazaarBranchToDatabase(self.bzr_branch, self.new_db_branch)
-        self.assertEqual(
-            getUtility(IBugBranchSet).getBugBranch(
-                self.bug1, self.new_db_branch),
-            None,
-            "Should not create a BugBranch.")
+        self.assertNotIn(self.new_db_branch, self.bug1.linked_branches)
 
     def test_nonMainlineRevisionsDontMakeBugBranches(self):
         """Don't add BugBranches based on non-mainline revisions."""
@@ -224,10 +217,7 @@
                 allow_pointless=True)
 
         self.syncBazaarBranchToDatabase(self.bzr_branch, self.db_branch)
-        self.assertEqual(
-            getUtility(IBugBranchSet).getBugBranch(self.bug1, self.db_branch),
-            None,
-            "Should not create a BugBranch.")
+        self.assertNotIn(self.db_branch, self.bug1.linked_branches)
 
     def test_ignoreNonExistentBug(self):
         """If the bug doesn't actually exist, we just ignore it."""
@@ -272,5 +262,4 @@
         revision_set.newFromBazaarRevisions([bzr_revision])
         notify(events.NewMainlineRevisions(
             db_branch, tree.branch, [bzr_revision]))
-        bug_branch = getUtility(IBugBranchSet).getBugBranch(bug, db_branch)
-        self.assertIsNot(None, bug_branch)
+        self.assertIn(db_branch, bug.linked_branches)


Follow ups