Skip to content

Commit

Permalink
Limit the queryset to today changes #106
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Jan 2, 2025
1 parent 071c2cf commit 2a041a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
26 changes: 11 additions & 15 deletions vulnerabilities/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,10 @@ def notify_vulnerability_data_update(dataspace):
Trigger the notifications related to fetching vulnerability data from
VulnerableCode.
"""
# today = timezone.now().date()
vulnerability_qs = Vulnerability.objects.scope(
dataspace
) # .filter(last_modified_date__date=today)
vulnerability_qs = Vulnerability.objects.scope(dataspace).added_or_updated_today()
package_qs = Package.objects.scope(dataspace).filter(
affected_by_vulnerabilities__in=vulnerability_qs
)
# product_qs = Product.objects.scope(dataspace).filter(packages=package_qs)

vulnerability_count = vulnerability_qs.count()
if not vulnerability_count:
Expand All @@ -160,6 +156,16 @@ def notify_vulnerability_data_update(dataspace):
package_count = package_qs.count()
subject = "[DejaCode] New vulnerabilities detected!"

# 1. Webhooks (simple message)
message = f"{vulnerability_count} vulnerabilities affecting {package_count} packages"
find_and_fire_hook(
"vulnerability.data_update",
instance=None,
dataspace=dataspace,
payload_override={"text": f"{subject}\n{message}"},
)

# 2. Internal notifications (message with internal links)
# TODO: Add filter by ?last_modified_date=today
package_list_url = reverse("component_catalog:package_list")
package_link = (
Expand All @@ -171,18 +177,8 @@ def notify_vulnerability_data_update(dataspace):
f'<a href="{vulnerability_list_url}" target="_blank">{vulnerability_count} '
f"vulnerabilities</a>"
)

message = f"{vulnerability_link} affecting {package_link}"

# 1. Webhooks
find_and_fire_hook(
"vulnerability.data_update",
instance=None,
dataspace=dataspace,
payload_override={"text": f"{subject}\n{message}"},
)

# 2. Internal notifications
users_to_notify = DejacodeUser.objects.get_vulnerability_notifications_users(dataspace)
for user in users_to_notify:
user.internal_notify(
Expand Down
6 changes: 5 additions & 1 deletion vulnerabilities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.db import models
from django.db.models import Count
from django.utils.translation import gettext_lazy as _

from django.utils import timezone
from cyclonedx.model import vulnerability as cdx_vulnerability

from dje.fields import JSONListField
Expand Down Expand Up @@ -49,6 +49,10 @@ def order_by_risk(self):
models.F("exploitability").desc(nulls_last=True),
)

def added_or_updated_today(self):
today = timezone.now().replace(hour=0, minute=0, second=0)
return self.filter(last_modified_date__gte=today)


class Vulnerability(HistoryDateFieldsMixin, DataspacedModel):
"""
Expand Down

0 comments on commit 2a041a0

Please sign in to comment.