Skip to content

Commit

Permalink
refactor(scanner): Make resolveProvenance suspending
Browse files Browse the repository at this point in the history
Make the function suspending to avoid having to call `runBlocking` in
its implementations.

Signed-off-by: Martin Nonnenmacher <martin.nonnenmacher@bosch.com>
  • Loading branch information
mnonnenmacher authored and sschuberth committed Aug 20, 2024
1 parent f04cb07 commit 4e19363
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
13 changes: 7 additions & 6 deletions scanner/src/main/kotlin/provenance/PackageProvenanceResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ package org.ossreviewtoolkit.scanner.provenance
import java.io.IOException
import java.net.HttpURLConnection

import kotlinx.coroutines.runBlocking

import okhttp3.Request

import org.apache.logging.log4j.kotlin.logger
Expand Down Expand Up @@ -52,7 +50,7 @@ interface PackageProvenanceResolver {
*
* Throws an [IOException] if the provenance cannot be resolved.
*/
fun resolveProvenance(pkg: Package, defaultSourceCodeOrigins: List<SourceCodeOrigin>): KnownProvenance
suspend fun resolveProvenance(pkg: Package, defaultSourceCodeOrigins: List<SourceCodeOrigin>): KnownProvenance
}

/**
Expand All @@ -69,7 +67,10 @@ class DefaultPackageProvenanceResolver(
* provided by the [package][pkg] metadata does not exist or is missing, the function tries to guess the tag based
* on the name and version of the [package][pkg].
*/
override fun resolveProvenance(pkg: Package, defaultSourceCodeOrigins: List<SourceCodeOrigin>): KnownProvenance {
override suspend fun resolveProvenance(
pkg: Package,
defaultSourceCodeOrigins: List<SourceCodeOrigin>
): KnownProvenance {
val errors = mutableMapOf<SourceCodeOrigin, Throwable>()
val sourceCodeOrigins = pkg.sourceCodeOrigins ?: defaultSourceCodeOrigins

Expand All @@ -78,13 +79,13 @@ class DefaultPackageProvenanceResolver(
when (sourceCodeOrigin) {
SourceCodeOrigin.ARTIFACT -> {
if (pkg.sourceArtifact != RemoteArtifact.EMPTY) {
return runBlocking { resolveSourceArtifact(pkg) }
return resolveSourceArtifact(pkg)
}
}

SourceCodeOrigin.VCS -> {
if (pkg.vcsProcessed != VcsInfo.EMPTY) {
return runBlocking { resolveVcs(pkg) }
return resolveVcs(pkg)
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion scanner/src/test/kotlin/ScannerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,10 @@ private class FakeProvenanceDownloader(val filename: String = "fake.txt") : Prov
* validation.
*/
private class FakePackageProvenanceResolver : PackageProvenanceResolver {
override fun resolveProvenance(pkg: Package, defaultSourceCodeOrigins: List<SourceCodeOrigin>): KnownProvenance {
override suspend fun resolveProvenance(
pkg: Package,
defaultSourceCodeOrigins: List<SourceCodeOrigin>
): KnownProvenance {
defaultSourceCodeOrigins.forEach { sourceCodeOrigin ->
when (sourceCodeOrigin) {
SourceCodeOrigin.ARTIFACT -> {
Expand Down

0 comments on commit 4e19363

Please sign in to comment.