| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e5821e6 commit 1d7ebea
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,6 +19,7 @@ | |||
| 19 | 19 | from __future__ import print_function | |
| 20 | 20 | from __future__ import division | |
| 21 | 21 | from __future__ import absolute_import | |
| 22 | + import inspect | ||
| 22 | 23 | import itertools | |
| 23 | 24 | import json | |
| 24 | 25 | import warnings | |
@@ -419,11 +420,14 @@ def get(self, obj_class, id=None, **kwargs): | |||
| 419 | 420 | raise_error_from_response(r, GitlabGetError) | |
| 420 | 421 | return r.json() | |
| 421 | 422 | ||
| 422 | - def delete(self, obj, **kwargs): | ||
| 423 | + def delete(self, obj, id=None, **kwargs): | ||
| 423 | 424 | """Delete an object on the GitLab server. | |
| 424 | 425 | ||
| 425 | 426 | Args: | |
| 426 | - obj (object): The object to delete. | ||
| 427 | + obj (object or id): The object, or the class of the object to | ||
| 428 | + delete. If it is the class, the id of the object must be | ||
| 429 | + specified as the `id` arguments. | ||
| 430 | + id: ID of the object to remove. Required if `obj` is a class. | ||
| 427 | 431 | **kwargs: Additional arguments to send to GitLab. | |
| 428 | 432 | ||
| 429 | 433 | Returns: | |
@@ -433,7 +437,13 @@ def delete(self, obj, **kwargs): | |||
| 433 | 437 | GitlabConnectionError: If the server cannot be reached. | |
| 434 | 438 | GitlabDeleteError: If the server fails to perform the request. | |
| 435 | 439 | """ | |
| 436 | - params = obj.__dict__.copy() | ||
| 440 | + if inspect.isclass(obj): | ||
| 441 | + if not issubclass(obj, GitlabObject): | ||
| 442 | + raise GitlabError("Invalid class: %s" % obj) | ||
| 443 | + params = {} | ||
| 444 | + params[obj.idAttr] = id | ||
| 445 | + else: | ||
| 446 | + params = obj.__dict__.copy() | ||
| 437 | 447 | params.update(kwargs) | |
| 438 | 448 | missing = [] | |
| 439 | 449 | for k in itertools.chain(obj.requiredUrlAttrs, | |
@@ -444,7 +454,7 @@ def delete(self, obj, **kwargs): | |||
| 444 | 454 | raise GitlabDeleteError('Missing attribute(s): %s' % | |
| 445 | 455 | ", ".join(missing)) | |
| 446 | 456 | ||
| 447 | - obj_id = getattr(obj, obj.idAttr) | ||
| 457 | + obj_id = params[obj.idAttr] | ||
| 448 | 458 | url = self._construct_url(id_=obj_id, obj=obj, parameters=params) | |
| 449 | 459 | headers = self._create_headers() | |
| 450 | 460 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -188,9 +188,9 @@ def do_delete(cls, gl, what, args): | |||
| 188 | 188 | if not cls.canDelete: | |
| 189 | 189 | _die("%s objects can't be deleted" % what) | |
| 190 | 190 | ||
| 191 | - o = do_get(cls, gl, what, args) | ||
| 191 | + id = args.pop(cls.idAttr) | ||
| 192 | 192 | try: | |
| 193 | - o.delete() | ||
| 193 | + gl.delete(cls, id, **args) | ||
| 194 | 194 | except Exception as e: | |
| 195 | 195 | _die("Impossible to destroy object (%s)" % str(e)) | |
| 196 | 196 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -134,6 +134,21 @@ def create(self, data, **kwargs): | |||
| 134 | 134 | raise NotImplementedError | |
| 135 | 135 | return self.obj_cls.create(self.gitlab, data, **kwargs) | |
| 136 | 136 | ||
| 137 | + def delete(self, id, **kwargs): | ||
| 138 | + """Delete a GitLab object. | ||
| 139 | + | ||
| 140 | + Args: | ||
| 141 | + id: ID of the object to delete. | ||
| 142 | + | ||
| 143 | + Raises: | ||
| 144 | + NotImplementedError: If objects cannot be deleted. | ||
| 145 | + GitlabDeleteError: If the server fails to perform the request. | ||
| 146 | + """ | ||
| 147 | + self._set_parent_args(**kwargs) | ||
| 148 | + if not self.obj_cls.canDelete: | ||
| 149 | + raise NotImplementedError | ||
| 150 | + self.gitlab.delete(self.obj_cls, id, **kwargs) | ||
| 151 | + | ||
| 137 | 152 | def _custom_list(self, url, cls, **kwargs): | |
| 138 | 153 | r = self.gitlab._raw_get(url, **kwargs) | |
| 139 | 154 | raise_error_from_response(r, GitlabListError) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -338,7 +338,7 @@ def resp_cont(url, request): | |||
| 338 | 338 | self.assertRaises(GitlabGetError, self.gl.get, | |
| 339 | 339 | Project, 1) | |
| 340 | 340 | ||
| 341 | - def test_delete(self): | ||
| 341 | + def test_delete_from_object(self): | ||
| 342 | 342 | @urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups/1", | |
| 343 | 343 | method="delete") | |
| 344 | 344 | def resp_delete_group(url, request): | |
@@ -351,6 +351,24 @@ def resp_delete_group(url, request): | |||
| 351 | 351 | data = self.gl.delete(obj) | |
| 352 | 352 | self.assertIs(data, True) | |
| 353 | 353 | ||
| 354 | + def test_delete_from_invalid_class(self): | ||
| 355 | + class InvalidClass(object): | ||
| 356 | + pass | ||
| 357 | + | ||
| 358 | + self.assertRaises(GitlabError, self.gl.delete, InvalidClass, 1) | ||
| 359 | + | ||
| 360 | + def test_delete_from_class(self): | ||
| 361 | + @urlmatch(scheme="http", netloc="localhost", path="/api/v3/groups/1", | ||
| 362 | + method="delete") | ||
| 363 | + def resp_delete_group(url, request): | ||
| 364 | + headers = {'content-type': 'application/json'} | ||
| 365 | + content = ''.encode("utf-8") | ||
| 366 | + return response(200, content, headers, None, 5, request) | ||
| 367 | + | ||
| 368 | + with HTTMock(resp_delete_group): | ||
| 369 | + data = self.gl.delete(Group, 1) | ||
| 370 | + self.assertIs(data, True) | ||
| 371 | + | ||
| 354 | 372 | def test_delete_unknown_path(self): | |
| 355 | 373 | obj = Project(self.gl, data={"name": "testname", "id": 1}) | |
| 356 | 374 | obj._from_api = True | |
| Back | FazBrowse Home | New Git URL |
0 commit comments