Skip to content

Commit e111395

Browse files
nnobelismnonnenmacher
authored andcommitted
feat(analyzer): Honor includes to the PackageManager
Note that the `skipExcluded` configuration property is not supported as for the excludes, for the sake of simplicity. Signed-off-by: Nicolas Nobelis <[email protected]>
1 parent 6ca7dd6 commit e111395

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

analyzer/src/funTest/kotlin/PackageManagerFunTest.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ import io.kotest.matchers.should
3333
import java.io.File
3434

3535
import org.ossreviewtoolkit.model.config.Excludes
36+
import org.ossreviewtoolkit.model.config.Includes
3637
import org.ossreviewtoolkit.model.config.PathExclude
3738
import org.ossreviewtoolkit.model.config.PathExcludeReason
39+
import org.ossreviewtoolkit.model.config.config.PathInclude
40+
import org.ossreviewtoolkit.model.config.config.PathIncludeReason
3841
import org.ossreviewtoolkit.plugins.api.PluginConfig
3942
import org.ossreviewtoolkit.utils.common.div
4043

@@ -184,6 +187,26 @@ class PackageManagerFunTest : WordSpec({
184187
managedFilesById["SBT"] should containExactly("sbt/build.sbt")
185188
}
186189

190+
"take path includes into account" {
191+
val tempDir = "test/"
192+
val definitionFilesWithExcludes = definitionFiles +
193+
listOf("pom.xml", "build.gradle", "build.sbt").map { "$tempDir$it" }
194+
val rootDir = tempdir()
195+
definitionFilesWithExcludes.writeFiles(rootDir)
196+
197+
val pathInclude = PathInclude("$tempDir**", PathIncludeReason.SOURCE_OF)
198+
val includes = Includes(paths = listOf(pathInclude))
199+
200+
val managedFilesById = PackageManager.findManagedFiles(rootDir, packageManagers, includes = includes)
201+
.groupById(rootDir).toSortedMap(String.CASE_INSENSITIVE_ORDER)
202+
203+
managedFilesById["GradleInspector"] should containExactly(
204+
"test/build.gradle"
205+
)
206+
managedFilesById["Maven"] should containExactly("test/pom.xml")
207+
managedFilesById["SBT"] should containExactly("test/build.sbt")
208+
}
209+
187210
"handle specific excluded definition files" {
188211
val pathExclude = PathExclude("gradle-groovy/build.gradle", PathExcludeReason.OTHER)
189212
val excludes = Excludes(paths = listOf(pathExclude))
@@ -196,6 +219,18 @@ class PackageManagerFunTest : WordSpec({
196219
)
197220
}
198221

222+
"handle specific included definition files" {
223+
val pathInclude = PathInclude("gradle-groovy/build.gradle", PathIncludeReason.SOURCE_OF)
224+
val includes = Includes(paths = listOf(pathInclude))
225+
226+
val managedFiles = PackageManager.findManagedFiles(projectDir, packageManagers, includes = includes)
227+
val managedFilesById = managedFiles.groupById(projectDir)
228+
229+
managedFilesById["GradleInspector"] should containExactly(
230+
"gradle-groovy/build.gradle"
231+
)
232+
}
233+
199234
"fail if the provided file is not a directory" {
200235
shouldThrow<IllegalArgumentException> {
201236
PackageManager.findManagedFiles(projectDir / "pom.xml", packageManagers)

analyzer/src/main/kotlin/Analyzer.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import kotlinx.coroutines.withContext
3636
import org.apache.logging.log4j.kotlin.logger
3737

3838
import org.ossreviewtoolkit.analyzer.PackageManager.Companion.excludes
39+
import org.ossreviewtoolkit.analyzer.PackageManager.Companion.includes
3940
import org.ossreviewtoolkit.downloader.VersionControlSystem
4041
import org.ossreviewtoolkit.model.AnalyzerResult
4142
import org.ossreviewtoolkit.model.AnalyzerRun
@@ -96,7 +97,8 @@ class Analyzer(private val config: AnalyzerConfiguration, private val labels: Ma
9697
PackageManager.findManagedFiles(
9798
absoluteProjectPath,
9899
distinctPackageManagers,
99-
config.excludes(repositoryConfiguration)
100+
config.excludes(repositoryConfiguration),
101+
config.includes(repositoryConfiguration)
100102
)
101103
}.mapNotNull { (manager, files) ->
102104
val mappedFiles = manager.mapDefinitionFiles(absoluteProjectPath, files, config)
@@ -175,6 +177,8 @@ class Analyzer(private val config: AnalyzerConfiguration, private val labels: Ma
175177
val excludes = config.excludes(info.repositoryConfiguration)
176178

177179
runBlocking {
180+
// The excludes are passed to allow the package managers to take in account the scope excludes. Since there
181+
// is no scope includes, the includes are not passed to the package managers.
178182
info.managedFiles.entries.map { (manager, files) ->
179183
PackageManagerRunner(
180184
manager = manager,

analyzer/src/main/kotlin/PackageManager.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import org.ossreviewtoolkit.model.VcsInfo
3838
import org.ossreviewtoolkit.model.VcsType
3939
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
4040
import org.ossreviewtoolkit.model.config.Excludes
41+
import org.ossreviewtoolkit.model.config.Includes
4142
import org.ossreviewtoolkit.model.config.PackageManagerConfiguration
4243
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
4344
import org.ossreviewtoolkit.model.createAndLogIssue
@@ -84,7 +85,8 @@ abstract class PackageManager(val projectType: String) : Plugin {
8485
fun findManagedFiles(
8586
directory: File,
8687
packageManagers: Collection<PackageManager>,
87-
excludes: Excludes = Excludes.EMPTY
88+
excludes: Excludes = Excludes.EMPTY,
89+
includes: Includes = Includes.EMPTY
8890
): ManagedProjectFiles {
8991
require(directory.isDirectory) {
9092
"The provided path is not a directory: ${directory.absolutePath}"
@@ -96,6 +98,8 @@ abstract class PackageManager(val projectType: String) : Plugin {
9698
val rootPath = directory.toPath()
9799
val distinctPackageManagers = packageManagers.distinct()
98100

101+
// Note: Even if the directory is not included and other includes are defined, it has to be walked as
102+
// subdirectories or files may be included.
99103
directory.walk().onEnter { dir ->
100104
val dirAsPath = dir.toPath()
101105

@@ -119,7 +123,8 @@ abstract class PackageManager(val projectType: String) : Plugin {
119123
}
120124
}.filter { it.isDirectory }.forEach { dir ->
121125
val filesInCurrentDir = dir.walk().maxDepth(1).filterTo(mutableListOf()) {
122-
it.isFile && !excludes.isPathExcluded(rootPath, it.toPath())
126+
val isIncluded = includes.isPathIncluded(rootPath, it.toPath())
127+
it.isFile && !excludes.isPathExcluded(rootPath, it.toPath()) && isIncluded
123128
}
124129

125130
distinctPackageManagers.forEach { manager ->
@@ -200,6 +205,15 @@ abstract class PackageManager(val projectType: String) : Plugin {
200205
internal fun AnalyzerConfiguration.excludes(repositoryConfiguration: RepositoryConfiguration): Excludes =
201206
repositoryConfiguration.excludes.takeIf { skipExcluded } ?: Excludes.EMPTY
202207

208+
/**
209+
* Return an [Includes] instance to be applied during analysis based on the given [repositoryConfiguration].
210+
* If this [AnalyzerConfiguration] has the [AnalyzerConfiguration.skipExcluded] flag set to true, the
211+
* includes configured in [repositoryConfiguration] are actually applied. Otherwise, return an empty [Includes]
212+
* object. This means that all dependencies are collected, and includes are applied later on the report level.
213+
*/
214+
internal fun AnalyzerConfiguration.includes(repositoryConfiguration: RepositoryConfiguration): Includes =
215+
repositoryConfiguration.includes.takeIf { skipExcluded } ?: Includes.EMPTY
216+
203217
/**
204218
* Check whether the given [path] interpreted relatively against [root] is matched by a path exclude in this
205219
* [Excludes] object.

0 commit comments

Comments
 (0)