Skip to content

Commit

Permalink
Update the status in save_scan_file_results #726
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Druez <tdruez@nexb.com>
  • Loading branch information
tdruez committed May 26, 2023
1 parent d9ebbf7 commit d24c803
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
9 changes: 8 additions & 1 deletion scanpipe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1562,17 +1562,24 @@ class Meta:
def scan_fields(cls):
return [field.name for field in ScanFieldsModelMixin._meta.get_fields()]

def set_scan_results(self, scan_results):
def set_scan_results(self, scan_results, status=None):
"""
Set the values of the current instance's scan-related fields using
``scan_results``.
This instance status can be updated along the scan results by providing the
optional ``status`` argument.
"""
updated_fields = []
for field_name, value in scan_results.items():
if value and field_name in self.scan_fields():
setattr(self, field_name, value)
updated_fields.append(field_name)

if status:
self.status = status
updated_fields.append("status")

if updated_fields:
self.save(update_fields=updated_fields)

Expand Down
8 changes: 4 additions & 4 deletions scanpipe/pipes/scancode.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ def save_scan_file_results(codebase_resource, scan_results, scan_errors):
Save the resource scan file results in the database.
Create project errors if any occurred during the scan.
"""
status = flag.SCANNED

if scan_errors:
codebase_resource.add_errors(scan_errors)
codebase_resource.status = flag.SCANNED_WITH_ERROR
else:
codebase_resource.status = flag.SCANNED
status = flag.SCANNED_WITH_ERROR

codebase_resource.set_scan_results(scan_results)
codebase_resource.set_scan_results(scan_results, status)


def save_scan_package_results(codebase_resource, scan_results, scan_errors):
Expand Down
3 changes: 2 additions & 1 deletion scanpipe/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,10 +955,11 @@ def test_scanpipe_scan_fields_model_mixin_methods(self):
"name": "name",
"non_resource_field": "value",
}
resource.set_scan_results(scan_results)
resource.set_scan_results(scan_results, status="scanned")
resource.refresh_from_db()
self.assertEqual("", resource.name)
self.assertEqual("mit", resource.detected_license_expression)
self.assertEqual("scanned", resource.status)

resource2 = CodebaseResource.objects.create(project=self.project1, path="file2")
resource2.copy_scan_results(from_instance=resource)
Expand Down

0 comments on commit d24c803

Please sign in to comment.