Skip to content

Commit

Permalink
Update ScannableURI API and model #285
Browse files Browse the repository at this point in the history
Signed-off-by: Jono Yang <[email protected]>
  • Loading branch information
JonoYang committed Feb 26, 2024
1 parent fb9e696 commit 93ad167
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 40 deletions.
58 changes: 34 additions & 24 deletions minecode/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# See https://aboutcode.org for more information about nexB OSS projects.
#

from django.utils import timezone
import json
from django.db import transaction
from packageurl import PackageURL
Expand Down Expand Up @@ -105,10 +106,11 @@ def get_next_download_url(self, request, *args, **kwargs):
scannable_uri = ScannableURI.objects.get_next_scannable()
if scannable_uri:
response = {
'package_uuid': scannable_uri.package.uuid,
'scannable_uri_uuid': scannable_uri.uuid,
'download_url': scannable_uri.uri,
}
scannable_uri.scan_status = ScannableURI.SCAN_SUBMITTED
scannable_uri.scan_date = timezone.now()
scannable_uri.save()
else:
response = {
Expand All @@ -118,30 +120,38 @@ def get_next_download_url(self, request, *args, **kwargs):
return Response(response)

@action(detail=False, methods=["post"])
def submit_scan_results(self, request, *args, **kwargs):
"""
Receive and index completed scan
"""
from packagedb.models import Package

package_uuid = request.data.get('package_uuid')
scan_file = request.data.get('scan_file')

missing = []
if not package_uuid:
missing.append('package_uuid')
if not scan_file:
missing.append('scan_file')
if missing:
msg = ', '.join(missing)
def update_status(self, request, *args, **kwargs):
scannable_uri_uuid = request.data.get('scannable_uri_uuid')
scan_status = request.data.get('scan_status')
if not scannable_uri_uuid:
response = {
'error': f'missing {msg}'
'error': 'missing scannable_uri_uuid'
}
return Response(response)

package = Package.objects.get(uuid=package_uuid)
scan_data= json.load(scan_file)
indexing_errors = index_package_files(package, scan_data, reindex=True)
if indexing_errors:
return Response({'error': f'indexing errors:\n\n{indexing_errors}'})
return Response({'message': 'success'})
scannable_uri = ScannableURI.objects.get(uuid=scannable_uri_uuid)

if scan_status == 'in progress':
scannable_uri.scan_status = ScannableURI.SCAN_IN_PROGRESS
scannable_uri.save()

if scan_status == 'failed':
scan_log = request.data.get('scan_log')
scannable_uri.scan_error = scan_log
scannable_uri.scan_status = ScannableURI.SCAN_FAILED
scannable_uri.wip_date = None
scannable_uri.save()

if scan_status == 'scanned':
scan_file = request.data.get('scan_file')
scannable_uri.scan_status = ScannableURI.SCAN_COMPLETED
package = scannable_uri.package
scan_data= json.load(scan_file)
indexing_errors = index_package_files(package, scan_data, reindex=True)
if indexing_errors:
scannable_uri.scan_status = ScannableURI.SCAN_INDEX_FAILED
scannable_uri.index_error = indexing_errors
else:
scannable_uri.scan_status = ScannableURI.SCAN_INDEXED
scannable_uri.wip_date = None
scannable_uri.save()
26 changes: 10 additions & 16 deletions minecode/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# See https://aboutcode.org for more information about nexB OSS projects.
#


import uuid
from datetime import timedelta
import logging
import sys
Expand Down Expand Up @@ -649,11 +649,15 @@ class ScannableURI(BaseURI):
- update the matching index for the PackageDB as needed with fingerprints from the scan
- set status and timestamps as needed
"""
scan_request_date = models.DateTimeField(
uuid = models.UUIDField(
verbose_name=_("UUID"), default=uuid.uuid4, unique=True, editable=False
)

scan_date = models.DateTimeField(
null=True,
blank=True,
db_index=True,
help_text='Timestamp set to the date when a scan was requested. Used to track scan status.',
help_text='Timestamp set to the date when a scan was taken by a worker',
)

last_status_poll_date = models.DateTimeField(
Expand All @@ -664,12 +668,10 @@ class ScannableURI(BaseURI):
'Used to track the scan polling.',
)

scan_uuid = models.CharField(
max_length=36,
blank=True,
null=True,
scan_project_url = models.CharField(
max_length=2048,
db_index=True,
help_text='UUID of a scan for this URI in ScanCode.io.',
help_text='URL to scan project for this Package',
)

SCAN_NEW = 0
Expand Down Expand Up @@ -708,14 +710,6 @@ class ScannableURI(BaseURI):
help_text='Flag indicating whether or not this URI should be rescanned and reindexed.',
)

scan_uuid = models.CharField(
max_length=36,
blank=True,
null=True,
db_index=True,
help_text='UUID of a scan for this URI in ScanCode.io.',
)

scan_error = models.TextField(
null=True,
blank=True,
Expand Down

0 comments on commit 93ad167

Please sign in to comment.