Skip to content

Commit

Permalink
feat(scanner): Add flag to scanner to detect unlicensed files
Browse files Browse the repository at this point in the history
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 <kiko@erlang.org>
  • Loading branch information
kikofernandez committed Jan 21, 2025
1 parent cba5464 commit cdbb4b2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions model/src/main/kotlin/config/ScannerConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ data class ScannerConfiguration(
*/
val skipExcluded: Boolean = false,

/**
* A flag to indicate whether the scanner should add files without license to the scanner results.
*/
val includeFilesWithoutFindings: Boolean = false,

/**
* Configuration of a [FileArchiver] that archives certain scanned files in an external [FileStorage].
*/
Expand Down
26 changes: 25 additions & 1 deletion scanner/src/main/kotlin/Scanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ 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
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.TextLocation.Companion.UNKNOWN_LINE
import org.ossreviewtoolkit.model.VcsInfo
import org.ossreviewtoolkit.model.config.DownloaderConfiguration
import org.ossreviewtoolkit.model.config.ScannerConfiguration
Expand All @@ -67,6 +70,7 @@ import org.ossreviewtoolkit.utils.common.collectMessages
import org.ossreviewtoolkit.utils.common.safeDeleteRecursively
import org.ossreviewtoolkit.utils.ort.Environment
import org.ossreviewtoolkit.utils.ort.showStackTrace
import org.ossreviewtoolkit.utils.spdx.SpdxConstants
import org.ossreviewtoolkit.utils.spdx.toSpdx

const val TOOL_NAME = "scanner"
Expand Down Expand Up @@ -207,13 +211,33 @@ class Scanner(
}
}

val scanResults = if (!scannerConfig.includeFilesWithoutFindings) {
filteredScanResults.mapTo(mutableSetOf()) { scanResult ->
val allPaths = controller.getAllFileLists()[scanResult.provenance]?.files?.map { it.path }.orEmpty()

Check notice on line 216 in scanner/src/main/kotlin/Scanner.kt

View workflow job for this annotation

GitHub Actions / qodana-scan

Argument could be converted to 'Set' to improve performance

The argument can be converted to 'Set' to improve performance

Check notice

Code scanning / QDJVMC

Argument could be converted to 'Set' to improve performance Note

The argument can be converted to 'Set' to improve performance
val pathsWithFindings = scanResult.summary.licenseFindings.map { it.location.path }
val pathsWithoutFindings = allPaths - pathsWithFindings

val findingsThatAreNone = pathsWithoutFindings.map {
LicenseFinding(SpdxConstants.NOASSERTION, TextLocation(it, UNKNOWN_LINE))
}

scanResult.copy(
summary = scanResult.summary.copy(
licenseFindings = scanResult.summary.licenseFindings + findingsThatAreNone
)
)
}
} else {
filteredScanResults
}

val scannerNames = scannerWrappers.mapTo(mutableSetOf()) { it.name }
val scanners = packages.associateBy({ it.id }) { scannerNames }

return ScannerRun.EMPTY.copy(
config = scannerConfig,
provenances = provenances,
scanResults = filteredScanResults,
scanResults = scanResults,
files = files,
scanners = scanners
)
Expand Down

0 comments on commit cdbb4b2

Please sign in to comment.