Skip to content

Commit

Permalink
feat(helper-cli): Inlcude package curations into generated ORT file
Browse files Browse the repository at this point in the history
As the analyzer is responsible for adding all package curations from all
configurated providers to the resolved configuration in the ORT file, do
so as well when generating the analyzer result from a package list file.

This allows to use the standard package curation workflow with
`CreateAnalyzerResultFromPackageListCommand`.

Signed-off-by: Frank Viernau <[email protected]>
  • Loading branch information
fviernau committed Sep 12, 2023
1 parent 65992e0 commit e6a57c0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import io.kotest.matchers.shouldBe

import org.ossreviewtoolkit.helper.HelperMain
import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.ResolvedConfiguration
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.model.toYaml
import org.ossreviewtoolkit.utils.ort.Environment
import org.ossreviewtoolkit.utils.ort.createOrtTempDir
import org.ossreviewtoolkit.utils.test.getAssetFile
Expand All @@ -47,7 +47,8 @@ class CreateAnalyzerResultFromPackageListCommandFunTest : WordSpec({
outputFile.absolutePath
)

outputFile.readText() shouldBe expectedOutputFile.readValue<OrtResult>().patchEnvironment().toYaml()
outputFile.readValue<OrtResult>().patchAnalyzerResult() shouldBe
expectedOutputFile.readValue<OrtResult>().patchAnalyzerResult()
}
}
})
Expand All @@ -61,4 +62,8 @@ private fun runMain(vararg args: String) {
}
}

private fun OrtResult.patchEnvironment(): OrtResult = copy(analyzer = analyzer?.copy(environment = Environment()))
private fun OrtResult.patchAnalyzerResult(): OrtResult =
copy(
analyzer = analyzer?.copy(environment = Environment()),
resolvedConfiguration = ResolvedConfiguration()
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.fasterxml.jackson.module.kotlin.readValue

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.convert
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.types.file
Expand All @@ -44,13 +45,18 @@ import org.ossreviewtoolkit.model.Scope
import org.ossreviewtoolkit.model.VcsInfo
import org.ossreviewtoolkit.model.VcsType
import org.ossreviewtoolkit.model.config.Excludes
import org.ossreviewtoolkit.model.config.OrtConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
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.addPackageCurations
import org.ossreviewtoolkit.plugins.packagecurationproviders.api.PackageCurationProviderFactory
import org.ossreviewtoolkit.utils.common.expandTilde
import org.ossreviewtoolkit.utils.ort.Environment
import org.ossreviewtoolkit.utils.ort.ORT_CONFIG_FILENAME
import org.ossreviewtoolkit.utils.ort.ortConfigDirectory

internal class CreateAnalyzerResultFromPackageListCommand : CliktCommand(
"A command which turns a package list file into an analyzer result."
Expand All @@ -71,6 +77,14 @@ internal class CreateAnalyzerResultFromPackageListCommand : CliktCommand(
.convert { it.absoluteFile.normalize() }
.required()

private val configFile by option(
"--config",
help = "The path to the ORT configuration file that configures the scan results storage."
).convert { it.expandTilde() }
.file(mustExist = true, canBeFile = true, canBeDir = false, mustBeWritable = false, mustBeReadable = true)
.convert { it.absoluteFile.normalize() }
.default(ortConfigDirectory.resolve(ORT_CONFIG_FILENAME))

override fun run() {
val packageList = packageListFile.mapper().copy().apply {
// Use camel case already now (even if in all places snake case is used), because there is a plan
Expand All @@ -92,6 +106,9 @@ internal class CreateAnalyzerResultFromPackageListCommand : CliktCommand(
)
)

val ortConfig = OrtConfiguration.load(emptyMap(), configFile)
val packageCurationProviders = PackageCurationProviderFactory.create(ortConfig.packageCurationProviders)

val ortResult = OrtResult(
analyzer = AnalyzerRun.EMPTY.copy(
result = AnalyzerResult(
Expand All @@ -110,7 +127,7 @@ internal class CreateAnalyzerResultFromPackageListCommand : CliktCommand(
)
)
)
)
).addPackageCurations(packageCurationProviders)

writeOrtResult(ortResult, ortFile)
}
Expand Down

0 comments on commit e6a57c0

Please sign in to comment.