Skip to content

Commit bd6b4ac

Browse files
author
Gauvain Pocentek
committed
support projects listing: search, all, owned
1 parent 2b4924e commit bd6b4ac

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

gitlab

+46-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ camel_re = re.compile('(.)([A-Z])')
3333

3434
extra_actions = {
3535
gitlab.ProjectBranch: {
36-
'protect': { 'requiredAttrs': ['id', 'project-id'] },
37-
'unprotect': { 'requiredAttrs': ['id', 'project-id'] }
36+
'protect': {'requiredAttrs': ['id', 'project-id']},
37+
'unprotect': {'requiredAttrs': ['id', 'project-id']}
38+
},
39+
gitlab.Project: {
40+
'search': {'requiredAttrs': ['query']},
41+
'owned': {'requiredAttrs': []},
42+
'all': {'requiredAttrs': []}
3843
},
3944
}
4045

@@ -203,6 +208,24 @@ def do_update(cls, d):
203208

204209
return o
205210

211+
def do_project_search(d):
212+
try:
213+
return gl.search_projects(d['query'])
214+
except:
215+
die("Impossible to search projects (%s)" % str(e))
216+
217+
def do_project_all():
218+
try:
219+
return gl.all_projects()
220+
except Exception as e:
221+
die("Impossible to list all projects (%s)" % str(e))
222+
223+
def do_project_owned():
224+
try:
225+
return gl.owned_projects()
226+
except:
227+
die("Impossible to list owned projects (%s)" % str(e))
228+
206229

207230
ssl_verify = True
208231
gitlab_id = None
@@ -338,6 +361,27 @@ elif action == "unprotect":
338361
o = do_get(cls, d)
339362
o.unprotect()
340363

364+
elif action == "search":
365+
if cls != gitlab.Project:
366+
die("%s objects don't support this request" % what)
367+
368+
for o in do_project_search(d):
369+
o.display(verbose)
370+
371+
elif action == "owned":
372+
if cls != gitlab.Project:
373+
die("%s objects don't support this request" % what)
374+
375+
for o in do_project_owned():
376+
o.display(verbose)
377+
378+
elif action == "all":
379+
if cls != gitlab.Project:
380+
die("%s objects don't support this request" % what)
381+
382+
for o in do_project_all():
383+
o.display(verbose)
384+
341385
else:
342386
die("Unknown action: %s. Use \"gitlab %s help\" to get details." % (action, what))
343387

gitlab.py

+26
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,32 @@ def Project(self, id=None, **kwargs):
337337
"""
338338
return self._getListOrObject(Project, id, **kwargs)
339339

340+
def _list_projects(self, url):
341+
r = self.rawGet(url)
342+
if r.status_code != 200:
343+
raise GitlabListError
344+
345+
l = []
346+
for o in r.json():
347+
l.append(Project(self, o))
348+
349+
return l
350+
351+
def search_projects(self, query):
352+
"""Searches projects by name.
353+
354+
Returns a list of matching projects.
355+
"""
356+
return self._list_projects("/projects/search/" + query)
357+
358+
def all_projects(self):
359+
"""Lists all the projects (need admin rights)."""
360+
return self._list_projects("/projects/all")
361+
362+
def owned_projects(self):
363+
"""Lists owned projects."""
364+
return self._list_projects("/projects/owned")
365+
340366
def Group(self, id=None, **kwargs):
341367
"""Creates/gets/lists group(s) known by the GitLab server.
342368

0 commit comments

Comments
 (0)