Skip to content

Commit 4e5c3a5

Browse files
committed
feat(scanoss): Add exclusion pattern support to ScanOss
Implement a custom filter in the scanPath method that integrates with the ScanOSS SDK to properly handle file exclusions based on patterns. The filter converts paths appropriately to work with the context's exclude patterns. Signed-off-by: Agustin Isasmendi <agustin.isasmendi@scanoss.com>
1 parent 83e5bed commit 4e5c3a5

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

plugins/scanners/scanoss/src/main/kotlin/ScanOss.kt

+30-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
package org.ossreviewtoolkit.plugins.scanners.scanoss
2121

2222
import com.scanoss.Scanner
23+
import com.scanoss.filters.FilterConfig
2324
import com.scanoss.utils.JsonUtils
2425
import com.scanoss.utils.PackageDetails
2526

2627
import java.io.File
2728
import java.time.Instant
2829

30+
import org.apache.logging.log4j.kotlin.logger
31+
2932
import org.ossreviewtoolkit.model.ScanSummary
3033
import org.ossreviewtoolkit.plugins.api.OrtPlugin
3134
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
@@ -61,14 +64,39 @@ class ScanOss(
6164
override fun scanPath(path: File, context: ScanContext): ScanSummary {
6265
val startTime = Instant.now()
6366

67+
// basePath: The reference path used for creating relative paths.
68+
val basePath = path.toPath()
69+
70+
val filterConfig = FilterConfig.builder()
71+
.customFilter { currentPath ->
72+
// currentPath: A Path object representing the file or directory being evaluated by the filter.
73+
// This is provided by the Scanner and represents individual files/directories during traversal.
74+
// It is an absolute path to a file or directory within the scan target.
75+
try {
76+
// relativePath: The path of the current file relative to the base scan directory.
77+
// Example: If basePath is "/project" and currentPath is "/project/src/main/file.kt",
78+
// then relativePath becomes "src/main/file.kt".
79+
// This relative representation is what the exclusion patterns in context.excludes expect.
80+
val relativePath = basePath.relativize(currentPath).toString()
81+
val isExcluded = context.excludes?.isPathExcluded(relativePath) ?: false
82+
logger.debug("Path: $currentPath, relative: $relativePath, isExcluded: $isExcluded")
83+
isExcluded
84+
} catch (e: IllegalArgumentException) {
85+
logger.warn("Error processing path $currentPath: ${e.message}")
86+
false
87+
}
88+
}
89+
.build()
90+
6491
val scanoss = Scanner.builder()
6592
.url(config.apiUrl.removeSuffix("/") + "/scan/direct")
6693
.apiKey(config.apiKey.value)
94+
.filterConfig(filterConfig)
6795
.build()
6896

6997
val rawResults: List<String> = when {
70-
path.isFile -> listOf(scanoss.scanFile(path.absolutePath))
71-
else -> scanoss.scanFolder(path.absolutePath)
98+
path.isFile -> listOf(scanoss.scanFile(path.toString()))
99+
else -> scanoss.scanFolder(path.toString())
72100
}
73101

74102
val results = JsonUtils.toScanFileResults(rawResults)

plugins/scanners/scanoss/src/test/kotlin/ScanOssScannerDirectoryTest.kt

+27
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import io.kotest.core.spec.style.StringSpec
2626
import io.kotest.matchers.collections.containExactlyInAnyOrder
2727
import io.kotest.matchers.collections.shouldContainExactly
2828
import io.kotest.matchers.should
29+
import io.kotest.matchers.shouldBe
2930

3031
import io.mockk.spyk
3132

@@ -39,6 +40,9 @@ import org.ossreviewtoolkit.model.SnippetFinding
3940
import org.ossreviewtoolkit.model.TextLocation
4041
import org.ossreviewtoolkit.model.VcsInfo
4142
import org.ossreviewtoolkit.model.VcsType
43+
import org.ossreviewtoolkit.model.config.Excludes
44+
import org.ossreviewtoolkit.model.config.PathExclude
45+
import org.ossreviewtoolkit.model.config.PathExcludeReason
4246
import org.ossreviewtoolkit.plugins.api.Secret
4347
import org.ossreviewtoolkit.scanner.ScanContext
4448
import org.ossreviewtoolkit.utils.spdx.SpdxExpression
@@ -110,4 +114,27 @@ class ScanOssScannerDirectoryTest : StringSpec({
110114
)
111115
}
112116
}
117+
118+
"Scanner should exclude files matching the specified path pattern (*.kt files)" {
119+
val pathExcludes = listOf(
120+
PathExclude(
121+
pattern = "**/*.kt",
122+
reason = PathExcludeReason.BUILD_TOOL_OF,
123+
comment = "Excluding .kt source files from scanning"
124+
)
125+
)
126+
127+
val summary = scanner.scanPath(
128+
TEST_DIRECTORY_TO_SCAN,
129+
ScanContext(
130+
labels = emptyMap(),
131+
packageType = PackageType.PACKAGE,
132+
excludes = Excludes(paths = pathExcludes)
133+
)
134+
)
135+
136+
summary.licenseFindings.size shouldBe 0
137+
summary.copyrightFindings.size shouldBe 0
138+
summary.snippetFindings.size shouldBe 0
139+
}
113140
})

0 commit comments

Comments
 (0)