From e7f9f2465bd5103925d79c0312d3b583d80e49b5 Mon Sep 17 00:00:00 2001 From: Harold Blankenship <36673698+hblankenship@users.noreply.github.com> Date: Thu, 2 Jan 2025 12:27:45 -0600 Subject: [PATCH] add ordering to test_import API endpoint (#11448) --- dojo/api_v2/views.py | 15 ++++----------- dojo/filters.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/dojo/api_v2/views.py b/dojo/api_v2/views.py index bca0a75e0b6..90fe948b32c 100644 --- a/dojo/api_v2/views.py +++ b/dojo/api_v2/views.py @@ -60,6 +60,7 @@ ApiTestFilter, ReportFindingFilter, ReportFindingFilterWithoutObjectLookups, + TestImportAPIFilter, ) from dojo.finding.queries import ( get_authorized_findings, @@ -2259,17 +2260,9 @@ class TestImportViewSet( serializer_class = serializers.TestImportSerializer queryset = Test_Import.objects.none() filter_backends = (DjangoFilterBackend,) - filterset_fields = [ - "test", - "findings_affected", - "version", - "branch_tag", - "build_id", - "commit_hash", - "test_import_finding_action__action", - "test_import_finding_action__finding", - "test_import_finding_action__created", - ] + + filterset_class = TestImportAPIFilter + permission_classes = ( IsAuthenticated, permissions.UserHasTestImportPermission, diff --git a/dojo/filters.py b/dojo/filters.py index 6a1228865b1..b4c452c2fea 100644 --- a/dojo/filters.py +++ b/dojo/filters.py @@ -3212,6 +3212,7 @@ class Meta: exclude = ["users"] +# This class is used exclusively by Findings class TestImportFilter(DojoFilter): version = CharFilter(field_name="version", lookup_expr="icontains") version_exact = CharFilter(field_name="version", lookup_expr="iexact", label="Version Exact") @@ -3238,6 +3239,7 @@ class Meta: fields = [] +# This class is used exclusively by Findings class TestImportFindingActionFilter(DojoFilter): action = MultipleChoiceFilter(choices=IMPORT_ACTIONS) o = OrderingFilter( @@ -3252,6 +3254,35 @@ class Meta: fields = [] +# Used within the TestImport API +class TestImportAPIFilter(DojoFilter): + o = OrderingFilter( + # tuple-mapping retains order + fields=( + ("id", "id"), + ("created", "created"), + ("modified", "modified"), + ("version", "version"), + ("branch_tag", "branch_tag"), + ("build_id", "build_id"), + ("commit_hash", "commit_hash"), + + ), + ) + + class Meta: + model = Test_Import + fields = ["test", + "findings_affected", + "version", + "branch_tag", + "build_id", + "commit_hash", + "test_import_finding_action__action", + "test_import_finding_action__finding", + "test_import_finding_action__created"] + + class LogEntryFilter(DojoFilter): from auditlog.models import LogEntry