Skip to content

Commit ea51998

Browse files
committed
Check if a Scan is already available before submitting #387
Signed-off-by: tdruez <[email protected]>
1 parent b91b90d commit ea51998

File tree

3 files changed

+22
-31
lines changed

3 files changed

+22
-31
lines changed

component_catalog/tests/test_scancodeio.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
from component_catalog.models import Package
1818
from component_catalog.tests import make_package
1919
from dejacode_toolkit.scancodeio import ScanCodeIO
20-
from dejacode_toolkit.scancodeio import check_for_existing_scan_workaround
2120
from dejacode_toolkit.scancodeio import get_hash_uid
2221
from dejacode_toolkit.scancodeio import get_notice_text_from_key_files
22+
from dejacode_toolkit.scancodeio import update_package_from_existing_scan_data
2323
from dje.models import Dataspace
2424
from dje.models import History
2525
from dje.tasks import scancodeio_submit_scan
@@ -67,9 +67,7 @@ def test_scancodeio_submit_scan_task(self, mock_submit_scan, mock_request_head):
6767

6868
expected = [
6969
mock.call("http://okurl.com", user_uuid, dataspace_uuid),
70-
mock.call().__bool__(),
7170
mock.call("https://okurl2.com", user_uuid, dataspace_uuid),
72-
mock.call().__bool__(),
7371
]
7472
self.assertEqual(expected, mock_submit_scan.mock_calls)
7573

@@ -334,19 +332,13 @@ def test_scancodeio_get_notice_text_from_key_files(self):
334332
self.assertEqual("", notice_text)
335333

336334
@mock.patch("component_catalog.models.Package.update_from_scan")
337-
def test_scancodeio_check_for_existing_scan_workaround(self, mock_update_from_scan):
335+
def test_scancodeio_update_package_from_existing_scan_data(self, mock_update_from_scan):
338336
mock_update_from_scan.return_value = ["updated_field"]
339337
download_url = self.package1.download_url
340338
user = self.basic_user
341339

342-
response_json = None
343-
results = check_for_existing_scan_workaround(response_json, download_url, user)
340+
results = update_package_from_existing_scan_data("unknown_url", user)
344341
self.assertIsNone(results)
345342

346-
response_json = {"success": True}
347-
results = check_for_existing_scan_workaround(response_json, download_url, user)
348-
self.assertIsNone(results)
349-
350-
response_json = {"name": "project with this name already exists."}
351-
results = check_for_existing_scan_workaround(response_json, download_url, user)
343+
results = update_package_from_existing_scan_data(download_url, user)
352344
self.assertEqual(["updated_field"], results)

dejacode_toolkit/scancodeio.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -556,22 +556,15 @@ def get_notice_text_from_key_files(scan_summary, separator="\n\n---\n\n"):
556556
return notice_text
557557

558558

559-
def check_for_existing_scan_workaround(response_json, uri, user):
559+
def update_package_from_existing_scan_data(uri, user):
560560
"""
561-
Workaroud the case where the Scan already exisit on the ScanCode.io side before
561+
Workaroud the case where the Scan already exisits on the ScanCode.io side before
562562
the package is created on the DejaCode side.
563563
This can happen if the package is deleted then re-created from the same user
564564
providing the same download URL.
565565
"""
566-
if not response_json or not isinstance(response_json, dict):
567-
return
568-
569-
already_exists_message = "project with this name already exists."
570-
already_exists = already_exists_message in response_json.get("name", [])
571-
572-
if already_exists:
573-
Package = apps.get_model("component_catalog", "package")
574-
package = Package.objects.get_or_none(download_url=uri, dataspace=user.dataspace)
575-
if package:
576-
updated_fields = package.update_from_scan(user)
577-
return updated_fields
566+
Package = apps.get_model("component_catalog", "package")
567+
package = Package.objects.get_or_none(download_url=uri, dataspace=user.dataspace)
568+
if package:
569+
updated_fields = package.update_from_scan(user)
570+
return updated_fields

dje/tasks.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from django_rq import job
2121

2222
from dejacode_toolkit.scancodeio import ScanCodeIO
23-
from dejacode_toolkit.scancodeio import check_for_existing_scan_workaround
23+
from dejacode_toolkit.scancodeio import update_package_from_existing_scan_data
2424
from dje.utils import is_available
2525

2626
logger = logging.getLogger(__name__)
@@ -109,11 +109,17 @@ def scancodeio_submit_scan(uris, user_uuid, dataspace_uuid):
109109

110110
scancodeio = ScanCodeIO(user.dataspace)
111111
for uri in uris:
112-
if is_available(uri):
113-
response_json = scancodeio.submit_scan(uri, user_uuid, dataspace_uuid)
114-
check_for_existing_scan_workaround(response_json, uri, user)
115-
else:
112+
if not is_available(uri):
116113
logger.info(f'uri="{uri}" is not reachable.')
114+
continue
115+
116+
# Check if a Scan is already available in ScanCode.io for this URI.
117+
existing_project = scancodeio.get_project_info(download_url=uri)
118+
if existing_project:
119+
logger.info(f'Update the local uri="{uri}" package from available Scan data.')
120+
update_package_from_existing_scan_data(uri, user)
121+
else:
122+
scancodeio.submit_scan(uri, user_uuid, dataspace_uuid)
117123

118124

119125
@job("default", timeout="3h")

0 commit comments

Comments
 (0)