Skip to content

Commit 74dc2ac

Browse files
author
Gauvain Pocentek
committed
Add a get method for GitlabObject
This change provides a way to implement GET for objects that don't support it, but support LIST. It is also a first step to a cleaner API.
1 parent c580b1e commit 74dc2ac

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

gitlab/__init__.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -695,12 +695,24 @@ def list(cls, gl, **kwargs):
695695

696696
return gl.list(cls, **kwargs)
697697

698+
@classmethod
699+
def get(cls, gl, id, **kwargs):
700+
if cls.canGet is True:
701+
return cls(gl, id, **kwargs)
702+
elif cls.canGet == 'from_list':
703+
for obj in cls.list(gl, **kwargs):
704+
obj_id = getattr(obj, obj.idAttr)
705+
if str(obj_id) == str(id):
706+
return obj
707+
708+
raise GitlabGetError("Object not found")
709+
698710
@classmethod
699711
def _get_list_or_object(cls, gl, id, **kwargs):
700712
if id is None and cls.getListWhenNoId:
701713
return cls.list(gl, **kwargs)
702714
else:
703-
return cls(gl, id, **kwargs)
715+
return cls.get(gl, id, **kwargs)
704716

705717
def _get_object(self, k, v):
706718
if self._constructorTypes and k in self._constructorTypes:
@@ -834,7 +846,7 @@ def json(self):
834846

835847
class UserKey(GitlabObject):
836848
_url = '/users/%(user_id)s/keys'
837-
canGet = False
849+
canGet = 'from_list'
838850
canUpdate = False
839851
requiredUrlAttrs = ['user_id']
840852
requiredCreateAttrs = ['title', 'key']
@@ -882,7 +894,7 @@ def Key(self, id=None, **kwargs):
882894

883895
class GroupMember(GitlabObject):
884896
_url = '/groups/%(group_id)s/members'
885-
canGet = False
897+
canGet = 'from_list'
886898
requiredUrlAttrs = ['group_id']
887899
requiredCreateAttrs = ['access_level', 'user_id']
888900
requiredUpdateAttrs = ['access_level']
@@ -928,7 +940,7 @@ class Issue(GitlabObject):
928940
_url = '/issues'
929941
_constructorTypes = {'author': 'User', 'assignee': 'User',
930942
'milestone': 'ProjectMilestone'}
931-
canGet = False
943+
canGet = 'from_list'
932944
canDelete = False
933945
canUpdate = False
934946
canCreate = False
@@ -997,7 +1009,7 @@ class ProjectKey(GitlabObject):
9971009

9981010
class ProjectEvent(GitlabObject):
9991011
_url = '/projects/%(project_id)s/events'
1000-
canGet = False
1012+
canGet = 'from_list'
10011013
canDelete = False
10021014
canUpdate = False
10031015
canCreate = False
@@ -1073,7 +1085,7 @@ class ProjectNote(GitlabObject):
10731085
class ProjectTag(GitlabObject):
10741086
_url = '/projects/%(project_id)s/repository/tags'
10751087
idAttr = 'name'
1076-
canGet = False
1088+
canGet = 'from_list'
10771089
canDelete = False
10781090
canUpdate = False
10791091
requiredUrlAttrs = ['project_id']

gitlab/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ def do_list(cls, gl, what, args):
170170

171171

172172
def do_get(cls, gl, what, args):
173-
if not cls.canGet:
173+
if cls.canGet is False:
174174
die("%s objects can't be retrieved" % what)
175175

176176
id = None
177177
if cls not in [gitlab.CurrentUser] and cls.getRequiresId:
178178
id = get_id(cls, args)
179179

180180
try:
181-
o = cls(gl, id, **args)
181+
o = cls.get(gl, id, **args)
182182
except Exception as e:
183183
die("Impossible to get object (%s)" % str(e))
184184

0 commit comments

Comments
 (0)