Skip to content

Commit 3d012e2

Browse files
committed
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 <[email protected]>
1 parent 44a175a commit 3d012e2

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

model/src/main/kotlin/config/ScannerConfiguration.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ data class ScannerConfiguration(
3939
*/
4040
val skipConcluded: Boolean = false,
4141

42+
/**
43+
* A flag to indicate whether the scanner should add files without license to the scanner results.
44+
*/
45+
val includeUnlicensed: Boolean = false,
46+
4247
/**
4348
* A flag to control whether excluded scopes and paths should be skipped during the scan.
4449
*/

scanner/src/main/kotlin/Scanner.kt

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ import org.ossreviewtoolkit.model.FileList
3636
import org.ossreviewtoolkit.model.Identifier
3737
import org.ossreviewtoolkit.model.Issue
3838
import org.ossreviewtoolkit.model.KnownProvenance
39+
import org.ossreviewtoolkit.model.LicenseFinding
3940
import org.ossreviewtoolkit.model.OrtResult
4041
import org.ossreviewtoolkit.model.Package
4142
import org.ossreviewtoolkit.model.PackageType
4243
import org.ossreviewtoolkit.model.ProvenanceResolutionResult
4344
import org.ossreviewtoolkit.model.ScanResult
4445
import org.ossreviewtoolkit.model.ScanSummary
4546
import org.ossreviewtoolkit.model.ScannerRun
47+
import org.ossreviewtoolkit.model.TextLocation
4648
import org.ossreviewtoolkit.model.VcsInfo
4749
import org.ossreviewtoolkit.model.config.DownloaderConfiguration
4850
import org.ossreviewtoolkit.model.config.ScannerConfiguration
@@ -192,8 +194,6 @@ class Scanner(
192194

193195
val vcsPathsForProvenances = getVcsPathsForProvenances(provenances)
194196

195-
val filteredScanResults = filterScanResultsByVcsPaths(controller.getAllScanResults(), vcsPathsForProvenances)
196-
197197
val files = controller.getAllFileLists().mapTo(mutableSetOf()) { (provenance, fileList) ->
198198
FileList(
199199
provenance = provenance.alignRevisions() as KnownProvenance,
@@ -207,6 +207,40 @@ class Scanner(
207207
}
208208
}
209209

210+
val filteredScanResults = filterScanResultsByVcsPaths(controller.getAllScanResults(), vcsPathsForProvenances)
211+
.mapTo(mutableSetOf()) { scanResult ->
212+
val licenseFiles = scanResult.summary.licenseFindings.mapTo(mutableSetOf()) { licenseFinding ->
213+
licenseFinding.location.path
214+
}
215+
216+
if (!scannerConfig.includeUnlicensed) {
217+
scanResult.copy(provenance = scanResult.provenance.alignRevisions())
218+
} else {
219+
// Adds files without license to the scanned results
220+
val scanSummary =
221+
controller.getAllFileLists()[scanResult.provenance]?.files
222+
.orEmpty().asSequence().mapNotNull { fileEntry ->
223+
if (fileEntry.path in licenseFiles) {
224+
null
225+
} else {
226+
fileEntry.path
227+
}
228+
}.toSet().let {
229+
(it subtract licenseFiles).mapTo(mutableSetOf()) {
230+
LicenseFinding(license = "NONE", location = TextLocation(it, 1))
231+
}.let {
232+
val allFindings = scanResult.summary.licenseFindings union it
233+
scanResult.summary.copy(licenseFindings = allFindings)
234+
}
235+
}
236+
237+
scanResult.copy(
238+
provenance = scanResult.provenance.alignRevisions(),
239+
summary = scanSummary
240+
)
241+
}
242+
}
243+
210244
val scannerNames = scannerWrappers.mapTo(mutableSetOf()) { it.name }
211245
val scanners = packages.associateBy({ it.id }) { scannerNames }
212246

0 commit comments

Comments
 (0)