Skip to content

Commit

Permalink
refactor(bazel): Simplify creating Bazel module registries
Browse files Browse the repository at this point in the history
There were multiple places that checked and transformed URLs to create
either local or remote Bazel module registry service instances.
Centralize this logic, so that redundancy can be reduced.

Signed-off-by: Oliver Heger <oliver.heger@bosch.io>
  • Loading branch information
oheger-bosch committed Aug 6, 2024
1 parent b0bddf9 commit 5dd19ff
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,35 @@ import java.io.File
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.decodeFromStream

import org.apache.logging.log4j.kotlin.logger

private const val BAZEL_MODULES_DIR = "modules"

/**
* A Bazel registry which is located on the local file system.
*/
class LocalBazelModuleRegistryService(directory: File) : BazelModuleRegistryService {
companion object {
/** A prefix for URLs pointing to local files. */
private const val FILE_URL_PREFIX = "file://"

/** Constant for a placeholder that is replaced by the current project directory. */
private const val WORKSPACE_PLACEHOLDER = "%workspace%"

/**
* Create a [LocalBazelModuleRegistryService] if the given [url] points to a local file. In this case,
* also replace the placeholder for the workspace by the given [projectDir]. Return *null* for all other URLs.
*/
fun createForLocalUrl(url: String?, projectDir: File): LocalBazelModuleRegistryService? =
url.takeIf { it?.startsWith(FILE_URL_PREFIX) == true }?.let { fileUrl ->
val directory = fileUrl.removePrefix(FILE_URL_PREFIX)
.replace(WORKSPACE_PLACEHOLDER, projectDir.absolutePath)

logger.info { "Creating local Bazel module registry at '$directory'." }
LocalBazelModuleRegistryService(File(directory))
}
}

private val moduleDirectory: File
private val bazelRegistry: BazelRegistry

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package org.ossreviewtoolkit.clients.bazelmoduleregistry
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient

import org.apache.logging.log4j.kotlin.logger

import retrofit2.Retrofit
import retrofit2.converter.kotlinx.serialization.asConverterFactory
import retrofit2.http.GET
Expand All @@ -46,9 +48,13 @@ interface RemoteBazelModuleRegistryService : BazelModuleRegistryService {
val bmrClient = client ?: OkHttpClient()

val contentType = "application/json".toMediaType()
val baseUrl = url ?: DEFAULT_URL

logger.info { "Creating remote Bazel module registry at '$baseUrl'." }

val retrofit = Retrofit.Builder()
.client(bmrClient)
.baseUrl(url ?: DEFAULT_URL)
.baseUrl(baseUrl)
.addConverterFactory(JSON.asConverterFactory(contentType))
.build()

Expand Down
20 changes: 3 additions & 17 deletions plugins/package-managers/bazel/src/main/kotlin/Bazel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -145,27 +145,13 @@ class Bazel(
if (lockfile.flags != null) {
val registryUrl = lockfile.registryUrl()

return registryUrl.withoutPrefix("file://")?.let {
val localRegistryURL = it.replace("%workspace%", projectDir.absolutePath)
logger.info {
"Using local Bazel module registry at '$localRegistryURL'."
}

LocalBazelModuleRegistryService(File(localRegistryURL))
} ?: RemoteBazelModuleRegistryService.create(registryUrl)
return LocalBazelModuleRegistryService.createForLocalUrl(registryUrl, projectDir)
?: RemoteBazelModuleRegistryService.create(registryUrl)
}

// Bazel version >= 7.2.0.
if (lockfile.registryFileHashes != null) {
val registryFileHashes = lockfile.registryFileHashes.map { (url, _) ->
if (url.startsWith("file://")) {
url.replace("%workspace%", projectDir.absolutePath)
} else {
url
}
}.toSet()

return CompositeBazelModuleRegistryService.create(registryFileHashes)
return CompositeBazelModuleRegistryService.create(lockfile.registryFileHashes.keys, projectDir)
}

val msg = "Bazel registry URL cannot be determined from the lockfile."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ internal class CompositeBazelModuleRegistryService(
internal val URL_REGEX = "^(?<server>.*/)modules/(?<package>[^/]+)/[^/]+/source\\.json$".toRegex()

/**
* Create a Composite Bazel Module Registry client instance.
* Create a Composite Bazel Module Registry client instance. The wrapped [BazelModuleRegistryService]s are
* created based on the passed in [urls]; local registries use the given [projectDir] as workspace.
*/
fun create(urls: Set<String>): CompositeBazelModuleRegistryService {
fun create(urls: Set<String>, projectDir: File): CompositeBazelModuleRegistryService {
val packageNamesForServer = urls.filter { it.endsWith("source.json") }.mapNotNull { url ->
val groups = URL_REGEX.matchEntire(url)?.groups

Expand All @@ -63,19 +64,8 @@ internal class CompositeBazelModuleRegistryService(
}.groupByTo(mutableMapOf(), { it.first }) { it.second }.mapValues { it.value.toSet() }

val packageNamesForRegistry = packageNamesForServer.mapKeys { (url, _) ->
if (url.startsWith("file://")) {
logger.info {
"Using local Bazel module registry at '$url'."
}

LocalBazelModuleRegistryService(File(url))
} else {
logger.info {
"Using remote Bazel module registry at '$url'."
}

RemoteBazelModuleRegistryService.create(url)
}
LocalBazelModuleRegistryService.createForLocalUrl(url, projectDir)
?: RemoteBazelModuleRegistryService.create(url)
}

return CompositeBazelModuleRegistryService(packageNamesForRegistry)
Expand Down

0 comments on commit 5dd19ff

Please sign in to comment.