Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions scanpipe/pipes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
logger = logging.getLogger("scanpipe.pipes")


class CodebaseResourcesExistError(Exception):
"""The project already has codebase resources."""


def make_codebase_resource(project, location, save=True, **extra_fields):
"""
Create a CodebaseResource instance in the database for the given ``project``.
Expand Down Expand Up @@ -138,7 +142,15 @@ def collect_and_create_codebase_resources(project, batch_size=5000):

The default ``batch_size`` can be overridden, although the benefits of a value
greater than 5000 objects are usually not significant.

Raise a ``CodebaseResourcesExistError`` if the project already has resources.
"""
if project.codebaseresources.exists():
raise CodebaseResourcesExistError(
"Codebase resources already exist for this project. "
"Reset the project before running this pipeline again."
)

model_class = CodebaseResource
objs = yield_resources_from_codebase(project)

Expand Down
2 changes: 1 addition & 1 deletion scanpipe/tests/pipes/test_d2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,14 +858,14 @@ def test_scanpipe_pipes_d2d_map_jvm_to_class_source_file_attribute(self):
# The `Tasks.class` is from lombok-1.18.42.jar
# Ref: https://github.com/aboutcode-org/scancode.io/issues/1993
copy_input(self.data / "jvm" / "Tasks.class", to_dir)
pipes.collect_and_create_codebase_resources(self.project1)

from1 = make_resource_file(
self.project1,
path="from/lombok/delombok/ant/DelombokTask.java",
extra_data={"java_package": "lombok.delombok.ant"},
)

pipes.collect_and_create_codebase_resources(self.project1)
to1 = self.project1.codebaseresources.get(
path="to/lombok/delombok/ant/Tasks.class"
)
Expand Down
14 changes: 14 additions & 0 deletions scanpipe/tests/pipes/test_pipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,17 @@ def test_scanpipe_pipes_collect_and_create_codebase_resources(self):
self.assertEqual("from", from_resource.tag)
to_resource = p1.codebaseresources.get(path="to/a.txt")
self.assertEqual("to", to_resource.tag)

def test_scanpipe_pipes_collect_and_create_codebase_resources_existing(self):
p1 = Project.objects.create(name="Analysis")
input_location = self.data / "codebase" / "a.txt"
copy_input(input_location, p1.codebase_path)
pipes.collect_and_create_codebase_resources(p1)

expected = (
"Codebase resources already exist for this project. "
"Reset the project before running this pipeline again."
)
with self.assertRaisesMessage(pipes.CodebaseResourcesExistError, expected):
pipes.collect_and_create_codebase_resources(p1)
self.assertEqual(1, p1.codebaseresources.count())
Loading