Skip to content

Commit 6c0c6c8

Browse files
committed
Add status url in live evaluation API response #1953
Signed-off-by: Michael Ehab Mikhail <michael.ehab@hotmail.com>
1 parent a7f7041 commit 6c0c6c8

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

vulnerabilities/api_v2.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
#
99

1010

11-
from concurrent.futures import ThreadPoolExecutor
12-
from concurrent.futures import as_completed
13-
1411
from django.db.models import Prefetch
12+
from django.urls import reverse
1513
from django_filters import rest_framework as filters
1614
from drf_spectacular.utils import OpenApiParameter
1715
from drf_spectacular.utils import extend_schema
@@ -1371,8 +1369,24 @@ def evaluate(self, request):
13711369
"run_id": str(run_id) if run_id else None,
13721370
}
13731371
)
1372+
1373+
request_obj = request
1374+
status_path = reverse(
1375+
"live-evaluation-status", kwargs={"live_run_id": str(live_run.run_id)}
1376+
)
1377+
1378+
if hasattr(request_obj, "build_absolute_uri"):
1379+
status_url = request_obj.build_absolute_uri(status_path)
1380+
else:
1381+
status_url = status_path
1382+
13741383
return Response(
1375-
{"live_run_id": str(live_run.run_id), "runs": runs}, status=status.HTTP_202_ACCEPTED
1384+
{
1385+
"live_run_id": str(live_run.run_id),
1386+
"runs": runs,
1387+
"status_url": status_url,
1388+
},
1389+
status=status.HTTP_202_ACCEPTED,
13761390
)
13771391

13781392
@extend_schema(
@@ -1390,7 +1404,6 @@ def evaluate(self, request):
13901404
@action(detail=False, methods=["get"], url_path=r"status/(?P<live_run_id>[0-9a-f\-]{36})")
13911405
def status(self, request, live_run_id=None):
13921406
from vulnerabilities.models import LivePipelineRun
1393-
from vulnerabilities.models import PipelineRun
13941407

13951408
try:
13961409
live_run = LivePipelineRun.objects.get(run_id=live_run_id)

vulnerabilities/tests/test_api_v2.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,15 +915,17 @@ def setUp(self):
915915
@patch("vulnerabilities.api_v2.enqueue_ad_hoc_pipeline")
916916
@patch("vulnerabilities.models.PipelineRun.objects.get")
917917
@patch("vulnerabilities.models.LivePipelineRun.objects.create")
918+
@patch("django.urls.reverse")
918919
def test_evaluate_success(
919-
self, mock_live_create, mock_pipeline_get, mock_enqueue, mock_registry
920+
self, mock_reverse, mock_live_create, mock_pipeline_get, mock_enqueue, mock_registry
920921
):
921922
class MockImporter:
922923
pipeline_id = "pypa_live_importer_v2"
923924
supported_types = ["pypi"]
924925

925926
mock_registry.values.return_value = [MockImporter]
926-
mock_live_run = type("MockLiveRun", (), {"run_id": "mock-live-id"})()
927+
valid_uuid = "00000000-0000-0000-0000-000000000001"
928+
mock_live_run = type("MockLiveRun", (), {"run_id": valid_uuid})()
927929
mock_live_create.return_value = mock_live_run
928930
mock_enqueue.return_value = "mock-run-id"
929931
mock_pipeline_run = type(
@@ -932,6 +934,7 @@ class MockImporter:
932934
{"run_id": "mock-run-id", "live_pipeline": None, "save": lambda self: None},
933935
)()
934936
mock_pipeline_get.return_value = mock_pipeline_run
937+
mock_reverse.return_value = f"/api/v2/live-evaluation/status/{valid_uuid}"
935938

936939
data = {"purl": "pkg:pypi/django@3.2"}
937940
response = self.client.post(self.url, data, format="json")
@@ -940,6 +943,8 @@ class MockImporter:
940943
assert response.data["live_run_id"] is not None
941944
assert response.data["runs"][0]["importer"] == "pypa_live_importer_v2"
942945
assert response.data["runs"][0]["run_id"] is not None
946+
assert "status_url" in response.data
947+
assert response.data["status_url"].endswith(f"/api/v2/live-evaluation/status/{valid_uuid}")
943948

944949
@patch("vulnerabilities.api_v2.LIVE_IMPORTERS_REGISTRY")
945950
def test_evaluate_no_importer_found(self, mock_registry):

0 commit comments

Comments
 (0)