Skip to content

Commit

Permalink
fix(Bazel): Support local registries
Browse files Browse the repository at this point in the history
The current implementation of Bazel only supports remote registries. This
commit extends the implementation to also support registries on the local
filesystem. They are called "Index Registries" (see [1]).

The variable "%workspace%" present in the test data is a built-in variable
of Bazel (see [2]).

[1]: https://docs.bazel.build/versions/5.1.0/bzlmod.html#index-registry
[2]: https://bazel.build/run/bazelrc#bazelrc-syntax-semantics

Signed-off-by: Nicolas Nobelis <[email protected]>
  • Loading branch information
nnobelis authored and sschuberth committed Jun 24, 2024
1 parent 50c0512 commit 5968180
Show file tree
Hide file tree
Showing 9 changed files with 1,805 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.clients.bazelmoduleregistry

import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonNamingStrategy

/**
* The JSON (de-)serialization object used by this client.
*/
internal val JSON = Json {
ignoreUnknownKeys = true
namingStrategy = JsonNamingStrategy.SnakeCase
}

/**
* A Bazel registry which is either local or remote.
*/
interface BazelModuleRegistryService {
/**
* Retrieve the metadata for a module.
*/
suspend fun getModuleMetadata(name: String): ModuleMetadata

/**
* Retrieve information about the source code for a specific version of a module.
*/
suspend fun getModuleSourceInfo(name: String, version: String): ModuleSourceInfo
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.clients.bazelmoduleregistry

import java.io.File

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.decodeFromStream

/**
* A Bazel registry which is located on the local file system.
*/
class LocalBazelModuleRegistryService(directory: File) : BazelModuleRegistryService {
private val moduleDirectory: File

init {
val registryFile = directory.resolve("bazel_registry.json")

require(registryFile.isFile) {
"The Bazel registry file bazel_registry.json does not exist in '${directory.canonicalPath}'."
}

val bazelRegistry = registryFile.inputStream().use {
JSON.decodeFromStream<BazelRegistry>(it)
}

moduleDirectory = registryFile.resolve(bazelRegistry.moduleBasePath)
}

override suspend fun getModuleMetadata(name: String): ModuleMetadata {
val metadataJson = moduleDirectory.resolve(name).resolve("metadata.json")
require(metadataJson.isFile)
return metadataJson.inputStream().use {
JSON.decodeFromStream<ModuleMetadata>(it)
}
}

override suspend fun getModuleSourceInfo(name: String, version: String): ModuleSourceInfo {
val sourceJson = moduleDirectory.resolve(name).resolve(version).resolve("source.json")
require(sourceJson.isFile)
return sourceJson.inputStream().use {
JSON.decodeFromStream<ModuleSourceInfo>(it)
}
}
}

@Serializable
private data class BazelRegistry(
val moduleBasePath: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@

package org.ossreviewtoolkit.clients.bazelmoduleregistry

import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonNamingStrategy

import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient

Expand All @@ -30,14 +27,6 @@ import retrofit2.converter.kotlinx.serialization.asConverterFactory
import retrofit2.http.GET
import retrofit2.http.Path

/**
* The JSON (de-)serialization object used by this client.
*/
private val JSON = Json {
ignoreUnknownKeys = true
namingStrategy = JsonNamingStrategy.SnakeCase
}

/**
* The client uses the Bazel Central Registry by default.
*/
Expand All @@ -47,7 +36,7 @@ private const val DEFAULT_URL = "https://bcr.bazel.build"
* Interface for a Bazel Module Registry, based on the directory structure of https://bcr.bazel.build/ and the Git
* repository it is based on (https://github.com/bazelbuild/bazel-central-registry/).
*/
interface RemoteBazelModuleRegistryService {
interface RemoteBazelModuleRegistryService : BazelModuleRegistryService {
companion object {
/**
* Create a Bazel Module Registry client instance for communicating with a server running at the given [url],
Expand All @@ -72,12 +61,15 @@ interface RemoteBazelModuleRegistryService {
* E.g. https://bcr.bazel.build/modules/glog/metadata.json.
*/
@GET("modules/{name}/metadata.json")
suspend fun getModuleMetadata(@Path("name") name: String): ModuleMetadata
override suspend fun getModuleMetadata(@Path("name") name: String): ModuleMetadata

/**
* Retrieve information about the source code for a specific version of a module.
* E.g. https://bcr.bazel.build/modules/glog/0.5.0/source.json.
*/
@GET("modules/{name}/{version}/source.json")
suspend fun getModuleSourceInfo(@Path("name") name: String, @Path("version") version: String): ModuleSourceInfo
override suspend fun getModuleSourceInfo(
@Path("name") name: String,
@Path("version") version: String
): ModuleSourceInfo
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
common --registry=file://%workspace%/tools/bazelbuild/registry/

common --registry=https://raw.githubusercontent.com/bazelbuild/bazel-central-registry/main/
Loading

0 comments on commit 5968180

Please sign in to comment.