From 9978aabc3ae95e494275b489c954a728d47120e7 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Fri, 23 Apr 2021 08:14:47 +0200 Subject: [PATCH] Handle user-facing empty configuration files leniently Fixes #3861. Signed-off-by: Sebastian Schuberth --- analyzer/src/main/kotlin/Analyzer.kt | 10 ++++------ .../kotlin/curation/FilePackageCurationProvider.kt | 4 ++-- cli/src/main/kotlin/commands/EvaluatorCommand.kt | 9 +++++++-- cli/src/main/kotlin/commands/ReporterCommand.kt | 5 ++++- cli/src/main/kotlin/commands/UploadCurationsCommand.kt | 4 ++-- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/analyzer/src/main/kotlin/Analyzer.kt b/analyzer/src/main/kotlin/Analyzer.kt index 50e44a246ba62..b8376586cd7b7 100644 --- a/analyzer/src/main/kotlin/Analyzer.kt +++ b/analyzer/src/main/kotlin/Analyzer.kt @@ -35,7 +35,7 @@ import org.ossreviewtoolkit.model.Repository import org.ossreviewtoolkit.model.VcsInfo import org.ossreviewtoolkit.model.config.AnalyzerConfiguration import org.ossreviewtoolkit.model.config.RepositoryConfiguration -import org.ossreviewtoolkit.model.readValue +import org.ossreviewtoolkit.model.readValueOrNull import org.ossreviewtoolkit.utils.Environment import org.ossreviewtoolkit.utils.ORT_REPO_CONFIG_FILENAME import org.ossreviewtoolkit.utils.log @@ -57,13 +57,11 @@ class Analyzer(private val config: AnalyzerConfiguration) { val actualRepositoryConfigurationFile = repositoryConfigurationFile ?: absoluteProjectPath.resolve(ORT_REPO_CONFIG_FILENAME) - val repositoryConfiguration = if (actualRepositoryConfigurationFile.isFile) { + val repositoryConfiguration = actualRepositoryConfigurationFile.takeIf { it.isFile }?.let { log.info { "Using configuration file '${actualRepositoryConfigurationFile.absolutePath}'." } - actualRepositoryConfigurationFile.readValue() - } else { - RepositoryConfiguration() - } + actualRepositoryConfigurationFile.readValueOrNull() + } ?: RepositoryConfiguration() log.debug { "Using the following configuration settings:\n$repositoryConfiguration" } diff --git a/analyzer/src/main/kotlin/curation/FilePackageCurationProvider.kt b/analyzer/src/main/kotlin/curation/FilePackageCurationProvider.kt index 31413d541b8cc..a871c8d622030 100644 --- a/analyzer/src/main/kotlin/curation/FilePackageCurationProvider.kt +++ b/analyzer/src/main/kotlin/curation/FilePackageCurationProvider.kt @@ -25,7 +25,7 @@ import org.ossreviewtoolkit.analyzer.PackageCurationProvider import org.ossreviewtoolkit.model.FileFormat import org.ossreviewtoolkit.model.Identifier import org.ossreviewtoolkit.model.PackageCuration -import org.ossreviewtoolkit.model.readValue +import org.ossreviewtoolkit.model.readValueOrDefault /** * A [PackageCurationProvider] that loads [PackageCuration]s from a single file. Supports all file formats specified @@ -33,7 +33,7 @@ import org.ossreviewtoolkit.model.readValue */ class FilePackageCurationProvider(curationFile: File) : PackageCurationProvider { internal val packageCurations: List by lazy { - curationFile.readValue>() + curationFile.readValueOrDefault(emptyList()) } override fun getCurationsFor(pkgId: Identifier) = packageCurations.filter { it.isApplicable(pkgId) } diff --git a/cli/src/main/kotlin/commands/EvaluatorCommand.kt b/cli/src/main/kotlin/commands/EvaluatorCommand.kt index c136145d86a7a..569274a3ee643 100644 --- a/cli/src/main/kotlin/commands/EvaluatorCommand.kt +++ b/cli/src/main/kotlin/commands/EvaluatorCommand.kt @@ -43,9 +43,11 @@ import org.ossreviewtoolkit.GroupTypes.FileType import org.ossreviewtoolkit.GroupTypes.StringType import org.ossreviewtoolkit.evaluator.Evaluator import org.ossreviewtoolkit.model.FileFormat +import org.ossreviewtoolkit.model.PackageCuration import org.ossreviewtoolkit.model.Severity import org.ossreviewtoolkit.model.config.CopyrightGarbage import org.ossreviewtoolkit.model.config.LicenseFilenamePatterns +import org.ossreviewtoolkit.model.config.RepositoryConfiguration import org.ossreviewtoolkit.model.config.createFileArchiver import org.ossreviewtoolkit.model.config.orEmpty import org.ossreviewtoolkit.model.licenses.DefaultLicenseInfoProvider @@ -53,6 +55,7 @@ import org.ossreviewtoolkit.model.licenses.LicenseClassifications import org.ossreviewtoolkit.model.licenses.LicenseInfoResolver import org.ossreviewtoolkit.model.licenses.orEmpty import org.ossreviewtoolkit.model.readValue +import org.ossreviewtoolkit.model.readValueOrDefault import org.ossreviewtoolkit.model.utils.mergeLabels import org.ossreviewtoolkit.readOrtResult import org.ossreviewtoolkit.utils.ORT_COPYRIGHT_GARBAGE_FILENAME @@ -235,11 +238,13 @@ class EvaluatorCommand : CliktCommand(name = "evaluate", help = "Evaluate ORT re var ortResultInput = readOrtResult(existingOrtFile) repositoryConfigurationFile?.let { - ortResultInput = ortResultInput.replaceConfig(it.readValue()) + val config = it.readValueOrDefault(RepositoryConfiguration()) + ortResultInput = ortResultInput.replaceConfig(config) } packageCurationsFile?.let { - ortResultInput = ortResultInput.replacePackageCurations(it.readValue()) + val curations = it.readValueOrDefault(emptyList()) + ortResultInput = ortResultInput.replacePackageCurations(curations) } val packageConfigurationProvider = packageConfigurationOption.createProvider() diff --git a/cli/src/main/kotlin/commands/ReporterCommand.kt b/cli/src/main/kotlin/commands/ReporterCommand.kt index e07a7a341df4f..d2e0d15e67428 100644 --- a/cli/src/main/kotlin/commands/ReporterCommand.kt +++ b/cli/src/main/kotlin/commands/ReporterCommand.kt @@ -44,6 +44,7 @@ import kotlinx.coroutines.runBlocking import org.ossreviewtoolkit.GlobalOptions import org.ossreviewtoolkit.model.config.CopyrightGarbage import org.ossreviewtoolkit.model.config.LicenseFilenamePatterns +import org.ossreviewtoolkit.model.config.RepositoryConfiguration import org.ossreviewtoolkit.model.config.Resolutions import org.ossreviewtoolkit.model.config.createFileArchiver import org.ossreviewtoolkit.model.config.orEmpty @@ -52,6 +53,7 @@ import org.ossreviewtoolkit.model.licenses.LicenseClassifications import org.ossreviewtoolkit.model.licenses.LicenseInfoResolver import org.ossreviewtoolkit.model.licenses.orEmpty import org.ossreviewtoolkit.model.readValue +import org.ossreviewtoolkit.model.readValueOrDefault import org.ossreviewtoolkit.model.utils.DefaultResolutionProvider import org.ossreviewtoolkit.readOrtResult import org.ossreviewtoolkit.reporter.DefaultLicenseTextProvider @@ -201,7 +203,8 @@ class ReporterCommand : CliktCommand( var ortResult = readOrtResult(ortFile) repositoryConfigurationFile?.let { - ortResult = ortResult.replaceConfig(it.readValue()) + val config = it.readValueOrDefault(RepositoryConfiguration()) + ortResult = ortResult.replaceConfig(config) } val resolutionProvider = DefaultResolutionProvider() diff --git a/cli/src/main/kotlin/commands/UploadCurationsCommand.kt b/cli/src/main/kotlin/commands/UploadCurationsCommand.kt index 71f8e068d6564..d3082ddc274f1 100644 --- a/cli/src/main/kotlin/commands/UploadCurationsCommand.kt +++ b/cli/src/main/kotlin/commands/UploadCurationsCommand.kt @@ -48,7 +48,7 @@ import org.ossreviewtoolkit.clients.clearlydefined.ErrorResponse import org.ossreviewtoolkit.clients.clearlydefined.HarvestStatus import org.ossreviewtoolkit.model.PackageCuration import org.ossreviewtoolkit.model.jsonMapper -import org.ossreviewtoolkit.model.readValue +import org.ossreviewtoolkit.model.readValueOrDefault import org.ossreviewtoolkit.model.utils.toClearlyDefinedCoordinates import org.ossreviewtoolkit.model.utils.toClearlyDefinedSourceLocation import org.ossreviewtoolkit.utils.OkHttpClientHelper @@ -95,7 +95,7 @@ class UploadCurationsCommand : CliktCommand( } override fun run() { - val curations = inputFile.readValue>() + val curations = inputFile.readValueOrDefault(emptyList()) val curationsToCoordinates = curations.mapNotNull { curation -> curation.id.toClearlyDefinedCoordinates()?.let { coordinates -> curation to coordinates