Skip to content

Commit 5f2a9bb

Browse files
authored
fix: raise a clear error when rerunning a pipeline on existing resources #1712 (#2367)
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent 6e34ff3 commit 5f2a9bb

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎scanpipe/pipes/__init__.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
logger = logging.getLogger("scanpipe.pipes")
4747

4848

49+
class CodebaseResourcesExistError(Exception):
50+
"""The project already has codebase resources."""
51+
52+
4953
def make_codebase_resource(project, location, save=True, **extra_fields):
5054
"""
5155
Create a CodebaseResource instance in the database for the given ``project``.
@@ -138,7 +142,15 @@ def collect_and_create_codebase_resources(project, batch_size=5000):
138142
139143
The default ``batch_size`` can be overridden, although the benefits of a value
140144
greater than 5000 objects are usually not significant.
145+
146+
Raise a ``CodebaseResourcesExistError`` if the project already has resources.
141147
"""
148+
if project.codebaseresources.exists():
149+
raise CodebaseResourcesExistError(
150+
"Codebase resources already exist for this project. "
151+
"Reset the project before running this pipeline again."
152+
)
153+
142154
model_class = CodebaseResource
143155
objs = yield_resources_from_codebase(project)
144156

‎scanpipe/tests/pipes/test_d2d.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,14 +858,14 @@ def test_scanpipe_pipes_d2d_map_jvm_to_class_source_file_attribute(self):
858858
# The `Tasks.class` is from lombok-1.18.42.jar
859859
# Ref: https://github.com/aboutcode-org/scancode.io/issues/1993
860860
copy_input(self.data / "jvm" / "Tasks.class", to_dir)
861+
pipes.collect_and_create_codebase_resources(self.project1)
861862

862863
from1 = make_resource_file(
863864
self.project1,
864865
path="from/lombok/delombok/ant/DelombokTask.java",
865866
extra_data={"java_package": "lombok.delombok.ant"},
866867
)
867868

868-
pipes.collect_and_create_codebase_resources(self.project1)
869869
to1 = self.project1.codebaseresources.get(
870870
path="to/lombok/delombok/ant/Tasks.class"
871871
)

‎scanpipe/tests/pipes/test_pipes.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,3 +642,17 @@ def test_scanpipe_pipes_collect_and_create_codebase_resources(self):
642642
self.assertEqual("from", from_resource.tag)
643643
to_resource = p1.codebaseresources.get(path="to/a.txt")
644644
self.assertEqual("to", to_resource.tag)
645+
646+
def test_scanpipe_pipes_collect_and_create_codebase_resources_existing(self):
647+
p1 = Project.objects.create(name="Analysis")
648+
input_location = self.data / "codebase" / "a.txt"
649+
copy_input(input_location, p1.codebase_path)
650+
pipes.collect_and_create_codebase_resources(p1)
651+
652+
expected = (
653+
"Codebase resources already exist for this project. "
654+
"Reset the project before running this pipeline again."
655+
)
656+
with self.assertRaisesMessage(pipes.CodebaseResourcesExistError, expected):
657+
pipes.collect_and_create_codebase_resources(p1)
658+
self.assertEqual(1, p1.codebaseresources.count())

0 commit comments

Comments
 (0)