Skip to content

Commit 0f7e8b0

Browse files
committed
feat: partition SBOM dependencies into direct and deep
1 parent 4a43cc2 commit 0f7e8b0

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

socketsecurity/fossa_compat.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,23 @@ def _build_dependency_entry(package: Package, dependency_paths: list[str]) -> di
414414
}
415415

416416

417+
def _compute_dependency_paths(package: Package, package_lookup: dict[str, Package]) -> list[str]:
418+
"""Stub: filled in by Task 9. For now: package name only."""
419+
return [package.name]
420+
421+
417422
def _partition_dependencies(packages: list[Package]) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
418-
"""Stub: filled in by Tasks 7-9. Returns (direct, deep) lists of Dependency dicts."""
419-
return ([], [])
423+
direct: list[dict[str, Any]] = []
424+
deep: list[dict[str, Any]] = []
425+
package_lookup = {getattr(p, "id", None): p for p in packages if getattr(p, "id", None)}
426+
for package in packages:
427+
paths = _compute_dependency_paths(package, package_lookup)
428+
entry = _build_dependency_entry(package, paths)
429+
if bool(getattr(package, "direct", False)):
430+
direct.append(entry)
431+
else:
432+
deep.append(entry)
433+
return direct, deep
420434

421435

422436
def build_fossa_attribution_payload(diff_report: Diff, config: CliConfig) -> dict[str, Any]:

tests/unit/test_fossa_compat.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,28 @@ def test_attribution_empty_diff_yields_empty_collections():
327327
assert payload["deepDependencies"] == []
328328

329329

330+
def test_attribution_partitions_direct_vs_deep():
331+
pkg_a = Package(
332+
type="pypi", name="a", version="1.0", id="pip+a$1.0",
333+
score={}, alerts=[], direct=True,
334+
)
335+
pkg_b = Package(
336+
type="pypi", name="b", version="1.0", id="pip+b$1.0",
337+
score={}, alerts=[], direct=False,
338+
)
339+
pkg_c = Package(
340+
type="pypi", name="c", version="1.0", id="pip+c$1.0",
341+
score={}, alerts=[], direct=True,
342+
)
343+
diff = Diff(packages={"id-a": pkg_a, "id-b": pkg_b, "id-c": pkg_c})
344+
config = CliConfig.from_args(["--api-token", "test", "--legal-format", "fossa"])
345+
payload = build_fossa_attribution_payload(diff, config)
346+
direct_names = sorted(d["package"] for d in payload["directDependencies"])
347+
deep_names = sorted(d["package"] for d in payload["deepDependencies"])
348+
assert direct_names == ["a", "c"]
349+
assert deep_names == ["b"]
350+
351+
330352
def test_vulnerability_version_ranges_sourced_from_socket_fields():
331353
"""affectedVersionRanges/patchedVersionRanges come from Socket's singular fields, wrapped."""
332354
from socketsecurity.fossa_compat import _build_vulnerability_entry

0 commit comments

Comments
 (0)