@@ -28,6 +28,7 @@ import org.ossreviewtoolkit.model.PackageLinkage
2828import org.ossreviewtoolkit.model.PackageReference
2929import org.ossreviewtoolkit.model.Project
3030import org.ossreviewtoolkit.model.config.Excludes
31+ import org.ossreviewtoolkit.model.config.Includes
3132
3233/* *
3334 * An object that supports the conversion of [AnalyzerResult]s to the dependency graph format.
@@ -45,42 +46,50 @@ import org.ossreviewtoolkit.model.config.Excludes
4546object DependencyGraphConverter {
4647 /* *
4748 * Convert the given [result], so that all dependencies are represented as dependency graphs. During conversion,
48- * apply the scope excludes defined in [excludes]. If the [result] already contains a dependency graph that covers
49- * all projects, it is returned as is.
49+ * apply the scope includes/ excludes defined in [includes] and [ excludes]. If the [result] already contains a
50+ * dependency graph that covers all projects, it is returned as is.
5051 */
51- fun convert (result : AnalyzerResult , excludes : Excludes = Excludes .EMPTY ): AnalyzerResult {
52+ fun convert (
53+ result : AnalyzerResult ,
54+ excludes : Excludes = Excludes .EMPTY ,
55+ includes : Includes = Includes .EMPTY
56+ ): AnalyzerResult {
5257 val projectsToConvert = result.projectsWithScopes()
5358 if (projectsToConvert.isEmpty()) return result
5459
55- val graphs = buildDependencyGraphs(projectsToConvert, excludes)
60+ val graphs = buildDependencyGraphs(projectsToConvert, excludes, includes )
5661 val allGraphs = result.dependencyGraphs + graphs
5762
58- val filteredPackages = if (excludes.scopes.isEmpty()) {
63+ val filteredPackages = if (includes.scopes.isEmpty() && excludes.scopes.isEmpty()) {
5964 result.packages
6065 } else {
6166 filterExcludedPackages(allGraphs.values, result.packages)
6267 }
6368
6469 return result.copy(
6570 dependencyGraphs = allGraphs,
66- projects = result.projects.mapTo(mutableSetOf ()) { it.convertToScopeNames(excludes) },
71+ projects = result.projects.mapTo(mutableSetOf ()) { it.convertToScopeNames(excludes, includes ) },
6772 packages = filteredPackages
6873 )
6974 }
7075
7176 /* *
72- * Create and populate [DependencyGraphBuilder]s for the given [projects] taking [excludes] into account. The
73- * resulting map contains one fully initialized graph builder for each package manager involved which can be
74- * used to obtain the [DependencyGraph] and all packages.
77+ * Create and populate [DependencyGraphBuilder]s for the given [projects] taking [includes] and [excludes] into
78+ * account. The resulting map contains one fully initialized graph builder for each package manager involved which
79+ * can be used to obtain the [DependencyGraph] and all packages.
7580 */
76- private fun buildDependencyGraphs (projects : Set <Project >, excludes : Excludes ): Map <String , DependencyGraph > {
81+ private fun buildDependencyGraphs (
82+ projects : Set <Project >,
83+ excludes : Excludes ,
84+ includes : Includes
85+ ): Map <String , DependencyGraph > {
7786 val graphs = mutableMapOf<String , DependencyGraph >()
7887
7988 projects.groupBy { it.id.type }.forEach { (type, projectsForType) ->
8089 val builder = DependencyGraphBuilder (ScopesDependencyHandler )
8190
8291 projectsForType.forEach { project ->
83- project.scopes.filterNot { excludes.isScopeExcluded (it.name) }.forEach { scope ->
92+ project.scopes.filter { isScopeIncluded (it.name, excludes, includes ) }.forEach { scope ->
8493 val scopeName = DependencyGraph .qualifyScope(project.id, scope.name)
8594 scope.dependencies.forEach { dependency ->
8695 builder.addDependency(scopeName, dependency)
@@ -96,7 +105,7 @@ object DependencyGraphConverter {
96105
97106 /* *
98107 * Filter out all [packages] that are no longer referenced by one of the given [dependency graphs][graphs]. These
99- * packages have been subject of scope excludes.
108+ * packages have been subject of scope excludes or not included by scope includes .
100109 */
101110 private fun filterExcludedPackages (
102111 graphs : Collection <DependencyGraph >,
@@ -115,12 +124,14 @@ object DependencyGraphConverter {
115124
116125 /* *
117126 * Convert the dependency representation used by this [Project] to the dependency graph format, i.e. a set of
118- * scope names. Use the given [excludes] to filter out excluded scopes. Return the same project if this format is
119- * already in use.
127+ * scope names. Use the given [excludes] and [includes] to filter out excluded scopes. Return the same project if
128+ * this format is already in use.
120129 */
121- private fun Project.convertToScopeNames (excludes : Excludes ): Project =
130+ private fun Project.convertToScopeNames (excludes : Excludes , includes : Includes ): Project =
122131 takeIf { scopeNames != null } ? : copy(
123- scopeNames = scopes.filterNot { excludes.isScopeExcluded(it.name) }.mapTo(mutableSetOf ()) { it.name },
132+ scopeNames = scopes.filter {
133+ isScopeIncluded(it.name, excludes, includes)
134+ }.mapTo(mutableSetOf ()) { it.name },
124135 scopeDependencies = null
125136 )
126137
@@ -143,5 +154,5 @@ object DependencyGraphConverter {
143154 }
144155}
145156
146- fun AnalyzerResult.convertToDependencyGraph (excludes : Excludes = Excludes .EMPTY ) =
147- DependencyGraphConverter .convert(this , excludes)
157+ fun AnalyzerResult.convertToDependencyGraph (excludes : Excludes = Excludes .EMPTY , includes : Includes = Includes . EMPTY ) =
158+ DependencyGraphConverter .convert(this , excludes, includes )
0 commit comments