Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5513d0f

Browse files
author
Gauvain Pocentek
committed
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 parent0ae315a commit5513d0f

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
raiseNotImplementedError
7171
returnself.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+
forjinr.json():
79+
o=cls(self,j)
80+
o._from_api=True
81+
l.append(o)
82+
83+
returnl
84+
7385

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

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

437457
classHook(GitlabObject):
438458
_url='/hooks'
@@ -1016,32 +1036,21 @@ class UserProject(GitlabObject):
10161036
classProjectManager(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-
foroinr.json():
1025-
p=Project(self,o)
1026-
p._from_api=True
1027-
l.append(p)
1028-
1029-
returnl
1030-
10311039
defsearch(self,query,**kwargs):
10321040
"""Searches projects by name.
10331041
10341042
Returns a list of matching projects.
10351043
"""
1036-
returnself._custom_list("/projects/search/"+query,**kwargs)
1044+
returnself._custom_list("/projects/search/"+query,Project,
1045+
**kwargs)
10371046

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

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

10461055

10471056
classUserProjectManager(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+
deftest_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+
defresp_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+
returnresponse(200,content,headers,None,5,request)
229+
230+
withHTTMock(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)

[8]ページ先頭

©2009-2025 Movatter.jp