← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~pelpsi/launchpad:new-API-endpoint-to-select-a-git-repository-by-id into launchpad:master

 

Simone Pelosi has proposed merging ~pelpsi/launchpad:new-API-endpoint-to-select-a-git-repository-by-id into launchpad:master.

Commit message:
Created API endpoint to select a git repository by id

Added new API to get repository by ID.
Added three test cases for the new API.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~pelpsi/launchpad/+git/launchpad/+merge/439326
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~pelpsi/launchpad:new-API-endpoint-to-select-a-git-repository-by-id into launchpad:master.
diff --git a/lib/lp/code/interfaces/gitrepository.py b/lib/lp/code/interfaces/gitrepository.py
index 453954f..31ed38b 100644
--- a/lib/lp/code/interfaces/gitrepository.py
+++ b/lib/lp/code/interfaces/gitrepository.py
@@ -1356,6 +1356,17 @@ class IGitRepositorySet(Interface):
         :param with_hosting: Create the repository on the hosting service.
         """
 
+    @call_with(user=REQUEST_USER)
+    @operation_parameters(id=Int(title=_("Repository id"), required=True))
+    @operation_returns_entry(IGitRepository)
+    @export_read_operation()
+    @operation_for_version("devel")
+    def getByID(user, id):
+        """Find a repository by its id.
+
+        Return None if no match was found.
+        """
+
     # Marker for references to Git URL layouts: ##GITNAMESPACE##
     @call_with(user=REQUEST_USER)
     @operation_parameters(
diff --git a/lib/lp/code/model/gitrepository.py b/lib/lp/code/model/gitrepository.py
index 8021ac4..0ffae31 100644
--- a/lib/lp/code/model/gitrepository.py
+++ b/lib/lp/code/model/gitrepository.py
@@ -2231,6 +2231,17 @@ class GitRepositorySet:
             clone_from_repository=clone_from_repository,
         )
 
+    def getByID(self, user, id):
+        """See `IGitRepositorySet`."""
+        repository = getUtility(IGitLookup).get(id)
+        if repository is None:
+            return None
+        # removeSecurityProxy is safe here since we're explicitly performing
+        # a permission check.
+        if removeSecurityProxy(repository).visibleByUser(user):
+            return repository
+        return None
+
     def getByPath(self, user, path):
         """See `IGitRepositorySet`."""
         repository, extra_path = getUtility(IGitLookup).getByPath(path)
diff --git a/lib/lp/code/model/tests/test_gitrepository.py b/lib/lp/code/model/tests/test_gitrepository.py
index 216d302..2b31131 100644
--- a/lib/lp/code/model/tests/test_gitrepository.py
+++ b/lib/lp/code/model/tests/test_gitrepository.py
@@ -4801,6 +4801,39 @@ class TestGitRepositorySet(TestCaseWithFactory):
         # GitRepositorySet instances provide IGitRepositorySet.
         verifyObject(IGitRepositorySet, self.repository_set)
 
+    def test_getByID(self):
+        # getByID returns a repository matching the id that it's given.
+        a = self.factory.makeGitRepository()
+        self.factory.makeGitRepository()
+        repository = self.repository_set.getByID(a.owner, a.id)
+        self.assertEqual(a, repository)
+
+    def test_getByID_not_found(self):
+        # If a repository cannot be found for a given id, then getById returns
+        # None.
+        a = self.factory.makeGitRepository()
+        self.factory.makeGitRepository()
+        repository = self.repository_set.getByID(a.owner, -1)
+        self.assertIsNone(repository)
+
+    def test_getByID_inaccessible(self):
+        # If the given user cannot view the matched repository, then
+        # getByID returns None.
+        owner = self.factory.makePerson()
+        repository = self.factory.makeGitRepository(
+            owner=owner, information_type=InformationType.USERDATA
+        )
+        with person_logged_in(owner):
+            repository_id = repository.id
+        self.assertEqual(
+            repository, self.repository_set.getByID(owner, repository_id)
+        )
+        self.assertIsNone(
+            self.repository_set.getByID(
+                self.factory.makePerson(), repository_id
+            )
+        )
+
     def test_getByPath(self):
         # getByPath returns a repository matching the path that it's given.
         a = self.factory.makeGitRepository()