Skip to content

Codebaseresource features mapping with commoncode.resource #1585 #1638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions scanpipe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import uuid
from collections import Counter
from collections import defaultdict
from collections import deque
from contextlib import suppress
from itertools import groupby
from operator import itemgetter
Expand Down Expand Up @@ -2875,6 +2876,24 @@ def descendants(self):
"""
return self.project.codebaseresources.filter(path__startswith=f"{self.path}/")

def ancestors(self):
"""
Return a QuerySet of ancestors CodebaseResource objects using a database query
on the current CodebaseResource `path`. The current CodebaseResource is not
included
"""
if not self.has_parent():
return []
ancestors = deque()
current = self.parent()
ancestors_appendleft = ancestors.appendleft

while current:
ancestors_appendleft(current)
current = current.parent()

return list(ancestors)

def children(self, codebase=None):
"""
Return a QuerySet of direct children CodebaseResource objects using a
Expand Down
12 changes: 12 additions & 0 deletions scanpipe/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1906,6 +1906,18 @@ def test_scanpipe_codebase_resource_descendants(self):
]
self.assertEqual(expected, sorted([resource.path for resource in descendants]))

def test_scanpipe_codebase_resource_ancestors(self):
path = "asgiref-3.3.0-py3-none-any.whl-extract/asgiref/__init__.py"
resource = self.project_asgiref.codebaseresources.get(path=path)
ancestors = list(resource.ancestors())
self.assertEqual(2, len(ancestors))
self.assertNotIn(resource.path, ancestors)
expected = [
"asgiref-3.3.0-py3-none-any.whl-extract",
"asgiref-3.3.0-py3-none-any.whl-extract/asgiref",
]
self.assertEqual(expected, [resource.path for resource in ancestors])

def test_scanpipe_codebase_resource_children(self):
path = "asgiref-3.3.0-py3-none-any.whl-extract"
resource = self.project_asgiref.codebaseresources.get(path=path)
Expand Down