Skip to content

Commit 5513d0f

Browse files
author
Gauvain Pocentek
committedJan 9, 2016
Add support for groups search
Factorize the code to avoid duplication with the ProjectManager class. Implement unit tests for the group search. Original patchh from Daniel Serodio (PR #55).
1 parent 0ae315a commit 5513d0f

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed
 

‎gitlab/objects.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ def create(self, data, **kwargs):
7070
raise NotImplementedError
7171
return self.obj_cls.create(self.gitlab, data, **kwargs)
7272

73+
def _custom_list(self, url, cls, **kwargs):
74+
r = self.gitlab._raw_get(url, **kwargs)
75+
raise_error_from_response(r, GitlabListError)
76+
77+
l = []
78+
for j in r.json():
79+
o = cls(self, j)
80+
o._from_api = True
81+
l.append(o)
82+
83+
return l
84+
7385

7486
class GitlabObject(object):
7587
"""Base class for all classes that interface with GitLab
@@ -433,6 +445,14 @@ def transfer_project(self, id, **kwargs):
433445
class GroupManager(BaseManager):
434446
obj_cls = Group
435447

448+
def search(self, query, **kwargs):
449+
"""Searches groups by name.
450+
451+
Returns a list of matching groups.
452+
"""
453+
url = '/groups?search=' + query
454+
return self._custom_list(url, Group, **kwargs)
455+
436456

437457
class Hook(GitlabObject):
438458
_url = '/hooks'
@@ -1016,32 +1036,21 @@ class UserProject(GitlabObject):
10161036
class ProjectManager(BaseManager):
10171037
obj_cls = Project
10181038

1019-
def _custom_list(self, url, **kwargs):
1020-
r = self.gitlab._raw_get(url, **kwargs)
1021-
raise_error_from_response(r, GitlabListError)
1022-
1023-
l = []
1024-
for o in r.json():
1025-
p = Project(self, o)
1026-
p._from_api = True
1027-
l.append(p)
1028-
1029-
return l
1030-
10311039
def search(self, query, **kwargs):
10321040
"""Searches projects by name.
10331041
10341042
Returns a list of matching projects.
10351043
"""
1036-
return self._custom_list("/projects/search/" + query, **kwargs)
1044+
return self._custom_list("/projects/search/" + query, Project,
1045+
**kwargs)
10371046

10381047
def all(self, **kwargs):
10391048
"""Lists all the projects (need admin rights)."""
1040-
return self._custom_list("/projects/all", **kwargs)
1049+
return self._custom_list("/projects/all", Project, **kwargs)
10411050

10421051
def owned(self, **kwargs):
10431052
"""Lists owned projects."""
1044-
return self._custom_list("/projects/owned", **kwargs)
1053+
return self._custom_list("/projects/owned", Project, **kwargs)
10451054

10461055

10471056
class UserProjectManager(BaseManager):

‎gitlab/tests/test_manager.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,26 @@ def resp_get_all(url, request):
214214
self.assertEqual(data[1].name, "foo2")
215215
self.assertEqual(data[0].id, 1)
216216
self.assertEqual(data[1].id, 2)
217+
218+
def test_group_manager_search(self):
219+
mgr = GroupManager(self.gitlab)
220+
221+
@urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups",
222+
query="search=foo", method="get")
223+
def resp_get_search(url, request):
224+
headers = {'content-type': 'application/json'}
225+
content = ('[{"name": "foo1", "id": 1}, '
226+
'{"name": "foo2", "id": 2}]')
227+
content = content.encode("utf-8")
228+
return response(200, content, headers, None, 5, request)
229+
230+
with HTTMock(resp_get_search):
231+
data = mgr.search('foo')
232+
self.assertEqual(type(data), list)
233+
self.assertEqual(2, len(data))
234+
self.assertEqual(type(data[0]), Group)
235+
self.assertEqual(type(data[1]), Group)
236+
self.assertEqual(data[0].name, "foo1")
237+
self.assertEqual(data[1].name, "foo2")
238+
self.assertEqual(data[0].id, 1)
239+
self.assertEqual(data[1].id, 2)

0 commit comments

Comments
 (0)