Skip to content

Commit

Permalink
feat(bazel): Prepare for other types of module source info
Browse files Browse the repository at this point in the history
The `source.json` files can have different types (see [1]). Currently, ORT
only supports the "archive" type. This commit refactors the code to be able
to deserialize the other types in the future. Among other changes, it adds
a polymorphic deserializer that defaults to the "archive" type, since the
latter can be implicit according to the specification.

[1]: https://bazel.build/external/registry

Signed-off-by: Nicolas Nobelis <nicolas.nobelis@bosch.com>
  • Loading branch information
nnobelis authored and sschuberth committed Sep 20, 2024
1 parent adeb51e commit 573b86f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.collections.shouldContain
import io.kotest.matchers.comparables.shouldBeGreaterThan
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeTypeOf

class BazelModuleRegistryClientFunTest : WordSpec({
val client = RemoteBazelModuleRegistryService.create()
Expand All @@ -42,6 +43,7 @@ class BazelModuleRegistryClientFunTest : WordSpec({
"include URL and hash data" {
val sourceInfo = client.getModuleSourceInfo("glog", "0.5.0")

sourceInfo.shouldBeTypeOf<ArchiveSourceInfo>()
sourceInfo.url.toString() shouldBe "$repoUrl/archive/refs/tags/v0.5.0.tar.gz"
sourceInfo.integrity shouldBe "sha256-7t5x8oNxvzmqabRd4jsynTchQBbiBVJps7Xnz9QLWfU="
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ package org.ossreviewtoolkit.clients.bazelmoduleregistry

import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonNamingStrategy
import kotlinx.serialization.modules.SerializersModule

/**
* The JSON (de-)serialization object used by this client.
*/
internal val JSON = Json {
ignoreUnknownKeys = true
namingStrategy = JsonNamingStrategy.SnakeCase
serializersModule = SerializersModule {
polymorphicDefaultDeserializer(ModuleSourceInfo::class) {
ArchiveSourceInfo.serializer()
}
}
}

/**
Expand Down
13 changes: 11 additions & 2 deletions clients/bazel-module-registry/src/main/kotlin/Model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import io.ks3.java.typealiases.UriAsString

import java.net.URI

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/**
Expand All @@ -45,16 +46,24 @@ data class ModuleMetadata(
}

/**
* An abstraction of a module source info.
*/
@Serializable
sealed class ModuleSourceInfo

/**
* A module source info with type 'archive'.
* E.g. https://bcr.bazel.build/modules/glog/0.5.0/source.json.
*/
@Serializable
data class ModuleSourceInfo(
@SerialName("archive")
class ArchiveSourceInfo(
val integrity: String,
val patchStrip: Int? = null,
val patches: Map<String, String>? = null,
val stripPrefix: String? = null,
val url: UriAsString
)
) : ModuleSourceInfo()

/**
* See https://bazel.build/rules/lib/globals/module#archive_override.
Expand Down
21 changes: 13 additions & 8 deletions plugins/package-managers/bazel/src/main/kotlin/Bazel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.apache.logging.log4j.kotlin.logger
import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory
import org.ossreviewtoolkit.analyzer.PackageManager
import org.ossreviewtoolkit.clients.bazelmoduleregistry.ArchiveOverride
import org.ossreviewtoolkit.clients.bazelmoduleregistry.ArchiveSourceInfo
import org.ossreviewtoolkit.clients.bazelmoduleregistry.BazelModuleRegistryService
import org.ossreviewtoolkit.clients.bazelmoduleregistry.LocalBazelModuleRegistryService
import org.ossreviewtoolkit.clients.bazelmoduleregistry.METADATA_JSON
Expand Down Expand Up @@ -444,13 +445,17 @@ private fun ModuleMetadata.toVcsInfo() =
)

private fun ModuleSourceInfo.toRemoteArtifact(): RemoteArtifact {
val (algo, b64digest) = integrity.split("-", limit = 2)
val digest = Base64.decode(b64digest).toHexString()

val hash = Hash(
value = digest,
algorithm = HashAlgorithm.fromString(algo)
)
when (this) {
is ArchiveSourceInfo -> {
val (algo, b64digest) = integrity.split("-", limit = 2)
val digest = Base64.decode(b64digest).toHexString()

val hash = Hash(
value = digest,
algorithm = HashAlgorithm.fromString(algo)
)

return RemoteArtifact(url = url.toString(), hash = hash)
return RemoteArtifact(url = url.toString(), hash = hash)
}
}
}

0 comments on commit 573b86f

Please sign in to comment.