From a9fd947cc8e3e28d154bb00423334ee7f5b23f43 Mon Sep 17 00:00:00 2001 From: Kiko Fernandez-Reyes Date: Wed, 4 Dec 2024 15:35:14 +0100 Subject: [PATCH] feat(scanner): Add flag to scanner to detect unlicensed files Add flag `includeUnlicensed` to the scanner configuration. Its default is `false`. When set to `true`, the scanner add to a `ScanResult` files without license as LicenseFindings with license set to `NONE`. This contribution makes possible to the scanner to display all files as license findings. The ultimate goal is that any file without license is catched by the scanner, so that curation mechanism can override files without licenses in cases where a license applies to a whole folder. Signed-off-by: Kiko Fernandez-Reyes --- .../kotlin/config/ScannerConfiguration.kt | 5 +++ scanner/src/main/kotlin/Scanner.kt | 38 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/model/src/main/kotlin/config/ScannerConfiguration.kt b/model/src/main/kotlin/config/ScannerConfiguration.kt index dedc38a1cbe64..b924cac5984e2 100644 --- a/model/src/main/kotlin/config/ScannerConfiguration.kt +++ b/model/src/main/kotlin/config/ScannerConfiguration.kt @@ -39,6 +39,11 @@ data class ScannerConfiguration( */ val skipConcluded: Boolean = false, + /** + * A flag to indicate whether the scanner should add files without license to the scanner results. + */ + val includeUnlicensed: Boolean = false, + /** * A flag to control whether excluded scopes and paths should be skipped during the scan. */ diff --git a/scanner/src/main/kotlin/Scanner.kt b/scanner/src/main/kotlin/Scanner.kt index 8747a2c411358..4d4f3b371ea5c 100644 --- a/scanner/src/main/kotlin/Scanner.kt +++ b/scanner/src/main/kotlin/Scanner.kt @@ -36,6 +36,7 @@ import org.ossreviewtoolkit.model.FileList import org.ossreviewtoolkit.model.Identifier import org.ossreviewtoolkit.model.Issue import org.ossreviewtoolkit.model.KnownProvenance +import org.ossreviewtoolkit.model.LicenseFinding import org.ossreviewtoolkit.model.OrtResult import org.ossreviewtoolkit.model.Package import org.ossreviewtoolkit.model.PackageType @@ -43,6 +44,7 @@ import org.ossreviewtoolkit.model.ProvenanceResolutionResult import org.ossreviewtoolkit.model.ScanResult import org.ossreviewtoolkit.model.ScanSummary import org.ossreviewtoolkit.model.ScannerRun +import org.ossreviewtoolkit.model.TextLocation import org.ossreviewtoolkit.model.VcsInfo import org.ossreviewtoolkit.model.config.DownloaderConfiguration import org.ossreviewtoolkit.model.config.ScannerConfiguration @@ -192,8 +194,6 @@ class Scanner( val vcsPathsForProvenances = getVcsPathsForProvenances(provenances) - val filteredScanResults = filterScanResultsByVcsPaths(controller.getAllScanResults(), vcsPathsForProvenances) - val files = controller.getAllFileLists().mapTo(mutableSetOf()) { (provenance, fileList) -> FileList( provenance = provenance.alignRevisions() as KnownProvenance, @@ -207,6 +207,40 @@ class Scanner( } } + val filteredScanResults = filterScanResultsByVcsPaths(controller.getAllScanResults(), vcsPathsForProvenances) + .mapTo(mutableSetOf()) { scanResult -> + val licenseFiles = scanResult.summary.licenseFindings.mapTo(mutableSetOf()) { licenseFinding -> + licenseFinding.location.path + } + + if (!scannerConfig.includeUnlicensed) { + scanResult.copy(provenance = scanResult.provenance.alignRevisions()) + } else { + // Adds files without license to the scanned results + val scanSummary = + controller.getAllFileLists()[scanResult.provenance]?.files + .orEmpty().asSequence().mapNotNull { fileEntry -> + if (fileEntry.path in licenseFiles) { + null + } else { + fileEntry.path + } + }.toSet().let { fileEntryFindings -> + (fileEntryFindings subtract licenseFiles).mapTo(mutableSetOf()) { + LicenseFinding(license = "NONE", location = TextLocation(fileEntryFindings, 1)) + }.let { + val allFindings = scanResult.summary.licenseFindings union fileEntryFindings + scanResult.summary.copy(licenseFindings = allFindings) + } + } + + scanResult.copy( + provenance = scanResult.provenance.alignRevisions(), + summary = scanSummary + ) + } + } + val scannerNames = scannerWrappers.mapTo(mutableSetOf()) { it.name } val scanners = packages.associateBy({ it.id }) { scannerNames }