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
6 changes: 6 additions & 0 deletions scanpipe/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ class Meta:
"task_exitcode",
"task_output",
"log",
"current_step",
"execution_time",
"can_start",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can_start runs a query for each not-started run (get_previous_runs()...exists()). Since RunSerializer is also nested in ProjectSerializer.runs, the project list endpoint now costs one extra query per queued run, up to the page size. The assertNumQueries in test_scanpipe_api_project_list does not catch it as those projects have no runs.

The issue use case is polling /api/runs/<uuid>/. What about excluding it from the nested runs: RunSerializer(many=True, read_only=True, exclude_fields=["can_start"])?

]
read_only_fields = [
"current_step",
"can_start",
]
Comment on lines +143 to 147

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read_only_fields is not needed: can_start is a property, so DRF already maps it to a ReadOnlyField, and runs are never written through this serializer (log, task_output are not declared read-only either).

Suggested change
]
read_only_fields = [
"current_step",
"can_start",
]
]



Expand Down
6 changes: 5 additions & 1 deletion scanpipe/pipes/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ def get_headers(self, project):
from scanpipe.api.serializers import RunSerializer

runs = project.runs.all()
runs = RunSerializer(runs, many=True, exclude_fields=("url", "project"))
runs = RunSerializer(
runs,
many=True,
exclude_fields=("url", "project", "can_start", "current_step"),
)

messages = project.projectmessages.all()
messages = ProjectMessageSerializer(messages, many=True)
Expand Down
19 changes: 19 additions & 0 deletions scanpipe/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,25 @@ def test_scanpipe_api_run_detail(self):
self.assertEqual("", response.data["task_output"])
self.assertIsNone(response.data["execution_time"])
self.assertEqual(Run.Status.NOT_STARTED, response.data["status"])
self.assertEqual("", response.data["current_step"])
self.assertTrue(response.data["can_start"])

run1.set_task_started(run1.pk)
run1.set_current_step("extract_archives")
response = self.csrf_client.get(url)
self.assertEqual(Run.Status.RUNNING, response.data["status"])
self.assertEqual("extract_archives", response.data["current_step"])
self.assertFalse(response.data["can_start"])

run2 = self.project1.add_pipeline("analyze_docker_image")
url2 = reverse("run-detail", args=[run2.uuid])
response2 = self.csrf_client.get(url2)
self.assertEqual(Run.Status.NOT_STARTED, response2.data["status"])
self.assertFalse(response2.data["can_start"])

run1.set_task_ended(exitcode=0)
response2 = self.csrf_client.get(url2)
self.assertTrue(response2.data["can_start"])
Comment on lines +1229 to +1237

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests the can_start logic, already covered by test_scanpipe_run_model_can_start. The API test only needs to check that the fields are serialized, I'd drop this part.


@mock.patch("scanpipe.models.Run.execute_task_async")
def test_scanpipe_api_run_action_start_pipeline(self, mock_execute_task):
Expand Down
2 changes: 2 additions & 0 deletions scanpipe/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2499,7 +2499,9 @@ def test_scanpipe_webhook_subscription_model_get_payload(self):
"task_exitcode": None,
"task_output": "",
"log": "",
"current_step": "",
"execution_time": None,
"can_start": True,
Comment on lines +2502 to +2504

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both fields are excluded from the JSON results in output.py but kept in the webhook payload. The webhook is delivered once the run has ended, so can_start is always false there and current_step is either empty or the last executed step. I'd exclude them in WebhookSubscription.get_payload too, for consistency.

},
}

Expand Down
Loading