← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:git-repository-adapters into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:git-repository-adapters into launchpad:master.

Commit message:
Add adapters from various targets to IGitRepository

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/448601

Since `GitRefVocabulary` attempts to adapt the vocabulary context to `IGitRepository`, this fixes the problem where the git branch picker in the web UI only worked if the repository was given as the full unique name for a repository (e.g. `~cjwatson/launchpad/+git/launchpad`) rather than one of its shortened forms (e.g. `~cjwatson/launchpad`).
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:git-repository-adapters into launchpad:master.
diff --git a/lib/lp/code/adapters/gitrepository.py b/lib/lp/code/adapters/gitrepository.py
index 1687bf3..10dd832 100644
--- a/lib/lp/code/adapters/gitrepository.py
+++ b/lib/lp/code/adapters/gitrepository.py
@@ -4,18 +4,64 @@
 """Components and adapters related to Git repositories."""
 
 __all__ = [
+    "git_repository_for_distro_source_package",
+    "git_repository_for_oci_project",
+    "git_repository_for_person_distro_source_package",
+    "git_repository_for_person_oci_project",
+    "git_repository_for_person_product",
+    "git_repository_for_project",
     "GitRepositoryDelta",
 ]
 
 from lazr.lifecycle.objectdelta import ObjectDelta
+from zope.component import getUtility
 from zope.interface import implementer
 
 from lp.code.interfaces.gitrepository import (
     IGitRepository,
     IGitRepositoryDelta,
+    IGitRepositorySet,
 )
 
 
+def git_repository_for_project(project):
+    """Adapt a project to a Git repository."""
+    return getUtility(IGitRepositorySet).getDefaultRepository(project)
+
+
+def git_repository_for_distro_source_package(distro_source_package):
+    """Adapt a distribution source package to a Git repository."""
+    return getUtility(IGitRepositorySet).getDefaultRepository(
+        distro_source_package
+    )
+
+
+def git_repository_for_oci_project(oci_project):
+    """Adapt an OCI project to a Git repository."""
+    return getUtility(IGitRepositorySet).getDefaultRepository(oci_project)
+
+
+def git_repository_for_person_product(person_product):
+    """Adapt a PersonProduct to a Git repository."""
+    return getUtility(IGitRepositorySet).getDefaultRepositoryForOwner(
+        person_product.person, person_product.product
+    )
+
+
+def git_repository_for_person_distro_source_package(person_dsp):
+    """Adapt a PersonDistributionSourcePackage to a Git repository."""
+    return getUtility(IGitRepositorySet).getDefaultRepositoryForOwner(
+        person_dsp.person, person_dsp.distro_source_package
+    )
+
+
+def git_repository_for_person_oci_project(person_oci_project):
+    """Adapt a PersonOCIProject to a Git repository."""
+    return getUtility(IGitRepositorySet).getDefaultRepositoryForOwner(
+        person_oci_project.person, person_oci_project.oci_project
+    )
+
+
 @implementer(IGitRepositoryDelta)
 class GitRepositoryDelta:
     """See `IGitRepositoryDelta`."""
diff --git a/lib/lp/code/adapters/tests/test_gitrepository.py b/lib/lp/code/adapters/tests/test_gitrepository.py
index 5fc0de1..b87b738 100644
--- a/lib/lp/code/adapters/tests/test_gitrepository.py
+++ b/lib/lp/code/adapters/tests/test_gitrepository.py
@@ -3,11 +3,73 @@
 
 from lazr.lifecycle.snapshot import Snapshot
 from testtools.matchers import MatchesStructure
+from zope.component import getUtility
 from zope.interface import providedBy
 
 from lp.code.adapters.gitrepository import GitRepositoryDelta
-from lp.testing import TestCaseWithFactory, person_logged_in
-from lp.testing.layers import LaunchpadFunctionalLayer
+from lp.code.interfaces.gitrepository import IGitRepository, IGitRepositorySet
+from lp.registry.model.persondistributionsourcepackage import (
+    PersonDistributionSourcePackage,
+)
+from lp.registry.model.personociproject import PersonOCIProject
+from lp.registry.model.personproduct import PersonProduct
+from lp.testing import TestCaseWithFactory, admin_logged_in, person_logged_in
+from lp.testing.layers import DatabaseFunctionalLayer, LaunchpadFunctionalLayer
+
+
+class TestAdapters(TestCaseWithFactory):
+    layer = DatabaseFunctionalLayer
+
+    def assertTargetBehaviour(self, target):
+        self.assertRaises(TypeError, IGitRepository, target)
+        repository = self.factory.makeGitRepository(target=target)
+        self.assertRaises(TypeError, IGitRepository, target)
+        with admin_logged_in():
+            getUtility(IGitRepositorySet).setDefaultRepository(
+                target, repository
+            )
+        self.assertEqual(repository, IGitRepository(target))
+
+    def test_project(self):
+        self.assertTargetBehaviour(self.factory.makeProduct())
+
+    def test_distribution_source_package(self):
+        self.assertTargetBehaviour(
+            self.factory.makeDistributionSourcePackage()
+        )
+
+    def test_oci_project(self):
+        self.assertTargetBehaviour(self.factory.makeOCIProject())
+
+    def assertPersonTargetBehaviour(self, target, person_target_factory):
+        person = self.factory.makePerson()
+        person_target = person_target_factory(person, target)
+        self.assertRaises(TypeError, IGitRepository, person_target)
+        repository = self.factory.makeGitRepository(
+            owner=person, target=target
+        )
+        self.assertRaises(TypeError, IGitRepository, person_target)
+        with person_logged_in(person):
+            getUtility(IGitRepositorySet).setDefaultRepositoryForOwner(
+                person, target, repository, person
+            )
+        self.assertEqual(repository, IGitRepository(person_target))
+
+    def test_person_project(self):
+        self.assertPersonTargetBehaviour(
+            self.factory.makeProduct(), PersonProduct
+        )
+
+    def test_person_distribution_source_package(self):
+        self.assertPersonTargetBehaviour(
+            self.factory.makeDistributionSourcePackage(),
+            PersonDistributionSourcePackage,
+        )
+
+    def test_person_oci_project(self):
+        self.assertPersonTargetBehaviour(
+            self.factory.makeOCIProject(), PersonOCIProject
+        )
 
 
 class TestGitRepositoryDelta(TestCaseWithFactory):
diff --git a/lib/lp/code/configure.zcml b/lib/lp/code/configure.zcml
index cf2e52e..862832f 100644
--- a/lib/lp/code/configure.zcml
+++ b/lib/lp/code/configure.zcml
@@ -847,6 +847,30 @@
         set_schema="lp.code.interfaces.gitrepository.IGitRepositoryAdminAttributes" />
   </class>
   <adapter
+      for="lp.registry.interfaces.product.IProduct"
+      provides="lp.code.interfaces.gitrepository.IGitRepository"
+      factory="lp.code.adapters.gitrepository.git_repository_for_project"/>
+  <adapter
+      for="lp.registry.interfaces.distributionsourcepackage.IDistributionSourcePackage"
+      provides="lp.code.interfaces.gitrepository.IGitRepository"
+      factory="lp.code.adapters.gitrepository.git_repository_for_distro_source_package"/>
+  <adapter
+      for="lp.registry.interfaces.ociproject.IOCIProject"
+      provides="lp.code.interfaces.gitrepository.IGitRepository"
+      factory="lp.code.adapters.gitrepository.git_repository_for_oci_project"/>
+  <adapter
+      for="lp.registry.interfaces.personproduct.IPersonProduct"
+      provides="lp.code.interfaces.gitrepository.IGitRepository"
+      factory="lp.code.adapters.gitrepository.git_repository_for_person_product"/>
+  <adapter
+      for="lp.registry.interfaces.persondistributionsourcepackage.IPersonDistributionSourcePackage"
+      provides="lp.code.interfaces.gitrepository.IGitRepository"
+      factory="lp.code.adapters.gitrepository.git_repository_for_person_distro_source_package"/>
+  <adapter
+      for="lp.registry.interfaces.personociproject.IPersonOCIProject"
+      provides="lp.code.interfaces.gitrepository.IGitRepository"
+      factory="lp.code.adapters.gitrepository.git_repository_for_person_oci_project"/>
+  <adapter
       for="lp.code.interfaces.gitrepository.IGitRepository"
       provides="lp.services.webapp.interfaces.ILaunchpadContainer"
       factory="lp.code.publisher.LaunchpadGitRepositoryContainer"/>