Skip to content

Commit

Permalink
Add endpoint for package scan notification #504
Browse files Browse the repository at this point in the history
Signed-off-by: Jono Yang <[email protected]>
  • Loading branch information
JonoYang committed Jul 30, 2024
1 parent 97b9790 commit 8ee69b7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
52 changes: 51 additions & 1 deletion minecode/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@

import json

from django import http
from django.core import signing
from django.db import transaction
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST

from packageurl import PackageURL
from rest_framework import serializers, status, viewsets
from rest_framework.decorators import action
Expand All @@ -21,7 +26,7 @@
# But importing the mappers and visitors module triggers routes registration
from minecode import visitors # NOQA
from minecode import priority_router
from minecode.models import PriorityResourceURI, ResourceURI, ScannableURI
from minecode.models import PriorityResourceURI, ResourceURI, ScannableURI, PurldbUser
from minecode.permissions import IsScanQueueWorkerAPIUser
from minecode.utils import get_temp_file
from minecode.utils import get_webhook_url
Expand Down Expand Up @@ -345,3 +350,48 @@ def statistics(self, request, *args, **kwargs):
response = ScannableURI.objects.statistics()
return Response(response)


@require_POST
@csrf_exempt
def send_scan_notification(request, key):
try:
json_data = json.loads(request.body.decode("utf-8"))
except json.JSONDecodeError:
raise http.Http404

user_uuid = signing.loads(key)
user = http.get_object_or_404(PurldbUser, uuid=user_uuid)

results = json_data.get('results')
summary = json_data.get('summary')
project_data = json_data.get('project')
extra_data = project_data.get('extra_data')
scannable_uri_uuid = extra_data.get('scannable_uri_uuid')

# Save results to temporary files
scan_results_location = get_temp_file(
file_name='scan_results',
extension='.json'
)
scan_summary_location = get_temp_file(
file_name='scan_summary',
extension='.json'
)

with open(scan_results_location, 'wb') as f:
json.dump(results, f)

with open(scan_summary_location, 'wb') as f:
json.dump(summary, f)

scannable_uri = http.get_object_or_404(ScannableURI, uuid=scannable_uri_uuid)
scannable_uri.process_scan_results(
scan_results_location=scan_results_location,
scan_summary_location=scan_summary_location,
project_extra_data=extra_data
)
msg = {
'status': f'scan results for scannable_uri {scannable_uri.uuid} '
'have been queued for indexing'
}
return Response(msg)
4 changes: 4 additions & 0 deletions purldb_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from matchcode.api import ApproximateDirectoryContentIndexViewSet
from matchcode.api import ApproximateDirectoryStructureIndexViewSet
from minecode.api import ScannableURIViewSet
from minecode.api import send_scan_notification
from packagedb.api import CollectViewSet
from packagedb.api import PackageSetViewSet
from packagedb.api import PackageUpdateSet
Expand Down Expand Up @@ -52,4 +53,7 @@
path("", RedirectView.as_view(url="api/")),
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
path('api/docs/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
path(
'api/send_scan_notification/<str:key>/', send_scan_notification, name='send_scan_notification'
),
]

0 comments on commit 8ee69b7

Please sign in to comment.