Skip to content

Commit

Permalink
refactor(clearly-defined): Simplify the API by using coordinates
Browse files Browse the repository at this point in the history
Introduce convenience extension functions that take `Coordinates`
instead of individual properties. Note that is it no possible to use a
`Coordinates`'s string representation directly as a Retrofit `@Path` as
the contained slashes would get escapted as %2F instead of being used
literally for the path.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Oct 11, 2023
1 parent ec843ea commit 804d959
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,7 @@ class ClearlyDefinedServiceFunTest : WordSpec({
"return single curation data" {
val service = ClearlyDefinedService.create()

val curation = service.getCuration(
coordinates.type,
coordinates.provider,
coordinates.namespace ?: "-",
coordinates.name,
coordinates.revision.orEmpty()
)
val curation = service.getCuration(coordinates)

curation.licensed?.declared shouldBe "CDDL-1.0 OR GPL-2.0-only WITH Classpath-exception-2.0"
}
Expand Down
22 changes: 22 additions & 0 deletions clients/clearly-defined/src/main/kotlin/ClearlyDefinedService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,28 @@ interface ClearlyDefinedService {
): ResponseBody
}

suspend fun ClearlyDefinedService.getCuration(coordinates: Coordinates): Curation =
@Suppress("DestructuringDeclarationWithTooManyEntries")
coordinates.strings.let { (_, _, namespace, name, revision) ->
getCuration(coordinates.type, coordinates.provider, namespace, name, revision)
}

suspend fun ClearlyDefinedService.harvestTools(coordinates: Coordinates): List<String> =
@Suppress("DestructuringDeclarationWithTooManyEntries")
coordinates.strings.let { (_, _, namespace, name, revision) ->
harvestTools(coordinates.type, coordinates.provider, namespace, name, revision)
}

suspend fun ClearlyDefinedService.harvestToolData(
coordinates: Coordinates,
tool: String,
toolVersion: String
): ResponseBody =
@Suppress("DestructuringDeclarationWithTooManyEntries")
coordinates.strings.let { (_, _, namespace, name, revision) ->
harvestToolData(coordinates.type, coordinates.provider, namespace, name, revision, tool, toolVersion)
}

suspend fun <T> ClearlyDefinedService.call(block: suspend ClearlyDefinedService.() -> T): T =
try {
block()
Expand Down
21 changes: 4 additions & 17 deletions scanner/src/main/kotlin/storages/ClearlyDefinedStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import org.apache.logging.log4j.kotlin.logger
import org.ossreviewtoolkit.clients.clearlydefined.ClearlyDefinedService
import org.ossreviewtoolkit.clients.clearlydefined.ComponentType
import org.ossreviewtoolkit.clients.clearlydefined.Coordinates
import org.ossreviewtoolkit.clients.clearlydefined.harvestToolData
import org.ossreviewtoolkit.clients.clearlydefined.harvestTools
import org.ossreviewtoolkit.clients.clearlydefined.toCoordinates
import org.ossreviewtoolkit.downloader.VcsHost
import org.ossreviewtoolkit.model.ArtifactProvenance
Expand Down Expand Up @@ -102,13 +104,7 @@ class ClearlyDefinedStorage(
return runCatching {
logger.debug { "Looking up ClearlyDefined scan results for '$coordinates'." }

val tools = service.harvestTools(
coordinates.type,
coordinates.provider,
coordinates.namespace ?: "-",
coordinates.name,
coordinates.revision.orEmpty()
)
val tools = service.harvestTools(coordinates)

val toolVersionsByName = tools.mapNotNull { it.withoutPrefix("$coordinates/") }
.groupBy({ it.substringBefore('/') }, { it.substringAfter('/') })
Expand Down Expand Up @@ -181,16 +177,7 @@ class ClearlyDefinedStorage(
* and return it as a [JsonNode].
*/
private suspend fun loadToolData(coordinates: Coordinates, name: String, version: String): JsonNode {
val toolData = service.harvestToolData(
coordinates.type,
coordinates.provider,
coordinates.namespace ?: "-",
coordinates.name,
coordinates.revision.orEmpty(),
name,
version
)

val toolData = service.harvestToolData(coordinates, name, version)
return toolData.use { jsonMapper.readTree(it.byteStream()) }
}

Expand Down

0 comments on commit 804d959

Please sign in to comment.