Skip to content

Commit 5cb5632

Browse files
committed
feat: include advisory todo count in api response
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent a7e9ce8 commit 5cb5632

2 files changed

Lines changed: 85 additions & 60 deletions

File tree

‎vulnerabilities/api_v3.py‎

Lines changed: 84 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from django.contrib.postgres.aggregates import ArrayAgg
1414
from django.core.cache import cache
15+
from django.db.models import Count
1516
from django.db.models import Exists
1617
from django.db.models import F
1718
from django.db.models import Max
@@ -126,6 +127,7 @@ class AdvisoryV3Serializer(serializers.ModelSerializer):
126127
severities = AdvisorySeveritySerializer(many=True)
127128
advisory_uid = serializers.CharField(source="avid", read_only=True)
128129
related_ssvc_trees = serializers.SerializerMethodField()
130+
todo_count = serializers.IntegerField(read_only=True)
129131

130132
def get_related_ssvc_trees(self, obj):
131133
seen = set()
@@ -165,6 +167,7 @@ class Meta:
165167
"weighted_severity",
166168
"risk_score",
167169
"related_ssvc_trees",
170+
"todo_count",
168171
]
169172

170173

@@ -396,51 +399,58 @@ def create(self, request, *args, **kwargs):
396399

397400
purls = serializer.validated_data["purls"]
398401

399-
latest_advisories = AdvisoryV2.objects.latest_advisories_for_purls(
400-
purls=purls
401-
).prefetch_related(
402-
Prefetch(
403-
"references",
404-
queryset=AdvisoryReference.objects.only(
405-
"id",
406-
"url",
407-
"reference_type",
408-
"reference_id",
402+
latest_advisories = (
403+
AdvisoryV2.objects.latest_advisories_for_purls(purls=purls)
404+
.annotate(
405+
todo_count=Count(
406+
"advisory_todos",
407+
filter=Q(advisory_todos__is_todo_stale=False),
408+
)
409+
)
410+
.prefetch_related(
411+
Prefetch(
412+
"references",
413+
queryset=AdvisoryReference.objects.only(
414+
"id",
415+
"url",
416+
"reference_type",
417+
"reference_id",
418+
),
409419
),
410-
),
411-
Prefetch(
412-
"severities",
413-
queryset=AdvisorySeverity.objects.only(
414-
"id",
415-
"url",
416-
"value",
417-
"scoring_system",
418-
"scoring_elements",
419-
"published_at",
420+
Prefetch(
421+
"severities",
422+
queryset=AdvisorySeverity.objects.only(
423+
"id",
424+
"url",
425+
"value",
426+
"scoring_system",
427+
"scoring_elements",
428+
"published_at",
429+
),
420430
),
421-
),
422-
"weaknesses",
423-
"aliases",
424-
Prefetch(
425-
"related_ssvcs",
426-
queryset=SSVC.objects.only(
427-
"id",
428-
"vector",
429-
"decision",
430-
"options",
431-
"source_advisory__url",
431+
"weaknesses",
432+
"aliases",
433+
Prefetch(
434+
"related_ssvcs",
435+
queryset=SSVC.objects.only(
436+
"id",
437+
"vector",
438+
"decision",
439+
"options",
440+
"source_advisory__url",
441+
),
432442
),
433-
),
434-
Prefetch(
435-
"source_ssvcs",
436-
queryset=SSVC.objects.only(
437-
"id",
438-
"vector",
439-
"decision",
440-
"options",
441-
"source_advisory__url",
443+
Prefetch(
444+
"source_ssvcs",
445+
queryset=SSVC.objects.only(
446+
"id",
447+
"vector",
448+
"decision",
449+
"options",
450+
"source_advisory__url",
451+
),
442452
),
443-
),
453+
)
444454
)
445455

446456
page = self.paginate_queryset(latest_advisories)
@@ -627,6 +637,10 @@ def get_affected_advisories_bulk(packages, max_advisories, base_url, reachabilit
627637
max_exploitability=Max(
628638
"members__advisory__exploitability",
629639
),
640+
todo_count=Count(
641+
"primary_advisory__advisory_todos",
642+
filter=Q(primary_advisory__advisory_todos__is_todo_stale=False),
643+
),
630644
)
631645
.only(
632646
"id",
@@ -772,6 +786,7 @@ def get_affected_advisories_bulk(packages, max_advisories, base_url, reachabilit
772786
),
773787
"ssvc_trees": adv.ssvc_trees,
774788
"resource_url": resource_url,
789+
"todo_count": adv.todo_count,
775790
}
776791
)
777792

@@ -809,25 +824,34 @@ def get_affected_advisories_bulk(packages, max_advisories, base_url, reachabilit
809824
if not allowed_package_ids:
810825
return result
811826

812-
advisories = AdvisoryV2.objects.filter(
813-
id__in=allowed_advisory_ids,
814-
).prefetch_related(
815-
"aliases",
816-
Prefetch(
817-
"related_ssvcs",
818-
queryset=(
819-
SSVC.objects.select_related("source_advisory")
820-
.only(
821-
"id",
822-
"decision",
823-
"options",
824-
"vector",
825-
"source_advisory__url",
826-
)
827-
.distinct("source_advisory__url")
827+
advisories = (
828+
AdvisoryV2.objects.filter(
829+
id__in=allowed_advisory_ids,
830+
)
831+
.annotate(
832+
todo_count=Count(
833+
"advisory_todos",
834+
filter=Q(advisory_todos__is_todo_stale=False),
835+
)
836+
)
837+
.prefetch_related(
838+
"aliases",
839+
Prefetch(
840+
"related_ssvcs",
841+
queryset=(
842+
SSVC.objects.select_related("source_advisory")
843+
.only(
844+
"id",
845+
"decision",
846+
"options",
847+
"vector",
848+
"source_advisory__url",
849+
)
850+
.distinct("source_advisory__url")
851+
),
852+
to_attr="prefetched_ssvc_trees",
828853
),
829-
to_attr="prefetched_ssvc_trees",
830-
),
854+
)
831855
)
832856

833857
advisory_by_id = {advisory.id: advisory for advisory in advisories}
@@ -880,6 +904,7 @@ def get_affected_advisories_bulk(packages, max_advisories, base_url, reachabilit
880904
for ssvc in advisory.prefetched_ssvc_trees
881905
],
882906
"resource_url": resource_url,
907+
"todo_count": advisory.todo_count,
883908
}
884909
)
885910

‎vulnerabilities/throttling.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from django.core.exceptions import ImproperlyConfigured
1111
from rest_framework.exceptions import Throttled
1212
from rest_framework.throttling import UserRateThrottle
13-
from rest_framework.views import exception_handler
1413

1514

1615
class PermissionBasedUserRateThrottle(UserRateThrottle):
@@ -68,6 +67,7 @@ def throttled_exception_handler(exception, context):
6867
"""
6968
Return this response whenever a request has been throttled
7069
"""
70+
from rest_framework.views import exception_handler
7171

7272
response = exception_handler(exception, context)
7373

0 commit comments

Comments
 (0)