From 7c9d4352dd23abdfc2585b47e838079ea145d509 Mon Sep 17 00:00:00 2001 From: Frank Viernau Date: Mon, 27 Nov 2023 17:24:15 +0100 Subject: [PATCH] fix(helper-cli): Fix two issues with listing licenses The list licenses command may crash in case `sourceCodeDir` is not provided in the following scenarios: 1. When the source artifact has been scanned for the given `packageId` andi `vcsProcessed` is empty, then `fetchScannedSources()` crashes withing `Downloader.download()`, because the downloader throws when it attempts to download from VCS. 2. When the ORT file does not contain any scan result for the given package, then the downloader also throws. Ensure that the downloader always attempts to download from the right source code origin, to fix scenario #1. Furthermore, return early in case there is no scan result for the given package to fix scenario #2. Recently a similar crash has been fixed by [1] also by returning earlier. So, move the early return from [1] to an even earlier position. [1] 5a2193202c09fb1ba91c2ad65838aed4a9a5161a Signed-off-by: Frank Viernau --- .../kotlin/commands/ListLicensesCommand.kt | 16 ++++++---- .../src/main/kotlin/utils/Extensions.kt | 32 ++++++++++--------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/helper-cli/src/main/kotlin/commands/ListLicensesCommand.kt b/helper-cli/src/main/kotlin/commands/ListLicensesCommand.kt index 300581996bdab..2409cccc4bff2 100644 --- a/helper-cli/src/main/kotlin/commands/ListLicensesCommand.kt +++ b/helper-cli/src/main/kotlin/commands/ListLicensesCommand.kt @@ -33,8 +33,10 @@ import com.github.ajalt.clikt.parameters.types.file import java.io.File import java.lang.IllegalArgumentException -import org.ossreviewtoolkit.helper.utils.fetchScannedSources +import org.ossreviewtoolkit.helper.utils.downloadSources import org.ossreviewtoolkit.helper.utils.getLicenseFindingsById +import org.ossreviewtoolkit.helper.utils.getScannedProvenance +import org.ossreviewtoolkit.helper.utils.getSourceCodeOrigin import org.ossreviewtoolkit.helper.utils.getViolatedRulesByLicense import org.ossreviewtoolkit.helper.utils.readOrtResult import org.ossreviewtoolkit.helper.utils.replaceConfig @@ -146,9 +148,14 @@ internal class ListLicensesCommand : CliktCommand( throw UsageError("Could not find the package for the given id '${packageId.toCoordinates()}'.") } + val sourceCodeOrigin = ortResult.getScannedProvenance(packageId).getSourceCodeOrigin() ?: run { + println("No scan results available.") + return + } + val sourcesDir = sourceCodeDir ?: run { println("Downloading sources for package '${packageId.toCoordinates()}'...") - ortResult.fetchScannedSources(packageId) + ortResult.downloadSources(packageId, sourceCodeOrigin) } val packageConfigurationProvider = DirPackageConfigurationProvider(packageConfigurationsDir) @@ -190,11 +197,6 @@ internal class ListLicensesCommand : CliktCommand( } } - if (findingsByProvenance.isEmpty()) { - println("No scan results available.") - return - } - buildString { appendLine(" scan results:") findingsByProvenance.keys.forEachIndexed { i, provenance -> diff --git a/helper-cli/src/main/kotlin/utils/Extensions.kt b/helper-cli/src/main/kotlin/utils/Extensions.kt index 4be7311b30c0a..ba965d858e533 100644 --- a/helper-cli/src/main/kotlin/utils/Extensions.kt +++ b/helper-cli/src/main/kotlin/utils/Extensions.kt @@ -34,7 +34,6 @@ import org.jetbrains.exposed.sql.transactions.TransactionManager import org.ossreviewtoolkit.analyzer.PackageManagerFactory import org.ossreviewtoolkit.downloader.Downloader -import org.ossreviewtoolkit.model.ArtifactProvenance import org.ossreviewtoolkit.model.Identifier import org.ossreviewtoolkit.model.Issue import org.ossreviewtoolkit.model.KnownProvenance @@ -43,13 +42,13 @@ import org.ossreviewtoolkit.model.Package import org.ossreviewtoolkit.model.PackageCuration import org.ossreviewtoolkit.model.Project import org.ossreviewtoolkit.model.Provenance -import org.ossreviewtoolkit.model.RemoteArtifact import org.ossreviewtoolkit.model.Repository +import org.ossreviewtoolkit.model.RepositoryProvenance import org.ossreviewtoolkit.model.RuleViolation import org.ossreviewtoolkit.model.ScanResult import org.ossreviewtoolkit.model.Severity +import org.ossreviewtoolkit.model.SourceCodeOrigin import org.ossreviewtoolkit.model.TextLocation -import org.ossreviewtoolkit.model.VcsInfo import org.ossreviewtoolkit.model.config.CopyrightGarbage import org.ossreviewtoolkit.model.config.Curations import org.ossreviewtoolkit.model.config.DownloaderConfiguration @@ -88,21 +87,14 @@ internal fun List.minimize(projectScopes: List): List null + this is RepositoryProvenance -> SourceCodeOrigin.VCS + else -> SourceCodeOrigin.ARTIFACT + } + /** * Return all issues from scan results. Issues for excludes [Project]s or [Package]s are not returned if and only if * the given [omitExcluded] is true.