Skip to content

Commit c23b526

Browse files
committed
Remove duplicated code #504
Signed-off-by: Jono Yang <[email protected]>
1 parent e471385 commit c23b526

File tree

2 files changed

+9
-169
lines changed

2 files changed

+9
-169
lines changed

minecode/api.py

+7-167
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from django.shortcuts import get_object_or_404
1717
from django.utils import timezone
1818
from django.views.decorators.csrf import csrf_exempt
19-
from django.views.decorators.http import require_POST
2019

2120
from packageurl import PackageURL
2221
from rest_framework import serializers, status, viewsets
@@ -118,7 +117,7 @@ def get_next_download_url(self, request, *args, **kwargs):
118117
scannable_uri = ScannableURI.objects.get_next_scannable()
119118
if scannable_uri:
120119
user = self.request.user
121-
webhook_url = get_webhook_url("send_scan_notification", user.id)
120+
webhook_url = get_webhook_url('index_package_scan', user.id)
122121
response = {
123122
'scannable_uri_uuid': scannable_uri.uuid,
124123
'download_url': scannable_uri.uri,
@@ -140,16 +139,10 @@ def get_next_download_url(self, request, *args, **kwargs):
140139
@action(detail=True, methods=['post'])
141140
def update_status(self, request, *args, **kwargs):
142141
"""
143-
Update the status of a ScannableURI with UUID of `scannable_uri_uuid`
144-
with `scan_status`
142+
Update the status of a ScannableURI with `scan_status`
145143
146144
If `scan_status` is 'failed', then a `scan_log` string is expected and
147145
should contain the error messages for that scan.
148-
149-
If `scan_status` is 'scanned', then a `scan_results_file`,
150-
`scan_summary_file`, and `project_extra_data` mapping are expected.
151-
`scan_results_file`, `scan_summary_file`, and `project_extra_data` are
152-
then used to update Package data and its Resources.
153146
"""
154147
scan_status = request.data.get('scan_status')
155148
if not scan_status:
@@ -161,13 +154,6 @@ def update_status(self, request, *args, **kwargs):
161154
scannable_uri = self.get_object()
162155
scannable_uri_uuid = scannable_uri.uuid
163156
scannable_uri_status = ScannableURI.SCAN_STATUSES_BY_CODE.get(scannable_uri.scan_status)
164-
scan_status_code = ScannableURI.SCAN_STATUS_CODES_BY_SCAN_STATUS.get(scan_status)
165-
166-
if not scan_status_code:
167-
msg = {
168-
'error': f'invalid scan_status: {scan_status}'
169-
}
170-
return Response(msg, status=status.HTTP_400_BAD_REQUEST)
171157

172158
if scannable_uri.scan_status in [
173159
ScannableURI.SCAN_INDEXED,
@@ -194,166 +180,20 @@ def update_status(self, request, *args, **kwargs):
194180
scannable_uri.scan_status = ScannableURI.SCAN_FAILED
195181
scannable_uri.wip_date = None
196182
scannable_uri.save()
197-
msg = {
198-
'status': f'updated scannable_uri {scannable_uri_uuid} scan_status to {scan_status}'
199-
}
200-
201-
elif scan_status == 'scanned':
202-
scan_results_file = request.data.get('scan_results_file')
203-
scan_summary_file = request.data.get('scan_summary_file')
204-
project_extra_data = request.data.get('project_extra_data')
205-
206-
# Save results to temporary files
207-
scan_results_location = get_temp_file(
208-
file_name='scan_results',
209-
extension='.json'
210-
)
211-
scan_summary_location = get_temp_file(
212-
file_name='scan_summary',
213-
extension='.json'
214-
)
215-
with open(scan_results_location, 'wb') as f:
216-
f.write(scan_results_file.read())
217-
with open(scan_summary_location, 'wb') as f:
218-
f.write(scan_summary_file.read())
219-
220-
scannable_uri.process_scan_results(
221-
scan_results_location=scan_results_location,
222-
scan_summary_location=scan_summary_location,
223-
project_extra_data=project_extra_data
224-
)
225-
msg = {
226-
'status': f'scan results for scannable_uri {scannable_uri_uuid} '
227-
'have been queued for indexing'
228-
}
229-
230-
return Response(msg)
231-
232-
@action(detail=True, methods=['post'])
233-
def update_status(self, request, *args, **kwargs):
234-
"""
235-
Update the status of a ScannableURI with UUID of `scannable_uri_uuid`
236-
with `scan_status`
237-
238-
If `scan_status` is 'failed', then a `scan_log` string is expected and
239-
should contain the error messages for that scan.
240-
241-
If `scan_status` is 'scanned', then a `scan_results_file`,
242-
`scan_summary_file`, and `project_extra_data` mapping are expected.
243-
`scan_results_file`, `scan_summary_file`, and `project_extra_data` are
244-
then used to update Package data and its Resources.
245-
"""
246-
scan_status = request.data.get('scan_status')
247-
if not scan_status:
248-
response = {
249-
'error': 'missing scan_status'
250-
}
251-
return Response(response, status=status.HTTP_400_BAD_REQUEST)
252-
253-
scannable_uri = self.get_object()
254-
scannable_uri_uuid = scannable_uri.uuid
255-
scannable_uri_status = ScannableURI.SCAN_STATUSES_BY_CODE.get(scannable_uri.scan_status)
256-
scan_status_code = ScannableURI.SCAN_STATUS_CODES_BY_SCAN_STATUS.get(scan_status)
257-
258-
if not scan_status_code:
259-
msg = {
260-
'error': f'invalid scan_status: {scan_status}'
261-
}
262-
return Response(msg, status=status.HTTP_400_BAD_REQUEST)
263-
264-
if scannable_uri.scan_status in [
265-
ScannableURI.SCAN_INDEXED,
266-
ScannableURI.SCAN_FAILED,
267-
ScannableURI.SCAN_TIMEOUT,
268-
ScannableURI.SCAN_INDEX_FAILED,
269-
]:
270-
response = {
271-
'error': f'cannot update status for scannable_uri {scannable_uri_uuid}: '
272-
f'scannable_uri has finished with status "{scannable_uri_status}"'
273-
}
274-
return Response(response, status=status.HTTP_400_BAD_REQUEST)
275-
276-
if scan_status == scannable_uri_status:
277183
response = {
278-
'error': f'cannot update status for scannable_uri {scannable_uri_uuid}: '
279-
f'scannable_uri status is already "{scannable_uri_status}"'
280-
}
281-
return Response(response, status=status.HTTP_400_BAD_REQUEST)
282-
283-
if scan_status == 'failed':
284-
scan_log = request.data.get('scan_log')
285-
scannable_uri.scan_error = scan_log
286-
scannable_uri.scan_status = ScannableURI.SCAN_FAILED
287-
scannable_uri.wip_date = None
288-
scannable_uri.save()
289-
msg = {
290184
'status': f'updated scannable_uri {scannable_uri_uuid} scan_status to {scan_status}'
291185
}
186+
return Response(response)
292187

293-
return Response(msg)
294-
295-
@action(detail=True, methods=['post'])
296-
def index_package_scan(self, request, *args, **kwargs):
297-
scannable_uri = self.get_object()
298-
scannable_uri_uuid = scannable_uri.uuid
299-
if scannable_uri.scan_status in [
300-
ScannableURI.SCAN_INDEXED,
301-
ScannableURI.SCAN_FAILED,
302-
ScannableURI.SCAN_TIMEOUT,
303-
ScannableURI.SCAN_INDEX_FAILED,
304-
]:
305-
response = {
306-
'error': f'cannot index package scan for scannable_uri {scannable_uri_uuid}: '
307-
f'scannable_uri has finished with status "{scannable_uri.status}"'
308-
}
309-
return Response(response, status=status.HTTP_400_BAD_REQUEST)
310-
311-
project_data = request.data.get('project')
312-
results = project_data.get('results')
313-
summary = project_data.get('summary')
314-
extra_data = project_data.get('extra_data')
315-
316-
# Save results to temporary files
317-
scan_results_location = get_temp_file(
318-
file_name='scan_results',
319-
extension='.json'
320-
)
321-
scan_summary_location = get_temp_file(
322-
file_name='scan_summary',
323-
extension='.json'
324-
)
325-
326-
with open(scan_results_location, 'wb') as f:
327-
json.dump(results, f)
328-
329-
with open(scan_summary_location, 'wb') as f:
330-
json.dump(summary, f)
331-
332-
scannable_uri = self.get_object()
333-
scannable_uri.process_scan_results(
334-
scan_results_location=scan_results_location,
335-
scan_summary_location=scan_summary_location,
336-
project_extra_data=extra_data
337-
)
338-
msg = {
339-
'status': f'scan results for scannable_uri {scannable_uri.uuid} '
340-
'have been queued for indexing'
188+
response = {
189+
'error': f'invalid scan_status: {scan_status}'
341190
}
191+
return Response(response, status=status.HTTP_400_BAD_REQUEST)
342192

343-
return Response(msg)
344-
345-
@action(detail=False, methods=['get'])
346-
def statistics(self, request, *args, **kwargs):
347-
"""
348-
Return a scan queue statistics.
349-
"""
350-
response = ScannableURI.objects.statistics()
351-
return Response(response)
352193

353194
@api_view(['POST'])
354-
@require_POST
355195
@csrf_exempt
356-
def send_scan_notification(request, key):
196+
def index_package_scan(request, key):
357197
try:
358198
json_data = json.loads(request.body.decode("utf-8"))
359199
except json.JSONDecodeError:

purldb_project/urls.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from matchcode.api import ApproximateDirectoryContentIndexViewSet
1919
from matchcode.api import ApproximateDirectoryStructureIndexViewSet
2020
from minecode.api import ScannableURIViewSet
21-
from minecode.api import send_scan_notification
21+
from minecode.api import index_package_scan
2222
from packagedb.api import CollectViewSet
2323
from packagedb.api import PackageSetViewSet
2424
from packagedb.api import PackageUpdateSet
@@ -54,6 +54,6 @@
5454
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
5555
path('api/docs/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
5656
path(
57-
'api/send_scan_notification/<str:key>/', send_scan_notification, name='send_scan_notification'
57+
'api/scan_queue/index_package_scan/<str:key>/', index_package_scan, name='index_package_scan'
5858
),
5959
]

0 commit comments

Comments
 (0)