summaryrefslogtreecommitdiff
path: root/gitlab
diff options
context:
space:
mode:
Diffstat (limited to 'gitlab')
-rw-r--r--gitlab/__init__.py18
-rw-r--r--gitlab/cli.py4
-rw-r--r--gitlab/objects.py15
-rw-r--r--gitlab/tests/test_gitlab.py20
4 files changed, 50 insertions, 7 deletions
diff --git a/gitlab/__init__.py b/gitlab/__init__.py
index 8f26670..c70550b 100644
--- a/gitlab/__init__.py
+++ b/gitlab/__init__.py
@@ -19,6 +19,7 @@
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
+import inspect
import itertools
import json
import warnings
@@ -419,11 +420,14 @@ class Gitlab(object):
raise_error_from_response(r, GitlabGetError)
return r.json()
- def delete(self, obj, **kwargs):
+ def delete(self, obj, id=None, **kwargs):
"""Delete an object on the GitLab server.
Args:
- obj (object): The object to delete.
+ obj (object or id): The object, or the class of the object to
+ delete. If it is the class, the id of the object must be
+ specified as the `id` arguments.
+ id: ID of the object to remove. Required if `obj` is a class.
**kwargs: Additional arguments to send to GitLab.
Returns:
@@ -433,7 +437,13 @@ class Gitlab(object):
GitlabConnectionError: If the server cannot be reached.
GitlabDeleteError: If the server fails to perform the request.
"""
- params = obj.__dict__.copy()
+ if inspect.isclass(obj):
+ if not issubclass(obj, GitlabObject):
+ raise GitlabError("Invalid class: %s" % obj)
+ params = {}
+ params[obj.idAttr] = id
+ else:
+ params = obj.__dict__.copy()
params.update(kwargs)
missing = []
for k in itertools.chain(obj.requiredUrlAttrs,
@@ -444,7 +454,7 @@ class Gitlab(object):
raise GitlabDeleteError('Missing attribute(s): %s' %
", ".join(missing))
- obj_id = getattr(obj, obj.idAttr)
+ obj_id = params[obj.idAttr]
url = self._construct_url(id_=obj_id, obj=obj, parameters=params)
headers = self._create_headers()
diff --git a/gitlab/cli.py b/gitlab/cli.py
index 3882c19..c2b2fa5 100644
--- a/gitlab/cli.py
+++ b/gitlab/cli.py
@@ -188,9 +188,9 @@ def do_delete(cls, gl, what, args):
if not cls.canDelete:
_die("%s objects can't be deleted" % what)
- o = do_get(cls, gl, what, args)
+ id = args.pop(cls.idAttr)
try:
- o.delete()
+ gl.delete(cls, id, **args)
except Exception as e:
_die("Impossible to destroy object (%s)" % str(e))
diff --git a/gitlab/objects.py b/gitlab/objects.py
index 628b41f..2ab2a52 100644
--- a/gitlab/objects.py
+++ b/gitlab/objects.py
@@ -134,6 +134,21 @@ class BaseManager(object):
raise NotImplementedError
return self.obj_cls.create(self.gitlab, data, **kwargs)
+ def delete(self, id, **kwargs):
+ """Delete a GitLab object.
+
+ Args:
+ id: ID of the object to delete.
+
+ Raises:
+ NotImplementedError: If objects cannot be deleted.
+ GitlabDeleteError: If the server fails to perform the request.
+ """
+ self._set_parent_args(**kwargs)
+ if not self.obj_cls.canDelete:
+ raise NotImplementedError
+ self.gitlab.delete(self.obj_cls, id, **kwargs)
+
def _custom_list(self, url, cls, **kwargs):
r = self.gitlab._raw_get(url, **kwargs)
raise_error_from_response(r, GitlabListError)
diff --git a/gitlab/tests/test_gitlab.py b/gitlab/tests/test_gitlab.py
index 337320f..7872083 100644
--- a/gitlab/tests/test_gitlab.py
+++ b/gitlab/tests/test_gitlab.py
@@ -338,7 +338,7 @@ class TestGitlabMethods(unittest.TestCase):
self.assertRaises(GitlabGetError, self.gl.get,
Project, 1)
- def test_delete(self):
+ def test_delete_from_object(self):
@urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups/1",
method="delete")
def resp_delete_group(url, request):
@@ -351,6 +351,24 @@ class TestGitlabMethods(unittest.TestCase):
data = self.gl.delete(obj)
self.assertIs(data, True)
+ def test_delete_from_invalid_class(self):
+ class InvalidClass(object):
+ pass
+
+ self.assertRaises(GitlabError, self.gl.delete, InvalidClass, 1)
+
+ def test_delete_from_class(self):
+ @urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups/1",
+ method="delete")
+ def resp_delete_group(url, request):
+ headers = {'content-type': 'application/json'}
+ content = ''.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_delete_group):
+ data = self.gl.delete(Group, 1)
+ self.assertIs(data, True)
+
def test_delete_unknown_path(self):
obj = Project(self.gl, data={"name": "testname", "id": 1})
obj._from_api = True