Skip to content

Commit

Permalink
refactor(evaluatedmodel): Use a plugin config class
Browse files Browse the repository at this point in the history
Add a config class for the reporter and use it instead of the options
passed to the `generateReport` function.

Signed-off-by: Martin Nonnenmacher <martin.nonnenmacher@bosch.com>
  • Loading branch information
mnonnenmacher committed Sep 22, 2024
1 parent 74b9710 commit c658c37
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import io.kotest.matchers.shouldBe

import org.ossreviewtoolkit.model.FileFormat
import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.config.PluginConfiguration
import org.ossreviewtoolkit.plugins.api.PluginConfig
import org.ossreviewtoolkit.reporter.ReporterInput
import org.ossreviewtoolkit.utils.common.normalizeLineBreaks
import org.ossreviewtoolkit.utils.test.getAssetAsString
Expand Down Expand Up @@ -54,7 +54,7 @@ class EvaluatedModelReporterFunTest : WordSpec({
val ortResult = readOrtResult("src/funTest/assets/reporter-test-input.yml")
val options = mapOf(
EvaluatedModelReporter.OPTION_OUTPUT_FILE_FORMATS to FileFormat.YAML.fileExtension,
EvaluatedModelReporter.OPTION_DEDUPLICATE_DEPENDENCY_TREE to "True"
EvaluatedModelReporter.OPTION_DEDUPLICATE_DEPENDENCY_TREE to "true"
)

generateReport(ortResult, options) shouldBe expectedResult
Expand All @@ -70,6 +70,6 @@ private fun TestConfiguration.generateReport(ortResult: OrtResult, options: Map<

val outputDir = tempdir()

return EvaluatedModelReporter().generateReport(input, outputDir, PluginConfiguration(options))
return EvaluatedModelReporterFactory().create(PluginConfig(options)).generateReport(input, outputDir)
.single().getOrThrow().readText().normalizeLineBreaks()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,43 @@ import java.io.File
import org.ossreviewtoolkit.model.FileFormat
import org.ossreviewtoolkit.model.config.PluginConfiguration
import org.ossreviewtoolkit.plugins.api.OrtPlugin
import org.ossreviewtoolkit.plugins.api.OrtPluginOption
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
import org.ossreviewtoolkit.reporter.Reporter
import org.ossreviewtoolkit.reporter.ReporterFactory
import org.ossreviewtoolkit.reporter.ReporterInput

data class EvaluatedModelReporterConfig(
/**
* Controls whether subtrees occurring multiple times in the dependency tree are stripped.
*/
@OrtPluginOption(
defaultValue = "false"
)
val deduplicateDependencyTree: Boolean,

/**
* The list of file formats to generate, defaults to JSON. Supported formats are JSON and YAML.
*/
@OrtPluginOption(
defaultValue = "JSON",
aliases = ["output.file.formats"]
)
val outputFileFormats: List<String>
)

/**
* A [Reporter] that generates an [EvaluatedModel].
*
* This reporter supports the following options:
* - *output.file.formats*: The list of [FileFormat]s to generate, defaults to [FileFormat.JSON].
* - *deduplicateDependencyTree*: Controls whether subtrees occurring multiple times in the dependency tree are
* stripped.
*/
@OrtPlugin(
displayName = "Evaluated Model Reporter",
description = "Generates an evaluated model of the ORT result.",
factory = ReporterFactory::class
)
class EvaluatedModelReporter(override val descriptor: PluginDescriptor) : Reporter {
class EvaluatedModelReporter(
override val descriptor: PluginDescriptor,
private val config: EvaluatedModelReporterConfig
) : Reporter {
companion object {
const val OPTION_OUTPUT_FILE_FORMATS = "output.file.formats"

Expand All @@ -54,15 +72,9 @@ class EvaluatedModelReporter(override val descriptor: PluginDescriptor) : Report
outputDir: File,
config: PluginConfiguration
): List<Result<File>> {
val evaluatedModel = EvaluatedModel.create(
input,
config.options[OPTION_DEDUPLICATE_DEPENDENCY_TREE].toBoolean()
)
val evaluatedModel = EvaluatedModel.create(input, this.config.deduplicateDependencyTree)

val outputFileFormats = config.options[OPTION_OUTPUT_FILE_FORMATS]
?.split(',')
?.mapTo(mutableSetOf()) { FileFormat.forExtension(it) }
?: setOf(FileFormat.JSON)
val outputFileFormats = this.config.outputFileFormats.map { FileFormat.forExtension(it) }

return outputFileFormats.map { fileFormat ->
runCatching {
Expand Down

0 comments on commit c658c37

Please sign in to comment.