Skip to content

Commit a998949

Browse files
committed
refactor(scanoss): Replace direct API calls with SCANOSS SDK
Replace custom direct API calls to SCANOSS with the official Java SDK. This change improves maintainability by leveraging the SDK's functionality instead of maintaining custom implementation for API interactions. Signed-off-by: Agustin Isasmendi <[email protected]>
1 parent 8465469 commit a998949

File tree

3 files changed

+9
-39
lines changed

3 files changed

+9
-39
lines changed

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

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@
1919

2020
package org.ossreviewtoolkit.plugins.scanners.scanoss
2121

22-
import com.scanoss.Winnowing
23-
import com.scanoss.rest.ScanApi
22+
import com.scanoss.Scanner
2423
import com.scanoss.utils.JsonUtils
2524
import com.scanoss.utils.PackageDetails
2625

2726
import java.io.File
2827
import java.time.Instant
2928

30-
import org.apache.logging.log4j.kotlin.logger
31-
3229
import org.ossreviewtoolkit.model.ScanSummary
3330
import org.ossreviewtoolkit.plugins.api.OrtPlugin
3431
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
@@ -37,7 +34,6 @@ import org.ossreviewtoolkit.scanner.ScanContext
3734
import org.ossreviewtoolkit.scanner.ScannerMatcher
3835
import org.ossreviewtoolkit.scanner.ScannerMatcherConfig
3936
import org.ossreviewtoolkit.scanner.ScannerWrapperFactory
40-
import org.ossreviewtoolkit.utils.common.VCS_DIRECTORIES
4137

4238
@OrtPlugin(
4339
id = "SCANOSS",
@@ -49,11 +45,10 @@ class ScanOss(
4945
override val descriptor: PluginDescriptor = ScanOssFactory.descriptor,
5046
config: ScanOssConfig
5147
) : PathScannerWrapper {
52-
private val service = ScanApi.builder()
48+
private val scanossBuilder = Scanner.builder()
5349
// As there is only a single endpoint, the SCANOSS API client expects the path to be part of the API URL.
5450
.url(config.apiUrl.removeSuffix("/") + "/scan/direct")
5551
.apiKey(config.apiKey.value)
56-
.build()
5752

5853
override val version: String by lazy {
5954
// TODO: Find out the best / cheapest way to query the SCANOSS server for its version.
@@ -81,30 +76,16 @@ class ScanOss(
8176
override fun scanPath(path: File, context: ScanContext): ScanSummary {
8277
val startTime = Instant.now()
8378

84-
val wfpString = buildString {
85-
path.walk()
86-
.onEnter { it.name !in VCS_DIRECTORIES }
87-
.filterNot { it.isDirectory }
88-
.forEach {
89-
logger.info { "Computing fingerprint for file ${it.absolutePath}..." }
90-
append(createWfpForFile(it))
91-
}
92-
}
93-
94-
val result = service.scan(
95-
wfpString,
96-
context.labels["scanOssContext"],
97-
context.labels["scanOssId"]?.toIntOrNull() ?: Thread.currentThread().threadId().toInt()
98-
)
79+
// Build the scanner at function level in case any path-specific settings or filters are needed later
80+
val scanoss = scanossBuilder.build()
9981

100-
// Replace the anonymized UUIDs by their file paths.
101-
val results = JsonUtils.toScanFileResultsFromObject(JsonUtils.toJsonObject(result))
82+
val rawResults = when {
83+
path.isFile -> listOf(scanoss.scanFile(path.toString()))
84+
else -> scanoss.scanFolder(path.toString())
85+
}
10286

87+
val results = JsonUtils.toScanFileResults(rawResults)
10388
val endTime = Instant.now()
10489
return generateSummary(startTime, endTime, results)
10590
}
106-
107-
internal fun createWfpForFile(file: File): String {
108-
return Winnowing.builder().build().wfpForFile(file.path, file.path)
109-
}
11091
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import io.kotest.matchers.collections.shouldContainExactly
2828
import io.kotest.matchers.should
2929

3030
import io.mockk.spyk
31-
import io.mockk.verify
3231

3332
import java.io.File
3433

@@ -77,11 +76,6 @@ class ScanOssScannerDirectoryTest : StringSpec({
7776
ScanContext(labels = emptyMap(), packageType = PackageType.PACKAGE)
7877
)
7978

80-
verify(exactly = 1) {
81-
scanner.createWfpForFile(TEST_DIRECTORY_TO_SCAN.resolve("ArchiveUtils.kt"))
82-
scanner.createWfpForFile(TEST_DIRECTORY_TO_SCAN.resolve("ScannerFactory.kt"))
83-
}
84-
8579
with(summary) {
8680
licenseFindings should containExactlyInAnyOrder(
8781
LicenseFinding(

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import io.kotest.matchers.collections.containExactly
2727
import io.kotest.matchers.should
2828

2929
import io.mockk.spyk
30-
import io.mockk.verify
3130

3231
import java.io.File
3332

@@ -70,10 +69,6 @@ class ScanOssScannerFileTest : StringSpec({
7069
ScanContext(labels = emptyMap(), packageType = PackageType.PACKAGE)
7170
)
7271

73-
verify(exactly = 1) {
74-
scanner.createWfpForFile(TEST_FILE_TO_SCAN)
75-
}
76-
7772
with(summary) {
7873
licenseFindings should containExactly(
7974
LicenseFinding(

0 commit comments

Comments
 (0)