Skip to content

Commit

Permalink
fix(aosd): Lookup node linkage breadth-first
Browse files Browse the repository at this point in the history
Change finding nodes from depth- to breadth-first for a more compact
implementation. This also fixes a bug of the original implementation
which "swallowed" the return value of `traverse()` calls which resulted
in the `linking` of transitive dependencies to be wrong.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Dec 16, 2024
1 parent 15a6021 commit 3bb2ef4
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions plugins/reporters/aosd/src/main/kotlin/Aosd21Reporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private fun Map<Identifier, IndexedValue<CuratedPackage>>.toComponents(
): Set<Component> =
values.mapTo(mutableSetOf()) { (index, pkg) ->
val dependencies = input.ortResult.getDependencies(pkg.metadata.id, maxLevel = 1, omitExcluded = true)
val node = input.ortResult.dependencyNavigator.findNode(project, pkg.metadata.id)
val node = input.ortResult.dependencyNavigator.findBreadthFirst(project, pkg.metadata.id)

val nonExcludedLicenseInfo = input.licenseInfoResolver.resolveLicenseInfo(pkg.metadata.id).filterExcluded()
val relevantLicenseInfo = nonExcludedLicenseInfo.filter(LicenseView.CONCLUDED_OR_DECLARED_AND_DETECTED)
Expand Down Expand Up @@ -124,24 +124,21 @@ private fun Map<Identifier, IndexedValue<CuratedPackage>>.toComponents(
}
}

private fun DependencyNavigator.findNode(project: Project, id: Identifier): DependencyNode? {
fun traverse(node: DependencyNode): DependencyNode? {
if (node.id == id) return node
private fun DependencyNavigator.findBreadthFirst(project: Project, nodeId: Identifier): DependencyNode? {
fun Sequence<DependencyNode>.findBreadthFirst(id: Identifier): DependencyNode? {
// This also turns the sequence into a list so it can be consumed twice, see below.
val directDependencies = mapTo(mutableListOf()) { it.getStableReference() }

node.visitDependencies { dependencies ->
dependencies.forEach(::traverse)
}

return null
}
directDependencies.find { node -> node.id == id }?.also { return it }

scopeNames(project).forEach { scopeName ->
directDependencies(project, scopeName).forEach { node ->
traverse(node)?.also { return it }
return directDependencies.firstNotNullOfOrNull { node ->
node.visitDependencies { it.findBreadthFirst(id) }
}
}

return null
return scopeNames(project).asSequence().mapNotNull { scopeName ->
directDependencies(project, scopeName).findBreadthFirst(nodeId)
}.firstOrNull()
}

private fun ResolvedLicenseInfo.toSimplifiedCompoundExpression(): SpdxExpression =
Expand Down

0 comments on commit 3bb2ef4

Please sign in to comment.