Skip to content

Commit

Permalink
refactor(reporter)!: Migrate to new plugin API
Browse files Browse the repository at this point in the history
Migrate the reporters to the new plugin API. Reporter implementations
get their configuration options via the `generateReport` function. With
the new plugin API the configuration options are provided as a
constructor parameter. With this change both ways are supported to allow
for a step-by-step migration in the following commits.

Signed-off-by: Martin Nonnenmacher <martin.nonnenmacher@bosch.com>
  • Loading branch information
mnonnenmacher committed Nov 9, 2024
1 parent bd82abb commit fac5bf3
Show file tree
Hide file tree
Showing 49 changed files with 263 additions and 99 deletions.
7 changes: 4 additions & 3 deletions cli/src/funTest/kotlin/ExamplesFunTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ import org.ossreviewtoolkit.model.config.SendMailConfiguration
import org.ossreviewtoolkit.model.licenses.LicenseClassifications
import org.ossreviewtoolkit.model.readValue
import org.ossreviewtoolkit.notifier.Notifier
import org.ossreviewtoolkit.plugins.api.PluginConfig
import org.ossreviewtoolkit.reporter.HowToFixTextProvider
import org.ossreviewtoolkit.reporter.Reporter
import org.ossreviewtoolkit.reporter.ReporterFactory
import org.ossreviewtoolkit.reporter.ReporterInput
import org.ossreviewtoolkit.utils.ort.ORT_PACKAGE_CURATIONS_FILENAME
import org.ossreviewtoolkit.utils.ort.ORT_REPO_CONFIG_FILENAME
Expand Down Expand Up @@ -125,12 +126,12 @@ class ExamplesFunTest : StringSpec({
}

"The Asciidoctor PDF theme file is a valid" {
val reporter = Reporter.ALL.getValue("PdfTemplate")
val reporter = ReporterFactory.ALL.getValue("PdfTemplate")
val outputDir = tempdir()

takeExampleFile("asciidoctor-pdf-theme.yml")

val report = reporter.generateReport(
val report = reporter.create(PluginConfig()).generateReport(
ReporterInput(OrtResult.EMPTY),
outputDir,
PluginConfiguration(
Expand Down
2 changes: 1 addition & 1 deletion integrations/completions/ort-completion.fish
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ complete -c ort -f -n __fish_use_subcommand -a report -d 'Present Analyzer, Scan
## Options for report
complete -c ort -n "__fish_seen_subcommand_from report" -l ort-file -s i -r -F -d 'The ORT result file to use.'
complete -c ort -n "__fish_seen_subcommand_from report" -l output-dir -s o -r -F -d 'The output directory to store the generated reports in.'
complete -c ort -n "__fish_seen_subcommand_from report" -l report-formats -s f -r -d 'A comma-separated list of report formats to generate, any of [AOSD2, CtrlXAutomation, CycloneDx, DocBookTemplate, EvaluatedModel, FossId, FossIdSnippet, GitLabLicenseModel, HtmlTemplate, ManPageTemplate, Opossum, PdfTemplate, PlainTextTemplate, SpdxDocument, StaticHtml, TrustSource, WebApp].'
complete -c ort -n "__fish_seen_subcommand_from report" -l report-formats -s f -r -d 'A comma-separated list of report formats to generate, any of [AOSD2, CtrlXAutomation, CycloneDX, DocBookTemplate, EvaluatedModel, FossID, FossIdSnippet, GitLabLicenseModel, HtmlTemplate, ManPageTemplate, Opossum, PdfTemplate, PlainTextTemplate, SpdxDocument, StaticHTML, TrustSource, WebApp].'
complete -c ort -n "__fish_seen_subcommand_from report" -l copyright-garbage-file -r -F -d 'A file containing copyright statements which are marked as garbage. This can make the output inconsistent with the evaluator output but is useful when testing copyright garbage.'
complete -c ort -n "__fish_seen_subcommand_from report" -l custom-license-texts-dir -r -F -d 'A directory which maps custom license IDs to license texts. It should contain one text file per license with the license ID as the filename. A custom license text is used only if its ID has a \'LicenseRef-\' prefix and if the respective license text is not known by ORT.'
complete -c ort -n "__fish_seen_subcommand_from report" -l how-to-fix-text-provider-script -r -F -d 'The path to a Kotlin script which returns an instance of a \'HowToFixTextProvider\'. That provider injects how-to-fix texts in Markdown format for ORT issues.'
Expand Down
21 changes: 12 additions & 9 deletions plugins/commands/reporter/src/main/kotlin/ReporterCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ 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.plugins.api.PluginConfig
import org.ossreviewtoolkit.plugins.commands.api.OrtCommand
import org.ossreviewtoolkit.plugins.commands.api.utils.configurationGroup
import org.ossreviewtoolkit.plugins.commands.api.utils.inputGroup
Expand All @@ -63,7 +64,7 @@ import org.ossreviewtoolkit.plugins.packageconfigurationproviders.api.SimplePack
import org.ossreviewtoolkit.plugins.packageconfigurationproviders.dir.DirPackageConfigurationProvider
import org.ossreviewtoolkit.reporter.DefaultLicenseTextProvider
import org.ossreviewtoolkit.reporter.HowToFixTextProvider
import org.ossreviewtoolkit.reporter.Reporter
import org.ossreviewtoolkit.reporter.ReporterFactory
import org.ossreviewtoolkit.reporter.ReporterInput
import org.ossreviewtoolkit.utils.common.collectMessages
import org.ossreviewtoolkit.utils.common.expandTilde
Expand Down Expand Up @@ -103,9 +104,10 @@ class ReporterCommand : OrtCommand(

private val reportFormats by option(
"--report-formats", "-f",
help = "A comma-separated list of report formats to generate, any of ${Reporter.ALL.keys}."
help = "A comma-separated list of report formats to generate, any of ${ReporterFactory.ALL.keys}."
).convert { name ->
Reporter.ALL[name] ?: throw BadParameterValue("Report formats must be one or more of ${Reporter.ALL.keys}.")
ReporterFactory.ALL[name]
?: throw BadParameterValue("Report formats must be one or more of ${ReporterFactory.ALL.keys}.")
}.split(",").required().outputGroup()

private val copyrightGarbageFile by option(
Expand Down Expand Up @@ -191,8 +193,8 @@ class ReporterCommand : OrtCommand(
"format, and the value is an arbitrary key-value pair. For example: " +
"-O PlainTextTemplate=template.id=NOTICE_SUMMARY"
).splitPair().convert { (format, option) ->
require(format in Reporter.ALL.keys) {
"Report formats must be one or more of ${Reporter.ALL.keys}."
require(format in ReporterFactory.ALL.keys) {
"Report formats must be one or more of ${ReporterFactory.ALL.keys}."
}

format to Pair(option.substringBefore("="), option.substringAfter("=", ""))
Expand Down Expand Up @@ -288,11 +290,12 @@ class ReporterCommand : OrtCommand(
reportFormats.map { reporter ->
async {
val threadName = Thread.currentThread().name
echo("Generating the '${reporter.type}' report in thread '$threadName'...")
echo("Generating the '${reporter.descriptor.id}' report in thread '$threadName'...")

reporter to measureTimedValue {
val options = reportConfigMap[reporter.type] ?: PluginConfiguration.EMPTY
reporter.generateReport(input, outputDir, options)
val options = reportConfigMap[reporter.descriptor.id] ?: PluginConfiguration.EMPTY
reporter.create(PluginConfig(options.options, options.secrets))
.generateReport(input, outputDir, options)
}
}
}.awaitAll()
Expand All @@ -302,7 +305,7 @@ class ReporterCommand : OrtCommand(
var failureCount = 0

reportDurationMap.value.forEach { (reporter, timedValue) ->
val name = reporter.type
val name = reporter.descriptor.id
val fileResults = timedValue.value

fileResults.forEach { fileResult ->
Expand Down
4 changes: 3 additions & 1 deletion plugins/reporters/aosd/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

plugins {
// Apply precompiled plugins.
id("ort-library-conventions")
id("ort-plugin-conventions")

// Apply third-party plugins.
alias(libs.plugins.kotlinSerialization)
Expand All @@ -29,6 +29,8 @@ dependencies {
api(projects.model)
api(projects.reporter)

ksp(projects.reporter)

implementation(projects.utils.spdxUtils)

implementation(libs.kotlinx.serialization.core)
Expand Down
13 changes: 10 additions & 3 deletions plugins/reporters/aosd/src/main/kotlin/Aosd2Reporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@ import org.ossreviewtoolkit.model.RemoteArtifact
import org.ossreviewtoolkit.model.RepositoryProvenance
import org.ossreviewtoolkit.model.config.PluginConfiguration
import org.ossreviewtoolkit.model.licenses.LicenseView
import org.ossreviewtoolkit.plugins.api.OrtPlugin
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
import org.ossreviewtoolkit.plugins.reporters.aosd.AOSD2.ExternalDependency
import org.ossreviewtoolkit.reporter.Reporter
import org.ossreviewtoolkit.reporter.ReporterFactory
import org.ossreviewtoolkit.reporter.ReporterInput
import org.ossreviewtoolkit.utils.spdx.SpdxLicense

class Aosd2Reporter : Reporter {
override val type = "AOSD2"

@OrtPlugin(
id = "AOSD2",
displayName = "Audi Open Source Diagnostics 2 Reporter",
description = "A reporter for the Audi Open Source Diagnostics 2 (AOSD2) format.",
factory = ReporterFactory::class
)
class Aosd2Reporter(override val descriptor: PluginDescriptor = Aosd2ReporterFactory.descriptor) : Reporter {
override fun generateReport(
input: ReporterInput,
outputDir: File,
Expand Down

This file was deleted.

4 changes: 3 additions & 1 deletion plugins/reporters/asciidoc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

plugins {
// Apply precompiled plugins.
id("ort-library-conventions")
id("ort-plugin-conventions")
}

dependencies {
api(projects.reporter)

ksp(projects.reporter)

implementation(projects.model)
implementation(projects.plugins.reporters.freemarkerReporter)
implementation(projects.utils.commonUtils)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@

package org.ossreviewtoolkit.plugins.reporters.asciidoc

import org.ossreviewtoolkit.plugins.api.OrtPlugin
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
import org.ossreviewtoolkit.reporter.Reporter
import org.ossreviewtoolkit.reporter.ReporterFactory

/**
* A [Reporter] that creates [DocBook][1] files from [Apache Freemarker][2] templates.
*
* [1]: https://docbook.org
* [2]: https://freemarker.apache.org
*/
class DocBookTemplateReporter : AsciiDocTemplateReporter() {
@OrtPlugin(
displayName = "DocBook Template Reporter",
description = "Generates DocBook from AsciiDoc files from Apache Freemarker templates.",
factory = ReporterFactory::class
)
class DocBookTemplateReporter(override val descriptor: PluginDescriptor = DocBookTemplateReporterFactory.descriptor) :
AsciiDocTemplateReporter() {
override val backend = "docbook"
override val type = "DocBookTemplate"
}
12 changes: 10 additions & 2 deletions plugins/reporters/asciidoc/src/main/kotlin/HtmlTemplateReporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,22 @@

package org.ossreviewtoolkit.plugins.reporters.asciidoc

import org.ossreviewtoolkit.plugins.api.OrtPlugin
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
import org.ossreviewtoolkit.reporter.Reporter
import org.ossreviewtoolkit.reporter.ReporterFactory

/**
* A [Reporter] that creates HTML files from [Apache Freemarker][1] templates.
*
* [1]: https://freemarker.apache.org
*/
class HtmlTemplateReporter : AsciiDocTemplateReporter() {
@OrtPlugin(
displayName = "HTML Template Reporter",
description = "Generates HTML from AsciiDoc files from Apache Freemarker templates.",
factory = ReporterFactory::class
)
class HtmlTemplateReporter(override val descriptor: PluginDescriptor = HtmlTemplateReporterFactory.descriptor) :
AsciiDocTemplateReporter() {
override val backend = "html"
override val type = "HtmlTemplate"
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,22 @@

package org.ossreviewtoolkit.plugins.reporters.asciidoc

import org.ossreviewtoolkit.plugins.api.OrtPlugin
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
import org.ossreviewtoolkit.reporter.Reporter
import org.ossreviewtoolkit.reporter.ReporterFactory

/**
* A [Reporter] that creates man pages from [Apache Freemarker][1] templates.
*
* [1]: https://freemarker.apache.org
*/
class ManPageTemplateReporter : AsciiDocTemplateReporter() {
@OrtPlugin(
displayName = "Man Page Template Reporter",
description = "Generates manpages from AsciiDoc files from Apache Freemarker templates.",
factory = ReporterFactory::class
)
class ManPageTemplateReporter(override val descriptor: PluginDescriptor = ManPageTemplateReporterFactory.descriptor) :
AsciiDocTemplateReporter() {
override val backend = "manpage"
override val type = "ManPageTemplate"
}
12 changes: 10 additions & 2 deletions plugins/reporters/asciidoc/src/main/kotlin/PdfTemplateReporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import java.io.File

import org.asciidoctor.Attributes

import org.ossreviewtoolkit.plugins.api.OrtPlugin
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
import org.ossreviewtoolkit.reporter.Reporter
import org.ossreviewtoolkit.reporter.ReporterFactory

/**
* A [Reporter] that creates PDF files using a combination of [Apache Freemarker][1] templates and [AsciiDoc][2]
Expand Down Expand Up @@ -52,14 +55,19 @@ import org.ossreviewtoolkit.reporter.Reporter
* [4]: https://github.com/asciidoctor/asciidoctorj-pdf
* [5]: https://docs.asciidoctor.org/pdf-converter/latest/theme/
*/
class PdfTemplateReporter : AsciiDocTemplateReporter() {
@OrtPlugin(
displayName = "PDF Template Reporter",
description = "Generates PDF from AsciiDoc files from Apache Freemarker templates.",
factory = ReporterFactory::class
)
class PdfTemplateReporter(override val descriptor: PluginDescriptor = PdfTemplateReporterFactory.descriptor) :
AsciiDocTemplateReporter() {
companion object {
private const val OPTION_PDF_THEME_FILE = "pdf.theme.file"
private const val OPTION_PDF_FONTS_DIR = "pdf.fonts.dir"
}

override val backend = "pdf"
override val type = "PdfTemplate"

override fun processTemplateOptions(outputDir: File, options: MutableMap<String, String>): Attributes =
Attributes.builder().apply {
Expand Down

This file was deleted.

4 changes: 3 additions & 1 deletion plugins/reporters/ctrlx/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

plugins {
// Apply precompiled plugins.
id("ort-library-conventions")
id("ort-plugin-conventions")

// Apply third-party plugins.
alias(libs.plugins.kotlinSerialization)
Expand All @@ -29,6 +29,8 @@ dependencies {
api(projects.reporter)
api(projects.model)

ksp(projects.reporter)

implementation(projects.utils.spdxUtils)

implementation(libs.kotlinx.serialization.core)
Expand Down
13 changes: 10 additions & 3 deletions plugins/reporters/ctrlx/src/main/kotlin/CtrlXAutomationReporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,21 @@ import kotlinx.serialization.json.encodeToStream

import org.ossreviewtoolkit.model.config.PluginConfiguration
import org.ossreviewtoolkit.model.licenses.LicenseView
import org.ossreviewtoolkit.plugins.api.OrtPlugin
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
import org.ossreviewtoolkit.reporter.Reporter
import org.ossreviewtoolkit.reporter.ReporterFactory
import org.ossreviewtoolkit.reporter.ReporterInput
import org.ossreviewtoolkit.utils.spdx.SpdxConstants
import org.ossreviewtoolkit.utils.spdx.SpdxLicense

class CtrlXAutomationReporter : Reporter {
@OrtPlugin(
displayName = "CtrlX Automation Reporter",
description = "A reporter for the ctrlX Automation format.",
factory = ReporterFactory::class
)
class CtrlXAutomationReporter(override val descriptor: PluginDescriptor = CtrlXAutomationReporterFactory.descriptor) :
Reporter {
companion object {
const val REPORT_FILENAME = "fossinfo.json"

Expand All @@ -44,8 +53,6 @@ class CtrlXAutomationReporter : Reporter {
)
}

override val type = "CtrlXAutomation"

override fun generateReport(
input: ReporterInput,
outputDir: File,
Expand Down

This file was deleted.

4 changes: 3 additions & 1 deletion plugins/reporters/cyclonedx/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@

plugins {
// Apply precompiled plugins.
id("ort-library-conventions")
id("ort-plugin-conventions")
}

dependencies {
api(projects.reporter)

api(libs.cyclonedx)

ksp(projects.reporter)

implementation(projects.model)
implementation(projects.utils.commonUtils)
implementation(projects.utils.ortUtils)
Expand Down
13 changes: 10 additions & 3 deletions plugins/reporters/cyclonedx/src/main/kotlin/CycloneDxReporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ import org.ossreviewtoolkit.model.config.PluginConfiguration
import org.ossreviewtoolkit.model.licenses.ResolvedLicenseInfo
import org.ossreviewtoolkit.model.utils.toPurl
import org.ossreviewtoolkit.model.vulnerabilities.Vulnerability
import org.ossreviewtoolkit.plugins.api.OrtPlugin
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
import org.ossreviewtoolkit.reporter.Reporter
import org.ossreviewtoolkit.reporter.ReporterFactory
import org.ossreviewtoolkit.reporter.ReporterInput
import org.ossreviewtoolkit.utils.common.alsoIfNull
import org.ossreviewtoolkit.utils.ort.Environment
Expand All @@ -69,7 +72,13 @@ import org.ossreviewtoolkit.utils.spdx.SpdxLicense
* - *output.file.formats*: A comma-separated list of (case-insensitive) output formats to export to. Supported are XML
* and JSON.
*/
class CycloneDxReporter : Reporter {
@OrtPlugin(
id = "CycloneDX",
displayName = "CycloneDX Reporter",
description = "Creates software bills of materials (SBOM) in the CycloneDX format.",
factory = ReporterFactory::class
)
class CycloneDxReporter(override val descriptor: PluginDescriptor = CycloneDxReporterFactory.descriptor) : Reporter {
companion object {
val DEFAULT_SCHEMA_VERSION = Version.VERSION_15
val DEFAULT_DATA_LICENSE = SpdxLicense.CC0_1_0
Expand All @@ -82,8 +91,6 @@ class CycloneDxReporter : Reporter {
const val OPTION_OUTPUT_FILE_FORMATS = "output.file.formats"
}

override val type = "CycloneDx"

private fun Bom.addExternalReference(type: ExternalReference.Type, url: String, comment: String? = null) {
if (url.isBlank()) return

Expand Down

This file was deleted.

Loading

0 comments on commit fac5bf3

Please sign in to comment.