From 6e4f97eeb0257cf0f7f373d3a1976abb944c0141 Mon Sep 17 00:00:00 2001 From: Nicolas Nobelis Date: Fri, 15 Mar 2024 08:43:58 +0100 Subject: [PATCH] chore(fossid-webapp): Extract license mapping code to a separate function Signed-off-by: Nicolas Nobelis --- .../src/main/kotlin/FossIdScanResults.kt | 48 ++++++++++++------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/plugins/scanners/fossid/src/main/kotlin/FossIdScanResults.kt b/plugins/scanners/fossid/src/main/kotlin/FossIdScanResults.kt index 6d73d27addc75..104f54b2b9701 100644 --- a/plugins/scanners/fossid/src/main/kotlin/FossIdScanResults.kt +++ b/plugins/scanners/fossid/src/main/kotlin/FossIdScanResults.kt @@ -90,28 +90,17 @@ internal fun List.mapSummary( val files = filterNot { it.getFileName() in ignoredFiles } files.forEach { summarizable -> val summary = summarizable.toSummary() - val location = TextLocation(summary.path, TextLocation.UNKNOWN_LINE, TextLocation.UNKNOWN_LINE) - - summary.licences.forEach { - runCatching { - // TODO: The detected license mapping must be applied here, because FossID can return license strings - // which cannot be parsed to an SpdxExpression. A better solution could be to automatically - // convert the strings into a form that can be parsed, then the mapping could be applied globally. - LicenseFinding(it.identifier.mapLicense(detectedLicenseMapping), location) - }.onSuccess { licenseFinding -> - licenseFindings += licenseFinding.copy(license = licenseFinding.license.normalize()) - }.onFailure { spdxException -> - issues += FossId.createAndLogIssue( - source = "FossId", - message = "Failed to parse license '${it.identifier}' as an SPDX expression: " + - spdxException.collectMessages() - ) + val defaultLocation = TextLocation(summary.path, TextLocation.UNKNOWN_LINE, TextLocation.UNKNOWN_LINE) + + summary.licences.forEach { licenseAddedInTheUI -> + mapLicense(licenseAddedInTheUI.identifier, defaultLocation, issues, detectedLicenseMapping)?.let { + licenseFindings += it } } summarizable.getCopyright().let { if (it.isNotEmpty()) { - copyrightFindings += CopyrightFinding(it, location) + copyrightFindings += CopyrightFinding(it, defaultLocation) } } } @@ -122,6 +111,31 @@ internal fun List.mapSummary( ) } +/** + * Convert a [license] at [location] from FossID to a valid [LicenseFinding]. If the license cannot be mapped, null is + * returned and an issue is added to [issues]. + */ +private fun mapLicense( + license: String, + location: TextLocation, + issues: MutableList, + detectedLicenseMapping: Map +): LicenseFinding? { + return runCatching { + // TODO: The detected license mapping must be applied here, because FossID can return license strings + // which cannot be parsed to an SpdxExpression. A better solution could be to automatically + // convert the strings into a form that can be parsed, then the mapping could be applied globally. + LicenseFinding(license.mapLicense(detectedLicenseMapping), location) + }.onSuccess { licenseFinding -> + licenseFinding.copy(license = licenseFinding.license.normalize()) + }.onFailure { spdxException -> + issues += FossId.createAndLogIssue( + source = "FossId", + message = "Failed to parse license '$license' as an SPDX expression: ${spdxException.collectMessages()}" + ) + }.getOrNull() +} + /** * Map the raw snippets to ORT [SnippetFinding]s. If a snippet license cannot be parsed, an issues is added to [issues]. * [LicenseFinding]s due to chosen snippets will be added to [snippetLicenseFindings].