Skip to content

Commit 804d959

Browse files
committed
refactor(clearly-defined): Simplify the API by using coordinates
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]>
1 parent ec843ea commit 804d959

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

clients/clearly-defined/src/funTest/kotlin/ClearlyDefinedServiceFunTest.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,7 @@ class ClearlyDefinedServiceFunTest : WordSpec({
6464
"return single curation data" {
6565
val service = ClearlyDefinedService.create()
6666

67-
val curation = service.getCuration(
68-
coordinates.type,
69-
coordinates.provider,
70-
coordinates.namespace ?: "-",
71-
coordinates.name,
72-
coordinates.revision.orEmpty()
73-
)
67+
val curation = service.getCuration(coordinates)
7468

7569
curation.licensed?.declared shouldBe "CDDL-1.0 OR GPL-2.0-only WITH Classpath-exception-2.0"
7670
}

clients/clearly-defined/src/main/kotlin/ClearlyDefinedService.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,28 @@ interface ClearlyDefinedService {
289289
): ResponseBody
290290
}
291291

292+
suspend fun ClearlyDefinedService.getCuration(coordinates: Coordinates): Curation =
293+
@Suppress("DestructuringDeclarationWithTooManyEntries")
294+
coordinates.strings.let { (_, _, namespace, name, revision) ->
295+
getCuration(coordinates.type, coordinates.provider, namespace, name, revision)
296+
}
297+
298+
suspend fun ClearlyDefinedService.harvestTools(coordinates: Coordinates): List<String> =
299+
@Suppress("DestructuringDeclarationWithTooManyEntries")
300+
coordinates.strings.let { (_, _, namespace, name, revision) ->
301+
harvestTools(coordinates.type, coordinates.provider, namespace, name, revision)
302+
}
303+
304+
suspend fun ClearlyDefinedService.harvestToolData(
305+
coordinates: Coordinates,
306+
tool: String,
307+
toolVersion: String
308+
): ResponseBody =
309+
@Suppress("DestructuringDeclarationWithTooManyEntries")
310+
coordinates.strings.let { (_, _, namespace, name, revision) ->
311+
harvestToolData(coordinates.type, coordinates.provider, namespace, name, revision, tool, toolVersion)
312+
}
313+
292314
suspend fun <T> ClearlyDefinedService.call(block: suspend ClearlyDefinedService.() -> T): T =
293315
try {
294316
block()

scanner/src/main/kotlin/storages/ClearlyDefinedStorage.kt

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import org.apache.logging.log4j.kotlin.logger
3333
import org.ossreviewtoolkit.clients.clearlydefined.ClearlyDefinedService
3434
import org.ossreviewtoolkit.clients.clearlydefined.ComponentType
3535
import org.ossreviewtoolkit.clients.clearlydefined.Coordinates
36+
import org.ossreviewtoolkit.clients.clearlydefined.harvestToolData
37+
import org.ossreviewtoolkit.clients.clearlydefined.harvestTools
3638
import org.ossreviewtoolkit.clients.clearlydefined.toCoordinates
3739
import org.ossreviewtoolkit.downloader.VcsHost
3840
import org.ossreviewtoolkit.model.ArtifactProvenance
@@ -102,13 +104,7 @@ class ClearlyDefinedStorage(
102104
return runCatching {
103105
logger.debug { "Looking up ClearlyDefined scan results for '$coordinates'." }
104106

105-
val tools = service.harvestTools(
106-
coordinates.type,
107-
coordinates.provider,
108-
coordinates.namespace ?: "-",
109-
coordinates.name,
110-
coordinates.revision.orEmpty()
111-
)
107+
val tools = service.harvestTools(coordinates)
112108

113109
val toolVersionsByName = tools.mapNotNull { it.withoutPrefix("$coordinates/") }
114110
.groupBy({ it.substringBefore('/') }, { it.substringAfter('/') })
@@ -181,16 +177,7 @@ class ClearlyDefinedStorage(
181177
* and return it as a [JsonNode].
182178
*/
183179
private suspend fun loadToolData(coordinates: Coordinates, name: String, version: String): JsonNode {
184-
val toolData = service.harvestToolData(
185-
coordinates.type,
186-
coordinates.provider,
187-
coordinates.namespace ?: "-",
188-
coordinates.name,
189-
coordinates.revision.orEmpty(),
190-
name,
191-
version
192-
)
193-
180+
val toolData = service.harvestToolData(coordinates, name, version)
194181
return toolData.use { jsonMapper.readTree(it.byteStream()) }
195182
}
196183

0 commit comments

Comments
 (0)