Skip to content

Commit 3b78f00

Browse files
committed
Add 'bundle delete' command
Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent 87e6152 commit 3b78f00

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

git_pw/api.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,22 @@ def _patch(url, data):
181181
return rsp
182182

183183

184+
def _delete(url):
185+
# type: (str) -> requests.Response
186+
"""Make DELETE request and handle errors."""
187+
LOG.debug('DELETE %s', url)
188+
189+
try:
190+
rsp = requests.delete(url, auth=_get_auth(), headers=_get_headers())
191+
rsp.raise_for_status()
192+
except requests.exceptions.RequestException as exc:
193+
_handle_error('delete', exc)
194+
195+
LOG.debug('Got response')
196+
197+
return rsp
198+
199+
184200
def version():
185201
# type: () -> Optional[Tuple[int, int]]
186202
"""Get the version of the server from the URL, if present."""
@@ -309,6 +325,25 @@ def create(resource_type, data):
309325
return _post(url, data).json()
310326

311327

328+
def delete(resource_type, resource_id):
329+
# type: (str, Union[str, int]) -> None
330+
"""Delete a specific API resource.
331+
332+
DELETE /{resource}/{resourceID}/
333+
334+
Arguments:
335+
resource_type: The resource endpoint name.
336+
resource_id: The ID for the specific resource.
337+
338+
Returns:
339+
A dictionary representing the detailed view of a given resource.
340+
"""
341+
# NOTE(stephenfin): All resources must have a trailing '/'
342+
url = '/'.join([_get_server(), resource_type, str(resource_id), ''])
343+
344+
_delete(url)
345+
346+
312347
def update(resource_type, resource_id, data):
313348
# type: (str, Union[int, str], dict) -> dict
314349
"""Update a specific API resource.

git_pw/bundle.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,21 @@ def update_cmd(bundle_id, name, patch_ids, public, fmt):
240240
bundle = api.update('bundles', bundle_id, data)
241241

242242
_show_bundle(bundle, fmt)
243+
244+
245+
@click.command(name='delete')
246+
@click.argument('bundle_id')
247+
@api.validate_minimum_version(
248+
(1, 2), 'Deleting bundles is only supported from API version 1.2',
249+
)
250+
@utils.format_options
251+
def delete_cmd(bundle_id, fmt):
252+
"""Delete a bundle.
253+
254+
Delete bundle BUNDLE_ID.
255+
256+
Requires API version 1.2 or greater.
257+
"""
258+
LOG.debug('Delete bundle: id=%s', bundle_id)
259+
260+
api.delete('bundles', bundle_id)

git_pw/shell.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def bundle():
122122
bundle.add_command(bundle_cmds.apply_cmd)
123123
bundle.add_command(bundle_cmds.show_cmd)
124124
bundle.add_command(bundle_cmds.download_cmd)
125+
bundle.add_command(bundle_cmds.list_cmd)
125126
bundle.add_command(bundle_cmds.create_cmd)
126127
bundle.add_command(bundle_cmds.update_cmd)
127-
bundle.add_command(bundle_cmds.list_cmd)
128+
bundle.add_command(bundle_cmds.delete_cmd)

releasenotes/notes/bundle-crud-47aadae6eb7a20ad.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ features:
55
66
- ``bundle create``
77
- ``bundle update``
8+
- ``bundle delete``
89
910
Together, these allow for creation, modification and deletion of bundles.
1011
Bundles are custom, user-defined groups of patches that can be used to keep

tests/test_bundle.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,34 @@ def test_update_api_v1_1(
425425

426426
assert result.exit_code == 1, result
427427
assert mock_log.error.called
428+
429+
430+
@mock.patch('git_pw.api.version', return_value=(1, 2))
431+
@mock.patch('git_pw.api.delete')
432+
@mock.patch('git_pw.utils.echo_via_pager')
433+
class DeleteTestCase(unittest.TestCase):
434+
435+
def test_delete(self, mock_echo, mock_delete, mock_version):
436+
"""Validate standard behavior."""
437+
438+
mock_delete.return_value = None
439+
440+
runner = CLIRunner()
441+
result = runner.invoke(bundle.delete_cmd, ['hello'])
442+
443+
assert result.exit_code == 0, result
444+
mock_delete.assert_called_once_with('bundles', 'hello')
445+
446+
@mock.patch('git_pw.api.LOG')
447+
def test_delete_api_v1_1(
448+
self, mock_log, mock_echo, mock_delete, mock_version,
449+
):
450+
"""Validate standard behavior."""
451+
452+
mock_version.return_value = (1, 1)
453+
454+
runner = CLIRunner()
455+
result = runner.invoke(bundle.delete_cmd, ['hello'])
456+
457+
assert result.exit_code == 1, result
458+
assert mock_log.error.called

0 commit comments

Comments
 (0)