Skip to content

Commit

Permalink
PackageConfigurationProvider: Return multiple configurations
Browse files Browse the repository at this point in the history
As ORT supports multiple simultanously used sources for package
configurations, return all package configurations for a package, instead
of only the first one. This allows to amend package configurations from
one source with package configurations from another source. This
scenario will become more important once a provider for the ort-config
repository [1] is added, because then the public configurations can be
combined with internal ones.

For now, keep the requirement of `SimplePackageConfigurationProvider`
and it's subclasses to allow only a single package configuration per
package and provenance. This might be reconsidered later on.

[1]: https://github.com/oss-review-toolkit/ort-config

Signed-off-by: Martin Nonnenmacher <martin.nonnenmacher@bosch.io>
  • Loading branch information
mnonnenmacher committed May 9, 2022
1 parent 9bb2f17 commit 00a8487
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 26 deletions.
10 changes: 5 additions & 5 deletions helper-cli/src/main/kotlin/commands/GetPackageLicensesCommand.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2021 HERE Europe B.V.
* Copyright (C) 2022 Bosch.IO GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -88,12 +89,11 @@ class GetPackageLicensesCommand : CliktCommand(
val packageConfigurationProvider = packageConfigurationOption.createProvider()

val result = scanResults.firstOrNull()?.let { scanResult ->
val packageConfiguration = packageConfigurationProvider.getPackageConfiguration(
packageId, scanResult.provenance
)
val packageConfigurations =
packageConfigurationProvider.getPackageConfigurations(packageId, scanResult.provenance)

val licenseFindingCurations = packageConfiguration?.licenseFindingCurations.orEmpty()
val pathExcludes = packageConfiguration?.pathExcludes.orEmpty()
val licenseFindingCurations = packageConfigurations.flatMap { it.licenseFindingCurations }
val pathExcludes = packageConfigurations.flatMap { it.pathExcludes }

val nonExcludedLicenseFindings = scanResult.summary.licenseFindings.filter { licenseFinding ->
pathExcludes.none { it.matches(licenseFinding.location.path) }
Expand Down
3 changes: 2 additions & 1 deletion helper-cli/src/main/kotlin/commands/ListLicensesCommand.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2019 HERE Europe B.V.
* Copyright (C) 2022 Bosch.IO GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -171,7 +172,7 @@ internal class ListLicensesCommand : CliktCommand(
if (ortResult.isProject(packageId)) {
ortResult.getExcludes().paths
} else {
packageConfigurationProvider.getPackageConfiguration(packageId, provenance)?.pathExcludes.orEmpty()
packageConfigurationProvider.getPackageConfigurations(packageId, provenance).flatMap { it.pathExcludes }
}.any { it.matches(path) }

val violatedRulesByLicense = ortResult.getViolatedRulesByLicense(packageId, offendingSeverity)
Expand Down
2 changes: 1 addition & 1 deletion helper-cli/src/main/kotlin/common/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ internal fun OrtResult.getLicenseFindingsById(
if (isProject(id)) {
getLicenseFindingsCurations(id)
} else {
packageConfigurationProvider.getPackageConfiguration(id, provenance)?.licenseFindingCurations.orEmpty()
packageConfigurationProvider.getPackageConfigurations(id, provenance).flatMap { it.licenseFindingCurations }
}

scanner?.results?.scanResults?.get(id)?.forEach { scanResult ->
Expand Down
7 changes: 4 additions & 3 deletions model/src/main/kotlin/licenses/DefaultLicenseInfoProvider.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2017-2020 HERE Europe B.V.
* Copyright (C) 2022 Bosch.IO GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -108,10 +109,10 @@ class DefaultLicenseInfoProvider(
ortResult.repository.config.excludes.paths,
ortResult.repository.getRelativePath(project.vcsProcessed).orEmpty()
)
} ?: packageConfigurationProvider.getPackageConfiguration(id, provenance).let { packageConfiguration ->
} ?: packageConfigurationProvider.getPackageConfigurations(id, provenance).let { packageConfigurations ->
Triple(
packageConfiguration?.licenseFindingCurations.orEmpty(),
packageConfiguration?.pathExcludes.orEmpty(),
packageConfigurations.flatMap { it.licenseFindingCurations },
packageConfigurations.flatMap { it.pathExcludes },
""
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ import org.ossreviewtoolkit.model.config.PackageConfiguration

/**
* A [PackageConfigurationProvider] that combines the provided [providers] into a single provider. The order of the
* [providers] determines the order in which they are queried when requesting a [PackageConfiguration] in
* [getPackageConfiguration].
* [providers] determines the order in which they are queried when calling [getPackageConfigurations].
*/
class CompositePackageConfigurationProvider(
private val providers: List<PackageConfigurationProvider>
) : PackageConfigurationProvider {
constructor(vararg providers: PackageConfigurationProvider) : this(providers.asList())

override fun getPackageConfiguration(packageId: Identifier, provenance: Provenance): PackageConfiguration? =
providers.firstNotNullOfOrNull { it.getPackageConfiguration(packageId, provenance) }
override fun getPackageConfigurations(packageId: Identifier, provenance: Provenance): List<PackageConfiguration> =
providers.flatMap { it.getPackageConfigurations(packageId, provenance) }
}
6 changes: 3 additions & 3 deletions model/src/main/kotlin/utils/PackageConfigurationProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ fun interface PackageConfigurationProvider {
* A provider that does not provide any curations.
*/
@JvmField
val EMPTY = PackageConfigurationProvider { _, _ -> null }
val EMPTY = PackageConfigurationProvider { _, _ -> emptyList() }
}

/**
* Return the first matching [PackageConfiguration] for the given [packageId] and [provenance] if any.
* Return a list of [PackageConfiguration]s for the given [packageId] and [provenance].
*/
fun getPackageConfiguration(packageId: Identifier, provenance: Provenance): PackageConfiguration?
fun getPackageConfigurations(packageId: Identifier, provenance: Provenance): List<PackageConfiguration>
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,8 @@ open class SimplePackageConfigurationProvider(
configurationsById = configurations.groupByTo(HashMap()) { it.id }
}

override fun getPackageConfiguration(packageId: Identifier, provenance: Provenance): PackageConfiguration? =
configurationsById[packageId]?.filter { it.matches(packageId, provenance) }?.let {
require(it.size <= 1) { "There must be at most one package configuration per Id and provenance." }
it.singleOrNull()
}
override fun getPackageConfigurations(packageId: Identifier, provenance: Provenance): List<PackageConfiguration> =
configurationsById[packageId]?.filter { it.matches(packageId, provenance) }.orEmpty()
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2017-2021 HERE Europe B.V.
* Copyright (C) 2021 Bosch.IO GmbH
* Copyright (C) 2021-2022 Bosch.IO GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -235,15 +235,16 @@ internal class EvaluatedModelMapper(private val input: ReporterInput) {
if (input.ortResult.isProject(id)) {
input.ortResult.repository.config.curations.licenseFindings
} else {
input.packageConfigurationProvider.getPackageConfiguration(id, provenance)
?.licenseFindingCurations.orEmpty()
input.packageConfigurationProvider.getPackageConfigurations(id, provenance)
.flatMap { it.licenseFindingCurations }
}

private fun getPathExcludes(id: Identifier, provenance: Provenance): List<PathExclude> =
if (input.ortResult.isProject(id)) {
input.ortResult.getExcludes().paths
} else {
input.packageConfigurationProvider.getPackageConfiguration(id, provenance)?.pathExcludes.orEmpty()
input.packageConfigurationProvider.getPackageConfigurations(id, provenance)
.flatMap { it.pathExcludes }
}

private fun addProject(project: Project) {
Expand Down

0 comments on commit 00a8487

Please sign in to comment.