Skip to content

Commit d1492bb

Browse files
committed
feat(scanner): Add detected license mapping to ScanContext
Add the detected license mapping from the `ScannerConfiguration` to the `ScanContext` to make it available to `ScannerWrapper`s. This is currently only required by the `FossId` implementation because FossID can return arbitrary strings as licenses. These strings can sometimes not be parsed as SPDX expressions and can therefore not be returned as part of a `LicenseFinding` in the `ScanSummary`. A better solution could be to automatically convert all license strings returned by FossID to a form that can be parsed as SPDX expression, then the mapping could be applied globally. However, as this would be a breaking configuration change it is not implemented now and only added as a TODO comment instead. Signed-off-by: Martin Nonnenmacher <[email protected]>
1 parent ffce6dc commit d1492bb

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

plugins/scanners/fossid/src/main/kotlin/FossId.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,15 @@ class FossId internal constructor(
310310

311311
if (config.waitForResult) {
312312
val rawResults = getRawResults(scanCode)
313-
createResultSummary(startTime, provenance, rawResults, scanCode, scanId, issues)
313+
createResultSummary(
314+
startTime,
315+
provenance,
316+
rawResults,
317+
scanCode,
318+
scanId,
319+
issues,
320+
context.detectedLicenseMapping
321+
)
314322
} else {
315323
val issue = createAndLogIssue(
316324
source = name,
@@ -857,7 +865,8 @@ class FossId internal constructor(
857865
rawResults: RawResults,
858866
scanCode: String,
859867
scanId: String,
860-
additionalIssues: MutableList<Issue>
868+
additionalIssues: MutableList<Issue>,
869+
detectedLicenseMapping: Map<String, String>
861870
): ScanResult {
862871
// TODO: Maybe get issues from FossID (see has_failed_scan_files, get_failed_files and maybe get_scan_log).
863872

@@ -875,7 +884,7 @@ class FossId internal constructor(
875884

876885
val (licenseFindings, copyrightFindings) = rawResults.markedAsIdentifiedFiles.ifEmpty {
877886
rawResults.identifiedFiles
878-
}.mapSummary(ignoredFiles, issues, scannerConfig.detectedLicenseMapping)
887+
}.mapSummary(ignoredFiles, issues, detectedLicenseMapping)
879888

880889
val summary = ScanSummary(
881890
startTime = startTime,

plugins/scanners/fossid/src/main/kotlin/FossIdScanResults.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ internal fun <T : Summarizable> List<T>.mapSummary(
8585

8686
summary.licences.forEach {
8787
runCatching {
88-
// TODO: The license mapping should be moved to a central place.
88+
// TODO: The detected license mapping must be applied here, because FossID can return license strings
89+
// which cannot be parsed to an SpdxExpression. A better solution could be to automatically
90+
// convert the strings into a form that can be parsed, then the mapping could be applied globally.
8991
LicenseFinding(it.identifier.mapLicense(detectedLicenseMapping), location)
9092
}.onSuccess { licenseFinding ->
9193
licenseFindings += licenseFinding.copy(license = licenseFinding.license.normalize())

scanner/src/main/kotlin/ScanContext.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919

2020
package org.ossreviewtoolkit.scanner
2121

22+
import org.ossreviewtoolkit.model.LicenseFinding
2223
import org.ossreviewtoolkit.model.OrtResult
2324
import org.ossreviewtoolkit.model.PackageType
2425
import org.ossreviewtoolkit.model.config.Excludes
26+
import org.ossreviewtoolkit.model.config.ScannerConfiguration
27+
import org.ossreviewtoolkit.utils.spdx.SpdxExpression
2528

2629
/**
2730
* Additional context information that can be used by a [ScannerWrapper] to alter its behavior.
@@ -40,5 +43,15 @@ data class ScanContext(
4043
/**
4144
* The [Excludes] of the project to scan.
4245
*/
43-
val excludes: Excludes? = null
46+
val excludes: Excludes? = null,
47+
48+
/**
49+
* The detected license mappings configured in the
50+
* [scanner configuration][ScannerConfiguration.detectedLicenseMapping]. Can be used by [ScannerWrapper]
51+
* implementations where the scanner can return arbitrary license strings which cannot be parsed as
52+
* [SpdxExpression]s and can therefore not be returned as a [LicenseFinding] without being mapped first. Should not
53+
* be used by scanners where scan results are stored, because then changes in the mapping would not be applied to
54+
* stored results.
55+
*/
56+
val detectedLicenseMapping: Map<String, String> = emptyMap()
4457
)

scanner/src/main/kotlin/Scanner.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ class Scanner(
124124
ScanContext(
125125
ortResult.labels + labels,
126126
PackageType.PROJECT,
127-
ortResult.repository.config.excludes
127+
ortResult.repository.config.excludes,
128+
scannerConfig.detectedLicenseMapping
128129
)
129130
)
130131
} else {
@@ -139,7 +140,15 @@ class Scanner(
139140

140141
logger.info { "Scanning ${packages.size} package(s) with ${packageScannerWrappers.size} scanner(s)." }
141142

142-
scan(packages, ScanContext(ortResult.labels, PackageType.PACKAGE, ortResult.repository.config.excludes))
143+
scan(
144+
packages,
145+
ScanContext(
146+
ortResult.labels,
147+
PackageType.PACKAGE,
148+
ortResult.repository.config.excludes,
149+
scannerConfig.detectedLicenseMapping
150+
)
151+
)
143152
} else {
144153
logger.info { "Skipping package scan as no package scanner is configured." }
145154

0 commit comments

Comments
 (0)