Skip to content

Commit fc85c21

Browse files
feat: Added serialize method to CodebaseResource class
Signed-off-by: abanoubfarhan <[email protected]>
1 parent 471f7a6 commit fc85c21

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

scanpipe/models.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -2855,7 +2855,7 @@ def parent(self, codebase=None):
28552855
"""
28562856
parent_path = self.parent_path()
28572857
return parent_path and self.project.codebaseresources.get(path=parent_path)
2858-
2858+
28592859
def siblings(self, codebase=None):
28602860
"""
28612861
Return a sequence of sibling Resource objects for this Resource
@@ -3078,6 +3078,18 @@ def as_spdx(self):
30783078
types=self.get_spdx_types(),
30793079
)
30803080

3081+
def serialize(self):
3082+
"""
3083+
Return a mapping of represting this CodebaseResource and it's data in a form
3084+
that can be serialized to JSON, YAML, etc. it can be used to reconstruct
3085+
the resource
3086+
"""
3087+
serializable = defaultdict(dict)
3088+
serializable["name"] = self.name
3089+
serializable["type"] = self.type.value
3090+
if self.location:
3091+
serializable["location"] = self.location
3092+
return dict(serializable)
30813093

30823094
class CodebaseRelation(
30833095
UUIDPKModel,

scanpipe/tests/test_models.py

+18
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,24 @@ def test_scanpipe_codebase_resource_ancestors(self):
19171917
"asgiref-3.3.0-py3-none-any.whl-extract/asgiref",
19181918
]
19191919
self.assertEqual(expected, [resource.path for resource in ancestors])
1920+
1921+
def test_scanpipe_codebase_resource_serialize(self):
1922+
resource1 = make_resource_file(self.project1, path="pathtoresource/resource1")
1923+
expected = {
1924+
"location": resource1.location,
1925+
"type": "file",
1926+
"name": "resource1",
1927+
}
1928+
self.assertEqual(expected, resource1.serialize())
1929+
1930+
resource2 = make_resource_directory(self.project1, path="pathtodir/resource2")
1931+
expected = {
1932+
"location": resource2.location,
1933+
"type": "directory",
1934+
"name": "resource2",
1935+
}
1936+
self.assertEqual(expected, resource2.serialize())
1937+
19201938

19211939
def test_scanpipe_codebase_resource_children(self):
19221940
path = "asgiref-3.3.0-py3-none-any.whl-extract"

0 commit comments

Comments
 (0)