Skip to content

Commit 453224a

Browse files
author
Gauvain Pocentek
committed
Re-implement _custom_list in the Gitlab class
Rename the method _raw_list. This adds support for the ``all=True`` option to enable automatic recursion and avoid pagination if requested by the user. Fixes #93
1 parent 44d0dc5 commit 453224a

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

docs/api-usage.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@ You can use pagination to go throught long lists:
112112
113113
ten_first_groups = gl.groups.list(page=0, per_page=10)
114114
115-
Use the ``all`` parameter to get all the items:
115+
Use the ``all`` parameter to get all the items when using listing methods:
116116

117117
.. code-block:: python
118118
119119
all_groups = gl.groups.list(all=True)
120+
all_owned_projects = gl.projects.owned(all=True)
120121
121122
Sudo
122123
====

gitlab/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,31 @@ def _raw_get(self, path, content_type=None, **kwargs):
269269
raise GitlabConnectionError(
270270
"Can't connect to GitLab server (%s)" % e)
271271

272+
def _raw_list(self, path, cls, **kwargs):
273+
r = self._raw_get(path, **kwargs)
274+
raise_error_from_response(r, GitlabListError)
275+
276+
cls_kwargs = kwargs.copy()
277+
278+
# Add _from_api manually, because we are not creating objects
279+
# through normal path
280+
cls_kwargs['_from_api'] = True
281+
get_all_results = kwargs.get('all', False)
282+
283+
# Remove parameters from kwargs before passing it to constructor
284+
for key in ['all', 'page', 'per_page', 'sudo']:
285+
if key in cls_kwargs:
286+
del cls_kwargs[key]
287+
288+
results = [cls(self, item, **cls_kwargs) for item in r.json()
289+
if item is not None]
290+
if ('next' in r.links and 'url' in r.links['next']
291+
and get_all_results is True):
292+
args = kwargs.copy()
293+
args['next_url'] = r.links['next']['url']
294+
results.extend(self.list(cls, **args))
295+
return results
296+
272297
def _raw_post(self, path, data=None, content_type=None, **kwargs):
273298
url = '%s%s' % (self._url, path)
274299
headers = self._create_headers(content_type)

gitlab/objects.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,6 @@ def delete(self, id, **kwargs):
155155
raise NotImplementedError
156156
self.gitlab.delete(self.obj_cls, id, **args)
157157

158-
def _custom_list(self, url, cls, **kwargs):
159-
r = self.gitlab._raw_get(url, **kwargs)
160-
raise_error_from_response(r, GitlabListError)
161-
162-
l = []
163-
for j in r.json():
164-
o = cls(self.gitlab, j)
165-
o._from_api = True
166-
l.append(o)
167-
168-
return l
169-
170158

171159
class GitlabObject(object):
172160
"""Base class for all classes that interface with GitLab."""
@@ -569,6 +557,7 @@ def search(self, query, **kwargs):
569557
570558
Args:
571559
query (str): The query string to send to GitLab for the search.
560+
all (bool): If True, return all the items, without pagination
572561
**kwargs: Additional arguments to send to GitLab.
573562
574563
Returns:
@@ -579,7 +568,7 @@ def search(self, query, **kwargs):
579568
GitlabListError: If the server fails to perform the request.
580569
"""
581570
url = self.obj_cls._url + '?search=' + query
582-
return self._custom_list(url, self.obj_cls, **kwargs)
571+
return self.gitlab._raw_list(url, self.obj_cls, **kwargs)
583572

584573
def get_by_username(self, username, **kwargs):
585574
"""Get a user by its username.
@@ -596,7 +585,7 @@ def get_by_username(self, username, **kwargs):
596585
GitlabGetError: If the server fails to perform the request.
597586
"""
598587
url = self.obj_cls._url + '?username=' + username
599-
results = self._custom_list(url, self.obj_cls, **kwargs)
588+
results = self.gitlab._raw_list(url, self.obj_cls, **kwargs)
600589
assert len(results) in (0, 1)
601590
try:
602591
return results[0]
@@ -712,10 +701,15 @@ class GroupManager(BaseManager):
712701
def search(self, query, **kwargs):
713702
"""Searches groups by name.
714703
715-
Returns a list of matching groups.
704+
Args:
705+
query (str): The search string
706+
all (bool): If True, return all the items, without pagination
707+
708+
Returns:
709+
list(Group): a list of matching groups.
716710
"""
717711
url = '/groups?search=' + query
718-
return self._custom_list(url, Group, **kwargs)
712+
return self.gitlab._raw_list(url, self.obj_cls, **kwargs)
719713

720714

721715
class Hook(GitlabObject):
@@ -1524,35 +1518,38 @@ def search(self, query, **kwargs):
15241518
15251519
Args:
15261520
query (str): The query string to send to GitLab for the search.
1521+
all (bool): If True, return all the items, without pagination
15271522
**kwargs: Additional arguments to send to GitLab.
15281523
15291524
Returns:
15301525
list(Project): A list of matching projects.
15311526
"""
1532-
return self._custom_list("/projects/search/" + query, Project,
1533-
**kwargs)
1527+
return self.gitlab._raw_list("/projects/search/" + query, Project,
1528+
**kwargs)
15341529

15351530
def all(self, **kwargs):
15361531
"""List all the projects (need admin rights).
15371532
15381533
Args:
1534+
all (bool): If True, return all the items, without pagination
15391535
**kwargs: Additional arguments to send to GitLab.
15401536
15411537
Returns:
15421538
list(Project): The list of projects.
15431539
"""
1544-
return self._custom_list("/projects/all", Project, **kwargs)
1540+
return self.gitlab._raw_list("/projects/all", Project, **kwargs)
15451541

15461542
def owned(self, **kwargs):
15471543
"""List owned projects.
15481544
15491545
Args:
1546+
all (bool): If True, return all the items, without pagination
15501547
**kwargs: Additional arguments to send to GitLab.
15511548
15521549
Returns:
15531550
list(Project): The list of owned projects.
15541551
"""
1555-
return self._custom_list("/projects/owned", Project, **kwargs)
1552+
return self.gitlab._raw_list("/projects/owned", Project, **kwargs)
15561553

15571554

15581555
class UserProjectManager(BaseManager):

0 commit comments

Comments
 (0)