Skip to content

Commit

Permalink
Handle user-facing empty configuration files leniently
Browse files Browse the repository at this point in the history
Fixes #3861.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed May 18, 2021
1 parent f24f5e4 commit 9978aab
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
10 changes: 4 additions & 6 deletions analyzer/src/main/kotlin/Analyzer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ 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
* in [FileFormat].
*/
class FilePackageCurationProvider(curationFile: File) : PackageCurationProvider {
internal val packageCurations: List<PackageCuration> by lazy {
curationFile.readValue<List<PackageCuration>>()
curationFile.readValueOrDefault(emptyList())
}

override fun getCurationsFor(pkgId: Identifier) = packageCurations.filter { it.isApplicable(pkgId) }
Expand Down
9 changes: 7 additions & 2 deletions cli/src/main/kotlin/commands/EvaluatorCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ 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
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
Expand Down Expand Up @@ -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<PackageCuration>())
ortResultInput = ortResultInput.replacePackageCurations(curations)
}

val packageConfigurationProvider = packageConfigurationOption.createProvider()
Expand Down
5 changes: 4 additions & 1 deletion cli/src/main/kotlin/commands/ReporterCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/kotlin/commands/UploadCurationsCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -95,7 +95,7 @@ class UploadCurationsCommand : CliktCommand(
}

override fun run() {
val curations = inputFile.readValue<List<PackageCuration>>()
val curations = inputFile.readValueOrDefault(emptyList<PackageCuration>())
val curationsToCoordinates = curations.mapNotNull { curation ->
curation.id.toClearlyDefinedCoordinates()?.let { coordinates ->
curation to coordinates
Expand Down

0 comments on commit 9978aab

Please sign in to comment.