Skip to content

Commit 777167e

Browse files
authored
Ensure a project cannot be deleted through the API while a pipeline is running (#439)
* Ensure project cannot be deleted in the API while a pipeline is running #402 Signed-off-by: Thomas Druez <tdruez@nexb.com>
1 parent 8ce5985 commit 777167e

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ v31.0.0 (next)
2626
- Add new SCANCODEIO_REDIS_PASSWORD environment variable and setting
2727
to optionally set Redis instance password
2828

29+
- Ensure a project cannot be deleted through the API while a pipeline is running.
30+
https://github.com/nexB/scancode.io/issues/402
31+
2932
v30.2.0 (2021-12-17)
3033
--------------------
3134

scanpipe/api/views.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,12 @@ def add_input(self, request, *args, **kwargs):
261261

262262
return Response({"status": "Input(s) added."})
263263

264+
def destroy(self, request, *args, **kwargs):
265+
try:
266+
return super().destroy(request, *args, **kwargs)
267+
except RunInProgressError as error:
268+
return Response({"status": str(error)}, status=status.HTTP_400_BAD_REQUEST)
269+
264270
@action(detail=True, methods=["get", "post"])
265271
def archive(self, request, *args, **kwargs):
266272
project = self.get_object()

scanpipe/tests/test_api.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,25 @@ def test_scanpipe_api_project_action_summary(self):
373373
self.assertEqual(status.HTTP_200_OK, response.status_code)
374374
self.assertEqual(10, len(response.data.keys()))
375375

376+
def test_scanpipe_api_project_action_delete(self):
377+
run = self.project1.add_pipeline("docker")
378+
run.set_task_started(task_id=uuid.uuid4())
379+
self.assertEqual(run.Status.RUNNING, run.status)
380+
381+
response = self.csrf_client.delete(self.project1_detail_url)
382+
self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
383+
expected = (
384+
"Cannot execute this action until all associated pipeline runs are "
385+
"completed."
386+
)
387+
self.assertEqual(expected, response.data["status"])
388+
389+
run.set_task_ended(exitcode=0)
390+
self.assertEqual(run.Status.SUCCESS, run.status)
391+
response = self.csrf_client.delete(self.project1_detail_url)
392+
self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
393+
self.assertFalse(Project.objects.filter(pk=self.project1.pk).exists())
394+
376395
def test_scanpipe_api_project_action_archive(self):
377396
(self.project1.input_path / "input_file").touch()
378397
(self.project1.codebase_path / "codebase_file").touch()

0 commit comments

Comments
 (0)