Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion scanpipe/management/commands/delete-project.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@

import sys

from django.core.management import CommandError

from scanpipe.management.commands import ProjectCommand
from scanpipe.models import RunInProgressError


class Command(ProjectCommand):
Expand Down Expand Up @@ -52,7 +55,10 @@ def handle(self, *inputs, **options):
self.stdout.write("Deletion cancelled.")
sys.exit(0)

self.project.delete()
try:
self.project.delete()
except RunInProgressError as error:
raise CommandError(error)

if self.verbosity > 0:
msg = f"All the {self.project} project data have been removed."
Expand Down
20 changes: 19 additions & 1 deletion scanpipe/management/commands/flush-projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from django.utils import timezone

from scanpipe.models import Project
from scanpipe.models import RunInProgressError


class Command(BaseCommand):
Expand Down Expand Up @@ -82,13 +83,19 @@ def add_arguments(self, parser):
"Do not delete any projects; just print the ones that would be flushed."
),
)
parser.add_argument(
"--skip-running",
action="store_true",
help="Skip projects that have active pipeline runs in progress.",
)

def handle(self, *inputs, **options):
verbosity = options["verbosity"]
retain_days = options["retain_days"]
labels = options["labels"]
pipelines = options["pipelines"]
dry_run = options["dry_run"]
skip_running = options["skip_running"]
projects = Project.objects.order_by("-created_date").distinct()

if retain_days:
Expand Down Expand Up @@ -128,7 +135,18 @@ def handle(self, *inputs, **options):

deletion_count = 0
for project in projects:
project.delete()
if skip_running:
try:
project.delete()
except RunInProgressError:
if verbosity > 0:
msg = self.style.WARNING(
f"Skipping {project}: pipeline run in progress"
)
self.stdout.write(msg)
continue
else:
project.delete()
deletion_count += 1

if verbosity > 0:
Expand Down
39 changes: 39 additions & 0 deletions scanpipe/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from scanpipe.models import DiscoveredPackage
from scanpipe.models import Project
from scanpipe.models import Run
from scanpipe.models import RunInProgressError
from scanpipe.models import WebhookSubscription
from scanpipe.pipes import flag
from scanpipe.pipes import purldb
Expand Down Expand Up @@ -827,6 +828,19 @@ def test_scanpipe_management_command_delete_project(self):
self.assertFalse(Project.objects.filter(name="my_project").exists())
self.assertFalse(work_path.exists())

def test_scanpipe_management_command_delete_project_run_in_progress(self):
project = make_project(name="my_project")
run = project.add_pipeline("analyze_docker_image")
run.set_task_started(task_id=1)

options = ["--project", project.name, "--no-color", "--no-input"]
expected_error = (
"Cannot execute this action until all associated pipeline runs are "
"completed."
)
with self.assertRaisesMessage(CommandError, expected_error):
call_command("delete-project", *options)

def test_scanpipe_management_command_archive_project(self):
project = make_project(name="my_project")
(project.input_path / "input_file").touch()
Expand Down Expand Up @@ -974,6 +988,31 @@ def test_scanpipe_management_command_flush_projects_filters(self):
expected = "1 projects would be deleted:\n- project3"
self.assertEqual(expected, out_value)

def test_scanpipe_management_command_flush_projects_skip_running(self):
project1 = make_project("project1")
make_project("project2")
run = project1.add_pipeline("analyze_docker_image")
run.set_task_started(task_id=1)

options = ["--no-color", "--no-input"]
with self.assertRaises(RunInProgressError):
call_command("flush-projects", *options)

if not Project.objects.filter(name="project2").exists():
make_project("project2")

out = StringIO()
options = ["--no-color", "--no-input", "--skip-running"]
call_command("flush-projects", *options, stdout=out)
out_value = out.getvalue().strip()
expected = (
"Skipping project1: pipeline run in progress\n"
"1 project and its related data have been removed."
)
self.assertEqual(expected, out_value)
self.assertTrue(Project.objects.filter(name="project1").exists())
self.assertFalse(Project.objects.filter(name="project2").exists())

def test_scanpipe_management_command_create_user(self):
out = StringIO()

Expand Down