@@ -26,19 +26,35 @@ import kotlinx.serialization.json.encodeToStream
26
26
27
27
import org.ossreviewtoolkit.model.licenses.LicenseView
28
28
import org.ossreviewtoolkit.plugins.api.OrtPlugin
29
+ import org.ossreviewtoolkit.plugins.api.OrtPluginOption
29
30
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
30
31
import org.ossreviewtoolkit.reporter.Reporter
31
32
import org.ossreviewtoolkit.reporter.ReporterFactory
32
33
import org.ossreviewtoolkit.reporter.ReporterInput
33
34
import org.ossreviewtoolkit.utils.spdx.SpdxConstants
34
35
import org.ossreviewtoolkit.utils.spdx.SpdxLicense
36
+ import org.ossreviewtoolkit.utils.spdx.toSpdx
37
+
38
+ data class CtrlXAutomationReporterConfig (
39
+ /* *
40
+ * The categories of the licenses of the packages to include in the report. If a component has a license which has a
41
+ * category not present in this parameter, the license is removed from the component and not visible in the report.
42
+ * If a component has ALL its licenses removed this way, it is not displayed in the report. If the parameter is not
43
+ * set for the reporter, all components and all licenses are present in the report.
44
+ */
45
+ @OrtPluginOption
46
+ val licenseCategoriesToInclude : List <String >?
47
+ )
35
48
36
49
@OrtPlugin(
37
50
displayName = " CtrlX Automation Reporter" ,
38
51
description = " A reporter for the ctrlX Automation format." ,
39
52
factory = ReporterFactory ::class
40
53
)
41
- class CtrlXAutomationReporter (override val descriptor : PluginDescriptor = CtrlXAutomationReporterFactory .descriptor) :
54
+ class CtrlXAutomationReporter (
55
+ override val descriptor : PluginDescriptor = CtrlXAutomationReporterFactory .descriptor,
56
+ private val config : CtrlXAutomationReporterConfig
57
+ ) :
42
58
Reporter {
43
59
companion object {
44
60
const val REPORT_FILENAME = " fossinfo.json"
@@ -54,7 +70,11 @@ class CtrlXAutomationReporter(override val descriptor: PluginDescriptor = CtrlXA
54
70
55
71
override fun generateReport (input : ReporterInput , outputDir : File ): List <Result <File >> {
56
72
val packages = input.ortResult.getPackages(omitExcluded = true )
57
- val components = packages.mapTo(mutableListOf ()) { (pkg, _) ->
73
+ val licensesToInclude = config.licenseCategoriesToInclude?.flatMap {
74
+ input.licenseClassifications.licensesByCategory[it].orEmpty()
75
+ }.orEmpty()
76
+
77
+ val components = packages.mapNotNullTo(mutableListOf ()) { (pkg, _) ->
58
78
val qualifiedName = when (pkg.id.type) {
59
79
// At least for NPM packages, CtrlX requires the component name to be prefixed with the scope name,
60
80
// separated with a slash. Other package managers might require similar handling, but there seems to be
@@ -73,25 +93,41 @@ class CtrlXAutomationReporter(override val descriptor: PluginDescriptor = CtrlXA
73
93
input.ortResult.getPackageLicenseChoices(pkg.id),
74
94
input.ortResult.getRepositoryLicenseChoices()
75
95
)
76
- val licenses = effectiveLicense?.decompose()?.map {
96
+ var licenses = effectiveLicense?.decompose()?.map {
77
97
val name = it.toString()
78
98
val spdxId = SpdxLicense .forId(name)?.id
79
99
val text = input.licenseTextProvider.getLicenseText(name)
80
100
License (name = name, spdx = spdxId, text = text.orEmpty())
81
101
}
82
102
83
- // The specification requires at least one license.
84
- val componentLicenses = licenses.orEmpty().ifEmpty { listOf (LICENSE_NOASSERTION ) }
85
-
86
- Component (
87
- name = qualifiedName,
88
- version = pkg.id.version,
89
- homepage = pkg.homepageUrl.takeUnless { it.isEmpty() },
90
- copyright = copyrights?.let { CopyrightInformation (it) },
91
- licenses = componentLicenses,
92
- usage = if (pkg.isModified) Usage .Modified else Usage .AsIs
93
- // TODO: Map the PackageLinkage to an IntegrationMechanism.
94
- )
103
+ var componentShouldBeExcluded = false
104
+
105
+ if (config.licenseCategoriesToInclude != null ) {
106
+ val filteredLicenses = licenses?.filter { it.name.toSpdx() in licensesToInclude }
107
+
108
+ if (filteredLicenses != null && filteredLicenses.isEmpty()) {
109
+ componentShouldBeExcluded = true
110
+ } else {
111
+ licenses = filteredLicenses
112
+ }
113
+ }
114
+
115
+ if (componentShouldBeExcluded) {
116
+ null
117
+ } else {
118
+ // The specification requires at least one license.
119
+ val componentLicenses = licenses.orEmpty().ifEmpty { listOf (LICENSE_NOASSERTION ) }
120
+
121
+ Component (
122
+ name = qualifiedName,
123
+ version = pkg.id.version,
124
+ homepage = pkg.homepageUrl.takeUnless { it.isEmpty() },
125
+ copyright = copyrights?.let { CopyrightInformation (it) },
126
+ licenses = componentLicenses,
127
+ usage = if (pkg.isModified) Usage .Modified else Usage .AsIs
128
+ // TODO: Map the PackageLinkage to an IntegrationMechanism.
129
+ )
130
+ }
95
131
}
96
132
97
133
val reportFileResult = runCatching {
0 commit comments