Skip to content

Commit 96a1a78

Browse files
author
Gauvain Pocentek
committed
Add support for getting list of user projects
Fixes #403
1 parent 08f19b3 commit 96a1a78

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

docs/gl_objects/projects.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
# user create
3434
alice = gl.users.list(username='alice')[0]
3535
user_project = alice.projects.create({'name': 'project'})
36+
user_projects = alice.projects.list()
3637
# end user create
3738

3839
# update

gitlab/v4/objects.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class UserProject(RESTObject):
181181
pass
182182

183183

184-
class UserProjectManager(CreateMixin, RESTManager):
184+
class UserProjectManager(ListMixin, CreateMixin, RESTManager):
185185
_path = '/projects/user/%(user_id)s'
186186
_obj_cls = UserProject
187187
_from_parent_attrs = {'user_id': 'id'}
@@ -192,6 +192,31 @@ class UserProjectManager(CreateMixin, RESTManager):
192192
'public', 'visibility', 'description', 'builds_enabled',
193193
'public_builds', 'import_url', 'only_allow_merge_if_build_succeeds')
194194
)
195+
_list_filters = ('archived', 'visibility', 'order_by', 'sort', 'search',
196+
'simple', 'owned', 'membership', 'starred', 'statistics',
197+
'with_issues_enabled', 'with_merge_requests_enabled')
198+
199+
def list(self, **kwargs):
200+
"""Retrieve a list of objects.
201+
202+
Args:
203+
all (bool): If True, return all the items, without pagination
204+
per_page (int): Number of items to retrieve per request
205+
page (int): ID of the page to return (starts with page 1)
206+
as_list (bool): If set to False and no pagination option is
207+
defined, return a generator instead of a list
208+
**kwargs: Extra options to send to the Gitlab server (e.g. sudo)
209+
210+
Returns:
211+
list: The list of objects, or a generator if `as_list` is False
212+
213+
Raises:
214+
GitlabAuthenticationError: If authentication is not correct
215+
GitlabListError: If the server cannot perform the request
216+
"""
217+
218+
path = '/users/%s/projects' % self._parent.id
219+
return ListMixin.list(self, path=path, **kwargs)
195220

196221

197222
class User(SaveMixin, ObjectDeleteMixin, RESTObject):

tools/python_test_v4.py

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@
9292
new_user.block()
9393
new_user.unblock()
9494

95+
# user projects list
96+
assert(len(new_user.projects.list()) == 0)
97+
9598
foobar_user = gl.users.create(
9699
{'email': 'foobar@example.com', 'username': 'foobar',
97100
'name': 'Foo Bar', 'password': 'foobar_password'})

0 commit comments

Comments
 (0)