From b1dd96a3344f3d868f74bcba01a994b087fe89fb Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Tue, 13 Aug 2024 20:44:07 +0200 Subject: [PATCH] fix(bazel): Distict registry URLs by their normalized form This avoids output like Creating remote Bazel module registry at 'https://bcr.bazel.build/'. Creating remote Bazel module registry at 'https://bcr.bazel.build'. Signed-off-by: Sebastian Schuberth --- .../src/main/kotlin/RemoteBazelModuleRegistryService.kt | 2 +- .../main/kotlin/CompositeBazelModuleRegistryService.kt | 2 +- .../src/main/kotlin/MultiBazelModuleRegistryService.kt | 9 +++++---- .../test/kotlin/MultiBazelModuleRegistryServiceTest.kt | 3 ++- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/clients/bazel-module-registry/src/main/kotlin/RemoteBazelModuleRegistryService.kt b/clients/bazel-module-registry/src/main/kotlin/RemoteBazelModuleRegistryService.kt index fdc2e96b31110..3dfe4ecac99e5 100644 --- a/clients/bazel-module-registry/src/main/kotlin/RemoteBazelModuleRegistryService.kt +++ b/clients/bazel-module-registry/src/main/kotlin/RemoteBazelModuleRegistryService.kt @@ -32,7 +32,7 @@ import retrofit2.http.Path /** * The client uses the Bazel Central Registry by default. */ -private const val DEFAULT_URL = "https://bcr.bazel.build" +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 diff --git a/plugins/package-managers/bazel/src/main/kotlin/CompositeBazelModuleRegistryService.kt b/plugins/package-managers/bazel/src/main/kotlin/CompositeBazelModuleRegistryService.kt index 5d43139199975..20232dd028d64 100644 --- a/plugins/package-managers/bazel/src/main/kotlin/CompositeBazelModuleRegistryService.kt +++ b/plugins/package-managers/bazel/src/main/kotlin/CompositeBazelModuleRegistryService.kt @@ -47,7 +47,7 @@ internal class CompositeBazelModuleRegistryService( * created based on the passed in [urls]; local registries use the given [projectDir] as workspace. */ fun create(urls: Collection, projectDir: File): CompositeBazelModuleRegistryService { - val packageNamesForServer = urls.filter { it.endsWith("source.json") }.mapNotNull { url -> + val packageNamesForServer = urls.distinct().filter { it.endsWith("source.json") }.mapNotNull { url -> val groups = URL_REGEX.matchEntire(url)?.groups val serverName = groups?.get("server")?.value ?: let { diff --git a/plugins/package-managers/bazel/src/main/kotlin/MultiBazelModuleRegistryService.kt b/plugins/package-managers/bazel/src/main/kotlin/MultiBazelModuleRegistryService.kt index 2ac020297d7c9..fda9c42a6a5bb 100644 --- a/plugins/package-managers/bazel/src/main/kotlin/MultiBazelModuleRegistryService.kt +++ b/plugins/package-managers/bazel/src/main/kotlin/MultiBazelModuleRegistryService.kt @@ -22,6 +22,7 @@ package org.ossreviewtoolkit.plugins.packagemanagers.bazel import java.io.File import org.ossreviewtoolkit.clients.bazelmoduleregistry.BazelModuleRegistryService +import org.ossreviewtoolkit.clients.bazelmoduleregistry.DEFAULT_URL import org.ossreviewtoolkit.clients.bazelmoduleregistry.LocalBazelModuleRegistryService import org.ossreviewtoolkit.clients.bazelmoduleregistry.ModuleMetadata import org.ossreviewtoolkit.clients.bazelmoduleregistry.ModuleSourceInfo @@ -46,14 +47,14 @@ internal class MultiBazelModuleRegistryService( * the last service a remote module registry for the Bazel Central Registry is added that serves as a fallback. */ fun create(urls: Collection, projectDir: File): MultiBazelModuleRegistryService { - val registryServices = urls.mapTo(mutableListOf()) { url -> + // Add the default Bazel registry as a fallback. + val registryUrls = (urls + DEFAULT_URL).distinctBy { it.removeSuffix("/") } + + val registryServices = registryUrls.mapTo(mutableListOf()) { url -> LocalBazelModuleRegistryService.create(url, projectDir) ?: RemoteBazelModuleRegistryService.create(url) } - // Add the default Bazel registry as a fallback. - registryServices += RemoteBazelModuleRegistryService.create() - return MultiBazelModuleRegistryService(registryServices) } diff --git a/plugins/package-managers/bazel/src/test/kotlin/MultiBazelModuleRegistryServiceTest.kt b/plugins/package-managers/bazel/src/test/kotlin/MultiBazelModuleRegistryServiceTest.kt index 18974fae7f6cd..ec53d158ce2e3 100644 --- a/plugins/package-managers/bazel/src/test/kotlin/MultiBazelModuleRegistryServiceTest.kt +++ b/plugins/package-managers/bazel/src/test/kotlin/MultiBazelModuleRegistryServiceTest.kt @@ -33,6 +33,7 @@ import io.mockk.unmockkAll import java.io.File import org.ossreviewtoolkit.clients.bazelmoduleregistry.BazelModuleRegistryService +import org.ossreviewtoolkit.clients.bazelmoduleregistry.DEFAULT_URL import org.ossreviewtoolkit.clients.bazelmoduleregistry.LocalBazelModuleRegistryService import org.ossreviewtoolkit.clients.bazelmoduleregistry.ModuleMetadata import org.ossreviewtoolkit.clients.bazelmoduleregistry.ModuleSourceInfo @@ -163,7 +164,7 @@ private class MockRegistryServices( RemoteBazelModuleRegistryService.create(registryUrls[1]) } returns remoteRegistry every { - RemoteBazelModuleRegistryService.create() + RemoteBazelModuleRegistryService.create(DEFAULT_URL) } returns centralRegistry return MockRegistryServices(listOf(localRegistry1, localRegistry2, remoteRegistry, centralRegistry))