Skip to content

Commit

Permalink
refactor(bazel): Create an issue instead of throwing on no registry
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
  • Loading branch information
sschuberth committed Aug 10, 2024
1 parent 742b393 commit 2c18272
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions plugins/package-managers/bazel/src/main/kotlin/Bazel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.ossreviewtoolkit.downloader.VersionControlSystem
import org.ossreviewtoolkit.model.Hash
import org.ossreviewtoolkit.model.HashAlgorithm
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Issue
import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.model.PackageLinkage
import org.ossreviewtoolkit.model.PackageReference
Expand All @@ -49,6 +50,7 @@ import org.ossreviewtoolkit.model.VcsType
import org.ossreviewtoolkit.model.collectDependencies
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.model.createAndLogIssue
import org.ossreviewtoolkit.model.orEmpty
import org.ossreviewtoolkit.utils.common.CommandLineTool
import org.ossreviewtoolkit.utils.common.ProcessCapture
Expand Down Expand Up @@ -109,9 +111,15 @@ class Bazel(
// If no lockfile is present, getDependencyGraph() runs "bazel mod graph", which creates a "MODULE.bazel.lock"
// file as a side effect. That file contains the URL of the Bazel module registry that was used for dependency
// resolution.
val issues = mutableListOf<Issue>()
val registry = determineRegistry(parseLockfile(lockfile), projectDir)

val packages = getPackages(scopes, registry)
val packages = if (registry != null) {
getPackages(scopes, registry)
} else {
issues += createAndLogIssue(managerName, "Bazel registry URL cannot be determined from the lockfile.")
emptySet()
}

return listOf(
ProjectAnalyzerResult(
Expand All @@ -129,18 +137,19 @@ class Bazel(
homepageUrl = "",
scopeDependencies = scopes
),
packages = packages
packages = packages,
issues = issues
)
)
}

/**
* This function determines the Bazel module registry to use based on the given [lockfile]: If this is a lockfile
* generated by Bazel version >= 7.2.0, a [CompositeBazelModuleRegistryService] based on the "registryFileHashes"
* will be returned. Else, either a [LocalBazelModuleRegistryService] or a [RemoteBazelModuleRegistryService] based
* on the "cmdRegistries" will be returned.
* Determine the Bazel module registry to use based on the Bazel version that generated the given [lockfile]. For
* Bazel version >= 7.2.0, a [CompositeBazelModuleRegistryService] based on the "registryFileHashes" is returned.
* For Bazel version < 7.2.0, either a [LocalBazelModuleRegistryService] or a [RemoteBazelModuleRegistryService]
* based on the "cmdRegistries" is returned. Return null if the registry cannot be determined.
*/
private fun determineRegistry(lockfile: Lockfile, projectDir: File): BazelModuleRegistryService {
private fun determineRegistry(lockfile: Lockfile, projectDir: File): BazelModuleRegistryService? {
// Bazel version < 7.2.0.
if (lockfile.flags != null) {
return MultiBazelModuleRegistryService.create(lockfile.registryUrls(), projectDir)
Expand All @@ -151,9 +160,7 @@ class Bazel(
return CompositeBazelModuleRegistryService.create(lockfile.registryFileHashes.keys, projectDir)
}

val msg = "Bazel registry URL cannot be determined from the lockfile."
logger.error(msg)
error(msg)
return null
}

private fun getPackages(scopes: Set<Scope>, registry: BazelModuleRegistryService): Set<Package> {
Expand Down

0 comments on commit 2c18272

Please sign in to comment.