diff --git a/scanpipe/pipes/d2d.py b/scanpipe/pipes/d2d.py index eda7703e38..a9d7d486b3 100644 --- a/scanpipe/pipes/d2d.py +++ b/scanpipe/pipes/d2d.py @@ -1556,9 +1556,17 @@ def scan_ignored_to_files(project, logger=None): .to_codebase() .filter(status=flag.IGNORED_FROM_CONFIG) ) + + # Capture the specific files id + scan_file_ids = list(scan_files.values_list("id", flat=True)) + + if not scan_file_ids: + return + scancode.scan_for_files(project, scan_files, progress_logger=logger) - project.codebaseresources.files().to_codebase().filter(status=flag.SCANNED).update( + # Revert to the original status value + project.codebaseresources.filter(id__in=scan_file_ids, status=flag.SCANNED).update( status=flag.IGNORED_FROM_CONFIG ) @@ -1573,9 +1581,17 @@ def scan_unmapped_to_files(project, logger=None): .to_codebase() .filter(status=flag.REQUIRES_REVIEW) ) + + # Capture the specific files id + scan_file_ids = list(scan_files.values_list("id", flat=True)) + + if not scan_file_ids: + return + scancode.scan_for_files(project, scan_files, progress_logger=logger) - project.codebaseresources.files().to_codebase().filter(status=flag.SCANNED).update( + # Revert to the original status value + project.codebaseresources.filter(id__in=scan_file_ids, status=flag.SCANNED).update( status=flag.REQUIRES_REVIEW ) diff --git a/scanpipe/tests/pipes/test_d2d.py b/scanpipe/tests/pipes/test_d2d.py index 5ef10243a3..18adc22cae 100644 --- a/scanpipe/tests/pipes/test_d2d.py +++ b/scanpipe/tests/pipes/test_d2d.py @@ -1530,14 +1530,18 @@ def test_scanpipe_pipes_d2d_scan_ignored_to_files(self): ) foo_java.update(status=flag.IGNORED_FROM_CONFIG) + # Create another file that is already scanned but should not be + # reverted + other_scanned = make_resource_file( + self.project1, "to/other_scanned.txt", status=flag.SCANNED + ) + d2d.scan_ignored_to_files(self.project1) foo_java.refresh_from_db() + other_scanned.refresh_from_db() - expected = self.project1.codebaseresources.filter( - status=flag.IGNORED_FROM_CONFIG - ).count() - - self.assertEqual(1, expected) + self.assertEqual(flag.IGNORED_FROM_CONFIG, foo_java.status) + self.assertEqual(flag.SCANNED, other_scanned.status) def test_scan_unmapped_to_files(self): to_dir = ( @@ -1558,14 +1562,18 @@ def test_scan_unmapped_to_files(self): ) foo_java.update(status=flag.REQUIRES_REVIEW) + # Create another file that is already scanned but should not be + # reverted + other_scanned = make_resource_file( + self.project1, "to/other_scanned.txt", status=flag.SCANNED + ) + d2d.scan_unmapped_to_files(self.project1) foo_java.refresh_from_db() + other_scanned.refresh_from_db() - expected = self.project1.codebaseresources.filter( - status=flag.REQUIRES_REVIEW - ).count() - - self.assertEqual(1, expected) + self.assertEqual(flag.REQUIRES_REVIEW, foo_java.status) + self.assertEqual(flag.SCANNED, other_scanned.status) def test_flag_deployed_from_resources_with_missing_license(self): from_dir = (