summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGauvain Pocentek <gauvain@pocentek.net>2017-09-07 20:56:52 +0200
committerGauvain Pocentek <gauvain@pocentek.net>2017-09-07 20:56:52 +0200
commit0268fc91e9596b8b02c13648ae4ea94ae0540f03 (patch)
treec7c91a1307948103e588ecaddad75485ac1eb146
parent947feaf344478fa1b81012124fedaa9de10e224a (diff)
downloadgitlab-0268fc91e9596b8b02c13648ae4ea94ae0540f03.tar.gz
[v4] fix CLI for some mixin methods
-rw-r--r--gitlab/cli.py25
-rw-r--r--gitlab/mixins.py15
-rw-r--r--gitlab/v4/cli.py9
3 files changed, 36 insertions, 13 deletions
diff --git a/gitlab/cli.py b/gitlab/cli.py
index f6b357b..be9b112 100644
--- a/gitlab/cli.py
+++ b/gitlab/cli.py
@@ -35,7 +35,7 @@ camel_re = re.compile('(.)([A-Z])')
custom_actions = {}
-def register_custom_action(cls_name, mandatory=tuple(), optional=tuple()):
+def register_custom_action(cls_names, mandatory=tuple(), optional=tuple()):
def wrap(f):
@functools.wraps(f)
def wrapped_f(*args, **kwargs):
@@ -43,15 +43,20 @@ def register_custom_action(cls_name, mandatory=tuple(), optional=tuple()):
# in_obj defines whether the method belongs to the obj or the manager
in_obj = True
- final_name = cls_name
- if cls_name.endswith('Manager'):
- final_name = cls_name.replace('Manager', '')
- in_obj = False
- if final_name not in custom_actions:
- custom_actions[final_name] = {}
-
- action = f.__name__
- custom_actions[final_name][action] = (mandatory, optional, in_obj)
+ classes = cls_names
+ if type(cls_names) != tuple:
+ classes = (cls_names, )
+
+ for cls_name in cls_names:
+ final_name = cls_name
+ if cls_name.endswith('Manager'):
+ final_name = cls_name.replace('Manager', '')
+ in_obj = False
+ if final_name not in custom_actions:
+ custom_actions[final_name] = {}
+
+ action = f.__name__.replace('_', '-')
+ custom_actions[final_name][action] = (mandatory, optional, in_obj)
return wrapped_f
return wrap
diff --git a/gitlab/mixins.py b/gitlab/mixins.py
index ee98dea..aa52989 100644
--- a/gitlab/mixins.py
+++ b/gitlab/mixins.py
@@ -17,6 +17,7 @@
import gitlab
from gitlab import base
+from gitlab import cli
from gitlab import exceptions as exc
@@ -296,6 +297,8 @@ class ObjectDeleteMixin(object):
class AccessRequestMixin(object):
+ @cli.register_custom_action(('ProjectAccessRequest', 'GroupAccessRequest'),
+ tuple(), ('access_level', ))
@exc.on_http_error(exc.GitlabUpdateError)
def approve(self, access_level=gitlab.DEVELOPER_ACCESS, **kwargs):
"""Approve an access request.
@@ -317,6 +320,8 @@ class AccessRequestMixin(object):
class SubscribableMixin(object):
+ @cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest',
+ 'ProjectLabel'))
@exc.on_http_error(exc.GitlabSubscribeError)
def subscribe(self, **kwargs):
"""Subscribe to the object notifications.
@@ -332,6 +337,8 @@ class SubscribableMixin(object):
server_data = self.manager.gitlab.http_post(path, **kwargs)
self._update_attrs(server_data)
+ @cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest',
+ 'ProjectLabel'))
@exc.on_http_error(exc.GitlabUnsubscribeError)
def unsubscribe(self, **kwargs):
"""Unsubscribe from the object notifications.
@@ -349,6 +356,7 @@ class SubscribableMixin(object):
class TodoMixin(object):
+ @cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest'))
@exc.on_http_error(exc.GitlabHttpError)
def todo(self, **kwargs):
"""Create a todo associated to the object.
@@ -365,6 +373,7 @@ class TodoMixin(object):
class TimeTrackingMixin(object):
+ @cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest'))
@exc.on_http_error(exc.GitlabTimeTrackingError)
def time_stats(self, **kwargs):
"""Get time stats for the object.
@@ -379,6 +388,8 @@ class TimeTrackingMixin(object):
path = '%s/%s/time_stats' % (self.manager.path, self.get_id())
return self.manager.gitlab.http_get(path, **kwargs)
+ @cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest'),
+ ('duration', ))
@exc.on_http_error(exc.GitlabTimeTrackingError)
def time_estimate(self, duration, **kwargs):
"""Set an estimated time of work for the object.
@@ -395,6 +406,7 @@ class TimeTrackingMixin(object):
data = {'duration': duration}
return self.manager.gitlab.http_post(path, post_data=data, **kwargs)
+ @cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest'))
@exc.on_http_error(exc.GitlabTimeTrackingError)
def reset_time_estimate(self, **kwargs):
"""Resets estimated time for the object to 0 seconds.
@@ -409,6 +421,8 @@ class TimeTrackingMixin(object):
path = '%s/%s/rest_time_estimate' % (self.manager.path, self.get_id())
return self.manager.gitlab.http_post(path, **kwargs)
+ @cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest'),
+ ('duration', ))
@exc.on_http_error(exc.GitlabTimeTrackingError)
def add_spent_time(self, duration, **kwargs):
"""Add time spent working on the object.
@@ -425,6 +439,7 @@ class TimeTrackingMixin(object):
data = {'duration': duration}
return self.manager.gitlab.http_post(path, post_data=data, **kwargs)
+ @cli.register_custom_action(('ProjectIssue', 'ProjectMergeRequest'))
@exc.on_http_error(exc.GitlabTimeTrackingError)
def reset_spent_time(self, **kwargs):
"""Resets the time spent working on the object.
diff --git a/gitlab/v4/cli.py b/gitlab/v4/cli.py
index e61ef20..637adfc 100644
--- a/gitlab/v4/cli.py
+++ b/gitlab/v4/cli.py
@@ -33,7 +33,7 @@ class GitlabCLI(object):
self.cls_name = cli.what_to_cls(what)
self.cls = gitlab.v4.objects.__dict__[self.cls_name]
self.what = what.replace('-', '_')
- self.action = action.lower().replace('-', '')
+ self.action = action.lower()
self.gl = gl
self.args = args
self.mgr_cls = getattr(gitlab.v4.objects,
@@ -64,7 +64,8 @@ class GitlabCLI(object):
if gitlab.mixins.GetWithoutIdMixin not in inspect.getmro(self.cls):
data[self.cls._id_attr] = self.args.pop(self.cls._id_attr)
o = self.cls(self.mgr, data)
- return getattr(o, self.action)(**self.args)
+ method_name = self.action.replace('-', '_')
+ return getattr(o, method_name)(**self.args)
else:
return getattr(self.mgr, self.action)(**self.args)
@@ -314,7 +315,9 @@ def run(gl, what, action, args, verbose, output, fields):
if k in fields}
return obj.attributes
- if isinstance(ret_val, list):
+ if isinstance(ret_val, dict):
+ printer.display(ret_val, verbose=True, obj=ret_val)
+ elif isinstance(ret_val, list):
for obj in ret_val:
if isinstance(obj, gitlab.base.RESTObject):
printer.display(get_dict(obj), verbose=verbose, obj=obj)