Skip to content

Commit 0268fc9

Browse files
author
Gauvain Pocentek
committed
[v4] fix CLI for some mixin methods
1 parent 947feaf commit 0268fc9

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

gitlab/cli.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,28 @@
3535
custom_actions = {}
3636

3737

38-
def register_custom_action(cls_name, mandatory=tuple(), optional=tuple()):
38+
def register_custom_action(cls_names, mandatory=tuple(), optional=tuple()):
3939
def wrap(f):
4040
@functools.wraps(f)
4141
def wrapped_f(*args, **kwargs):
4242
return f(*args, **kwargs)
4343

4444
# in_obj defines whether the method belongs to the obj or the manager
4545
in_obj = True
46-
final_name = cls_name
47-
if cls_name.endswith('Manager'):
48-
final_name = cls_name.replace('Manager', '')
49-
in_obj = False
50-
if final_name not in custom_actions:
51-
custom_actions[final_name] = {}
52-
53-
action = f.__name__
54-
custom_actions[final_name][action] = (mandatory, optional, in_obj)
46+
classes = cls_names
47+
if type(cls_names) != tuple:
48+
classes = (cls_names, )
49+
50+
for cls_name in cls_names:
51+
final_name = cls_name
52+
if cls_name.endswith('Manager'):
53+
final_name = cls_name.replace('Manager', '')
54+
in_obj = False
55+
if final_name not in custom_actions:
56+
custom_actions[final_name] = {}
57+
58+
action = f.__name__.replace('_', '-')
59+
custom_actions[final_name][action] = (mandatory, optional, in_obj)
5560

5661
return wrapped_f
5762
return wrap

gitlab/mixins.py

+15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import gitlab
1919
from gitlab import base
20+
from gitlab import cli
2021
from gitlab import exceptions as exc
2122

2223

@@ -296,6 +297,8 @@ def delete(self, **kwargs):
296297

297298

298299
class AccessRequestMixin(object):
300+
@cli.register_custom_action(('ProjectAccessRequest', 'GroupAccessRequest'),
301+
tuple(), ('access_level', ))
299302
@exc.on_http_error(exc.GitlabUpdateError)
300303
def approve(self, access_level=gitlab.DEVELOPER_ACCESS, **kwargs):
301304
"""Approve an access request.
@@ -317,6 +320,8 @@ def approve(self, access_level=gitlab.DEVELOPER_ACCESS, **kwargs):
317320

318321

319322
class SubscribableMixin(object):
323+
@cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest',
324+
'ProjectLabel'))
320325
@exc.on_http_error(exc.GitlabSubscribeError)
321326
def subscribe(self, **kwargs):
322327
"""Subscribe to the object notifications.
@@ -332,6 +337,8 @@ def subscribe(self, **kwargs):
332337
server_data = self.manager.gitlab.http_post(path, **kwargs)
333338
self._update_attrs(server_data)
334339

340+
@cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest',
341+
'ProjectLabel'))
335342
@exc.on_http_error(exc.GitlabUnsubscribeError)
336343
def unsubscribe(self, **kwargs):
337344
"""Unsubscribe from the object notifications.
@@ -349,6 +356,7 @@ def unsubscribe(self, **kwargs):
349356

350357

351358
class TodoMixin(object):
359+
@cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest'))
352360
@exc.on_http_error(exc.GitlabHttpError)
353361
def todo(self, **kwargs):
354362
"""Create a todo associated to the object.
@@ -365,6 +373,7 @@ def todo(self, **kwargs):
365373

366374

367375
class TimeTrackingMixin(object):
376+
@cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest'))
368377
@exc.on_http_error(exc.GitlabTimeTrackingError)
369378
def time_stats(self, **kwargs):
370379
"""Get time stats for the object.
@@ -379,6 +388,8 @@ def time_stats(self, **kwargs):
379388
path = '%s/%s/time_stats' % (self.manager.path, self.get_id())
380389
return self.manager.gitlab.http_get(path, **kwargs)
381390

391+
@cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest'),
392+
('duration', ))
382393
@exc.on_http_error(exc.GitlabTimeTrackingError)
383394
def time_estimate(self, duration, **kwargs):
384395
"""Set an estimated time of work for the object.
@@ -395,6 +406,7 @@ def time_estimate(self, duration, **kwargs):
395406
data = {'duration': duration}
396407
return self.manager.gitlab.http_post(path, post_data=data, **kwargs)
397408

409+
@cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest'))
398410
@exc.on_http_error(exc.GitlabTimeTrackingError)
399411
def reset_time_estimate(self, **kwargs):
400412
"""Resets estimated time for the object to 0 seconds.
@@ -409,6 +421,8 @@ def reset_time_estimate(self, **kwargs):
409421
path = '%s/%s/rest_time_estimate' % (self.manager.path, self.get_id())
410422
return self.manager.gitlab.http_post(path, **kwargs)
411423

424+
@cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest'),
425+
('duration', ))
412426
@exc.on_http_error(exc.GitlabTimeTrackingError)
413427
def add_spent_time(self, duration, **kwargs):
414428
"""Add time spent working on the object.
@@ -425,6 +439,7 @@ def add_spent_time(self, duration, **kwargs):
425439
data = {'duration': duration}
426440
return self.manager.gitlab.http_post(path, post_data=data, **kwargs)
427441

442+
@cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest'))
428443
@exc.on_http_error(exc.GitlabTimeTrackingError)
429444
def reset_spent_time(self, **kwargs):
430445
"""Resets the time spent working on the object.

gitlab/v4/cli.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(self, gl, what, action, args):
3333
self.cls_name = cli.what_to_cls(what)
3434
self.cls = gitlab.v4.objects.__dict__[self.cls_name]
3535
self.what = what.replace('-', '_')
36-
self.action = action.lower().replace('-', '')
36+
self.action = action.lower()
3737
self.gl = gl
3838
self.args = args
3939
self.mgr_cls = getattr(gitlab.v4.objects,
@@ -64,7 +64,8 @@ def do_custom(self):
6464
if gitlab.mixins.GetWithoutIdMixin not in inspect.getmro(self.cls):
6565
data[self.cls._id_attr] = self.args.pop(self.cls._id_attr)
6666
o = self.cls(self.mgr, data)
67-
return getattr(o, self.action)(**self.args)
67+
method_name = self.action.replace('-', '_')
68+
return getattr(o, method_name)(**self.args)
6869
else:
6970
return getattr(self.mgr, self.action)(**self.args)
7071

@@ -314,7 +315,9 @@ def get_dict(obj):
314315
if k in fields}
315316
return obj.attributes
316317

317-
if isinstance(ret_val, list):
318+
if isinstance(ret_val, dict):
319+
printer.display(ret_val, verbose=True, obj=ret_val)
320+
elif isinstance(ret_val, list):
318321
for obj in ret_val:
319322
if isinstance(obj, gitlab.base.RESTObject):
320323
printer.display(get_dict(obj), verbose=verbose, obj=obj)

0 commit comments

Comments
 (0)