Skip to content

Commit

Permalink
refactor!: Move config helpers from model to new config-utils module
Browse files Browse the repository at this point in the history
Move the `ConfigurationResolver` and related helper functions to a new
`config-utils` module. This is preparation for being able to move the
`PackageConfigurationProvider` and `PackageCurationProvider` to the
respective API modules which is required for migrating them to the new
plugin API.

Signed-off-by: Martin Nonnenmacher <martin.nonnenmacher@bosch.com>
  • Loading branch information
mnonnenmacher committed Sep 2, 2024
1 parent d782466 commit 3c8b32a
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 49 deletions.
1 change: 1 addition & 0 deletions analyzer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
api(projects.model)

implementation(projects.downloader)
implementation(projects.utils.configUtils)
implementation(projects.utils.ortUtils)

implementation(libs.kotlinx.coroutines)
Expand Down
2 changes: 1 addition & 1 deletion analyzer/src/main/kotlin/Analyzer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.model.orEmpty
import org.ossreviewtoolkit.model.toYaml
import org.ossreviewtoolkit.model.utils.PackageCurationProvider
import org.ossreviewtoolkit.model.utils.setPackageCurations
import org.ossreviewtoolkit.utils.common.CommandLineTool
import org.ossreviewtoolkit.utils.common.VCS_DIRECTORIES
import org.ossreviewtoolkit.utils.config.setPackageCurations
import org.ossreviewtoolkit.utils.ort.Environment
import org.ossreviewtoolkit.utils.ort.runBlocking

Expand Down
1 change: 1 addition & 0 deletions helper-cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
implementation(projects.plugins.packageCurationProviders.filePackageCurationProvider)

implementation(projects.scanner)
implementation(projects.utils.configUtils)
implementation(projects.utils.ortUtils)

implementation(libs.clikt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ import org.ossreviewtoolkit.model.config.ScopeExclude
import org.ossreviewtoolkit.model.config.ScopeExcludeReason
import org.ossreviewtoolkit.model.mapper
import org.ossreviewtoolkit.model.orEmpty
import org.ossreviewtoolkit.model.utils.setPackageCurations
import org.ossreviewtoolkit.plugins.packagecurationproviders.api.PackageCurationProviderFactory
import org.ossreviewtoolkit.utils.common.expandTilde
import org.ossreviewtoolkit.utils.config.setPackageCurations
import org.ossreviewtoolkit.utils.ort.Environment
import org.ossreviewtoolkit.utils.ort.ORT_CONFIG_FILENAME
import org.ossreviewtoolkit.utils.ort.ortConfigDirectory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.config.CopyrightGarbage
import org.ossreviewtoolkit.model.config.orEmpty
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.utils.setPackageConfigurations
import org.ossreviewtoolkit.plugins.packageconfigurationproviders.dir.DirPackageConfigurationProvider
import org.ossreviewtoolkit.utils.common.expandTilde
import org.ossreviewtoolkit.utils.config.setPackageConfigurations

internal class ListCopyrightsCommand : CliktCommand(
help = "Lists the copyright findings."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import com.github.ajalt.clikt.parameters.types.file
import org.ossreviewtoolkit.helper.utils.readOrtResult
import org.ossreviewtoolkit.helper.utils.writeOrtResult
import org.ossreviewtoolkit.model.ResolvedPackageCurations.Companion.REPOSITORY_CONFIGURATION_PROVIDER_ID
import org.ossreviewtoolkit.model.utils.setPackageCurations
import org.ossreviewtoolkit.plugins.packagecurationproviders.api.SimplePackageCurationProvider
import org.ossreviewtoolkit.plugins.packagecurationproviders.file.FilePackageCurationProvider
import org.ossreviewtoolkit.utils.common.expandTilde
import org.ossreviewtoolkit.utils.config.setPackageCurations

internal class SetCommand : CliktCommand(
help = "(Re-)set all package curations for a given ORT file to the curations specified via package curations " +
Expand Down
39 changes: 0 additions & 39 deletions model/src/main/kotlin/utils/OrtResultExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,45 +26,6 @@ import org.ossreviewtoolkit.model.config.LicenseFilePatterns
import org.ossreviewtoolkit.model.licenses.DefaultLicenseInfoProvider
import org.ossreviewtoolkit.model.licenses.LicenseInfoResolver

/**
* Replace the package configurations in [OrtResult.resolvedConfiguration] with the ones obtained from
* [packageConfigurationProvider].
*/
fun OrtResult.setPackageConfigurations(packageConfigurationProvider: PackageConfigurationProvider): OrtResult {
val packageConfigurations = ConfigurationResolver.resolvePackageConfigurations(
identifiers = getUncuratedPackages().mapTo(mutableSetOf()) { it.id },
scanResultProvider = { id -> getScanResultsForId(id) },
packageConfigurationProvider = packageConfigurationProvider
)

return copy(resolvedConfiguration = resolvedConfiguration.copy(packageConfigurations = packageConfigurations))
}

/**
* Replace the package curations in [OrtResult.resolvedConfiguration] with the ones obtained from
* [packageCurationProviders]. The [packageCurationProviders] must be ordered highest-priority-first.
*/
fun OrtResult.setPackageCurations(packageCurationProviders: List<Pair<String, PackageCurationProvider>>): OrtResult {
val packageCurations =
ConfigurationResolver.resolvePackageCurations(getUncuratedPackages(), packageCurationProviders)

return copy(resolvedConfiguration = resolvedConfiguration.copy(packageCurations = packageCurations))
}

/**
* Replace the resolutions in [OrtResult.resolvedConfiguration] with the ones obtained from [resolutionProvider].
*/
fun OrtResult.setResolutions(resolutionProvider: ResolutionProvider): OrtResult {
val resolutions = ConfigurationResolver.resolveResolutions(
issues = getIssues().values.flatten(),
ruleViolations = getRuleViolations(),
vulnerabilities = getVulnerabilities().values.flatten(),
resolutionProvider = resolutionProvider
)

return copy(resolvedConfiguration = resolvedConfiguration.copy(resolutions = resolutions))
}

/**
* Create a [LicenseInfoResolver] for [this] [OrtResult]. If the resolver is used multiple times it should be stored
* instead of calling this function multiple times for better performance.
Expand Down
1 change: 1 addition & 0 deletions plugins/commands/evaluator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {
implementation(projects.evaluator)
implementation(projects.model)
implementation(projects.utils.commonUtils)
implementation(projects.utils.configUtils)
implementation(projects.utils.ortUtils)
implementation(projects.utils.spdxUtils)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ import org.ossreviewtoolkit.model.readValueOrDefault
import org.ossreviewtoolkit.model.utils.CompositePackageConfigurationProvider
import org.ossreviewtoolkit.model.utils.DefaultResolutionProvider
import org.ossreviewtoolkit.model.utils.mergeLabels
import org.ossreviewtoolkit.model.utils.setPackageConfigurations
import org.ossreviewtoolkit.model.utils.setPackageCurations
import org.ossreviewtoolkit.model.utils.setResolutions
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
import org.ossreviewtoolkit.plugins.commands.api.utils.SeverityStatsPrinter
import org.ossreviewtoolkit.plugins.commands.api.utils.configurationGroup
Expand All @@ -75,6 +72,9 @@ import org.ossreviewtoolkit.plugins.packagecurationproviders.api.SimplePackageCu
import org.ossreviewtoolkit.plugins.packagecurationproviders.file.FilePackageCurationProvider
import org.ossreviewtoolkit.utils.common.expandTilde
import org.ossreviewtoolkit.utils.common.safeMkdirs
import org.ossreviewtoolkit.utils.config.setPackageConfigurations
import org.ossreviewtoolkit.utils.config.setPackageCurations
import org.ossreviewtoolkit.utils.config.setResolutions
import org.ossreviewtoolkit.utils.ort.ORT_COPYRIGHT_GARBAGE_FILENAME
import org.ossreviewtoolkit.utils.ort.ORT_EVALUATOR_RULES_FILENAME
import org.ossreviewtoolkit.utils.ort.ORT_LICENSE_CLASSIFICATIONS_FILENAME
Expand Down
1 change: 1 addition & 0 deletions plugins/commands/reporter/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
implementation(projects.model)
implementation(projects.reporter)
implementation(projects.utils.commonUtils)
implementation(projects.utils.configUtils)
implementation(projects.utils.ortUtils)

implementation(libs.clikt)
Expand Down
4 changes: 2 additions & 2 deletions plugins/commands/reporter/src/main/kotlin/ReporterCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.readValueOrDefault
import org.ossreviewtoolkit.model.utils.CompositePackageConfigurationProvider
import org.ossreviewtoolkit.model.utils.DefaultResolutionProvider
import org.ossreviewtoolkit.model.utils.setPackageConfigurations
import org.ossreviewtoolkit.model.utils.setResolutions
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
import org.ossreviewtoolkit.plugins.commands.api.utils.configurationGroup
import org.ossreviewtoolkit.plugins.commands.api.utils.inputGroup
Expand All @@ -70,6 +68,8 @@ import org.ossreviewtoolkit.reporter.ReporterInput
import org.ossreviewtoolkit.utils.common.collectMessages
import org.ossreviewtoolkit.utils.common.expandTilde
import org.ossreviewtoolkit.utils.common.safeMkdirs
import org.ossreviewtoolkit.utils.config.setPackageConfigurations
import org.ossreviewtoolkit.utils.config.setResolutions
import org.ossreviewtoolkit.utils.ort.ORT_COPYRIGHT_GARBAGE_FILENAME
import org.ossreviewtoolkit.utils.ort.ORT_CUSTOM_LICENSE_TEXTS_DIRNAME
import org.ossreviewtoolkit.utils.ort.ORT_HOW_TO_FIX_TEXT_PROVIDER_FILENAME
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ include(":notifier")
include(":reporter")
include(":scanner")
include(":utils:common")
include(":utils:config")
include(":utils:ort")
include(":utils:scripting")
include(":utils:spdx")
Expand All @@ -58,6 +59,7 @@ project(":clients:osv").name = "osv-client"
project(":clients:vulnerable-code").name = "vulnerable-code-client"

project(":utils:common").name = "common-utils"
project(":utils:config").name = "config-utils"
project(":utils:ort").name = "ort-utils"
project(":utils:scripting").name = "scripting-utils"
project(":utils:spdx").name = "spdx-utils"
Expand Down
27 changes: 27 additions & 0 deletions utils/config/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/

plugins {
// Apply precompiled plugins.
id("ort-library-conventions")
}

dependencies {
api(projects.model)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.model.utils
package org.ossreviewtoolkit.utils.config

import kotlin.time.measureTimedValue

Expand All @@ -32,6 +32,9 @@ import org.ossreviewtoolkit.model.RuleViolation
import org.ossreviewtoolkit.model.ScanResult
import org.ossreviewtoolkit.model.config.PackageConfiguration
import org.ossreviewtoolkit.model.config.Resolutions
import org.ossreviewtoolkit.model.utils.PackageConfigurationProvider
import org.ossreviewtoolkit.model.utils.PackageCurationProvider
import org.ossreviewtoolkit.model.utils.ResolutionProvider
import org.ossreviewtoolkit.model.vulnerabilities.Vulnerability

object ConfigurationResolver {
Expand Down
64 changes: 64 additions & 0 deletions utils/config/src/main/kotlin/OrtResultExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2023 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.utils.config

import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.utils.PackageConfigurationProvider
import org.ossreviewtoolkit.model.utils.PackageCurationProvider
import org.ossreviewtoolkit.model.utils.ResolutionProvider

/**
* Replace the package configurations in [OrtResult.resolvedConfiguration] with the ones obtained from
* [packageConfigurationProvider].
*/
fun OrtResult.setPackageConfigurations(packageConfigurationProvider: PackageConfigurationProvider): OrtResult {
val packageConfigurations = ConfigurationResolver.resolvePackageConfigurations(
identifiers = getUncuratedPackages().mapTo(mutableSetOf()) { it.id },
scanResultProvider = { id -> getScanResultsForId(id) },
packageConfigurationProvider = packageConfigurationProvider
)

return copy(resolvedConfiguration = resolvedConfiguration.copy(packageConfigurations = packageConfigurations))
}

/**
* Replace the package curations in [OrtResult.resolvedConfiguration] with the ones obtained from
* [packageCurationProviders]. The [packageCurationProviders] must be ordered highest-priority-first.
*/
fun OrtResult.setPackageCurations(packageCurationProviders: List<Pair<String, PackageCurationProvider>>): OrtResult {
val packageCurations =
ConfigurationResolver.resolvePackageCurations(getUncuratedPackages(), packageCurationProviders)

return copy(resolvedConfiguration = resolvedConfiguration.copy(packageCurations = packageCurations))
}

/**
* Replace the resolutions in [OrtResult.resolvedConfiguration] with the ones obtained from [resolutionProvider].
*/
fun OrtResult.setResolutions(resolutionProvider: ResolutionProvider): OrtResult {
val resolutions = ConfigurationResolver.resolveResolutions(
issues = getIssues().values.flatten(),
ruleViolations = getRuleViolations(),
vulnerabilities = getVulnerabilities().values.flatten(),
resolutionProvider = resolutionProvider
)

return copy(resolvedConfiguration = resolvedConfiguration.copy(resolutions = resolutions))
}

0 comments on commit 3c8b32a

Please sign in to comment.