Skip to content

Commit

Permalink
fix(package-managers): Use projectType as the ID type for projects
Browse files Browse the repository at this point in the history
Do not use the `managerName` to create IDs for projects, even if in most
cases the strings happen to match the respective `projectType`.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Jan 2, 2025
1 parent 39c2b73 commit dd9079f
Show file tree
Hide file tree
Showing 32 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion plugins/package-managers/bazel/src/main/kotlin/Bazel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class Bazel(
ProjectAnalyzerResult(
project = Project(
id = Identifier(
type = managerName,
type = projectType,
namespace = "",
name = moduleMetadata.module?.name ?: VersionControlSystem.getPathInfo(definitionFile).path,
version = moduleMetadata.module?.version.orEmpty()
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/bower/src/main/kotlin/Bower.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Bower(
graphBuilder.addDependencies(projectPackageInfo.toIdentifier(), scopeName, dependencies)
}

val project = projectPackageInfo.toProject(definitionFile, SCOPE_NAMES)
val project = projectPackageInfo.toProject(definitionFile, projectType, SCOPE_NAMES)
return listOf(ProjectAnalyzerResult(project, emptySet()))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ private fun PackageInfo.toPackage() =
vcs = toVcsInfo()
)

internal fun PackageInfo.toProject(definitionFile: File, scopeNames: Set<String>) =
internal fun PackageInfo.toProject(definitionFile: File, projectType: String, scopeNames: Set<String>) =
with(toPackage()) {
Project(
id = id,
id = id.copy(type = projectType),
definitionFilePath = VersionControlSystem.getPathInfo(definitionFile).path,
authors = authors,
declaredLicenses = declaredLicenses,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class Bundler(
val gemsInfo = resolveGemsInfo(workingDir)

return with(parseProject(definitionFile, gemsInfo)) {
val projectId = Identifier(managerName, "", name, version)
val projectId = Identifier(projectType, "", name, version)
val groupedDeps = getDependencyGroups(workingDir)

groupedDeps.forEach { (groupName, dependencyList) ->
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/cargo/src/main/kotlin/Cargo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class Cargo(

val hashes = readHashes(resolveLockfile(metadata))
val projectPkg = packageById.getValue(projectId).let { cargoPkg ->
cargoPkg.toPackage(hashes).let { it.copy(id = it.id.copy(type = managerName)) }
cargoPkg.toPackage(hashes).let { it.copy(id = it.id.copy(type = projectType)) }
}

val project = Project(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Carthage(
ProjectAnalyzerResult(
project = Project(
id = Identifier(
type = managerName,
type = projectType,
namespace = projectInfo.namespace.orEmpty(),
name = projectInfo.projectName.orEmpty(),
version = projectInfo.revision.orEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class CocoaPods(
packages = packages,
project = Project(
id = Identifier(
type = managerName,
type = projectType,
namespace = "",
name = getFallbackProjectName(analysisRoot, definitionFile),
version = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class Composer(

return Project(
id = Identifier(
type = managerName,
type = projectType,
namespace = namespace,
name = name,
version = pkgInfo.version.orEmpty()
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/conan/src/main/kotlin/Conan.kt
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ class Conan(

return Package(
id = Identifier(
type = managerName,
type = projectType,
namespace = "",
name = inspectPyFile("name") ?: pkgInfo.reference.orEmpty(),
version = inspectPyFile("version").orEmpty()
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/go/src/main/kotlin/GoMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class GoMod(
}

Identifier(
type = managerName,
type = projectType,
namespace = "",
name = getModuleInfos(projectDir).single().path,
version = processProjectVcs(projectDir).revision
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class GradleInspector(
}

val projectId = Identifier(
type = "Gradle",
type = projectType,
namespace = dependencyTreeModel.group,
name = dependencyTreeModel.name,
version = dependencyTreeModel.version
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/gradle/src/main/kotlin/Gradle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ class Gradle(
}

val projectId = Identifier(
type = managerName,
type = projectType,
namespace = dependencyTreeModel.group,
name = dependencyTreeModel.name,
version = dependencyTreeModel.version
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/maven/src/main/kotlin/Maven.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Maven(
val projectBuildingResult = mvn.buildMavenProject(definitionFile)
val mavenProject = projectBuildingResult.project
val projectId = Identifier(
type = managerName,
type = projectType,
namespace = mavenProject.groupId,
name = mavenProject.artifactId,
version = mavenProject.version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class MavenDependencyHandler(

override fun identifierFor(dependency: DependencyNode): Identifier =
Identifier(
type = if (isLocalProject(dependency.identifier())) managerName else "Maven",
type = if (isLocalProject(dependency.identifier())) projectType else "Maven",
namespace = dependency.artifact.groupId,
name = dependency.artifact.artifactId,
version = dependency.artifact.version
Expand Down
4 changes: 2 additions & 2 deletions plugins/package-managers/node/src/main/kotlin/NpmSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ internal fun parsePackage(
return module
}

internal fun parseProject(packageJsonFile: File, analysisRoot: File, managerName: String): Project {
internal fun parseProject(packageJsonFile: File, analysisRoot: File, projectType: String): Project {
logger.debug { "Parsing project info from '$packageJsonFile'." }

val packageJson = parsePackageJson(packageJsonFile)
Expand Down Expand Up @@ -258,7 +258,7 @@ internal fun parseProject(packageJsonFile: File, analysisRoot: File, managerName

return Project(
id = Identifier(
type = managerName,
type = projectType,
namespace = namespace,
name = projectName,
version = version
Expand Down
4 changes: 2 additions & 2 deletions plugins/package-managers/node/src/main/kotlin/npm/Npm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Npm(

if (issues.any { it.severity == Severity.ERROR }) {
val project = runCatching {
parseProject(definitionFile, analysisRoot, managerName)
parseProject(definitionFile, analysisRoot, projectType)
}.getOrElse {
logger.error { "Failed to parse project information: ${it.collectMessages()}" }
Project.EMPTY
Expand All @@ -111,7 +111,7 @@ class Npm(
return listOf(ProjectAnalyzerResult(project, emptySet(), issues))
}

val project = parseProject(definitionFile, analysisRoot, managerName)
val project = parseProject(definitionFile, analysisRoot, projectType)

Check warning on line 114 in plugins/package-managers/node/src/main/kotlin/npm/Npm.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/node/src/main/kotlin/npm/Npm.kt#L114

Added line #L114 was not covered by tests
val projectModuleInfo = listModules(workingDir, issues).undoDeduplication()

val scopeNames = Scope.entries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal class NpmDependencyHandler(private val npm: Npm) : DependencyHandler<Mo
private val packageJsonCache = mutableMapOf<File, PackageJson>()

override fun identifierFor(dependency: ModuleInfo): Identifier {
val type = npm.managerName.takeIf { dependency.isProject } ?: "NPM"
val type = if (dependency.isProject) npm.projectType else "NPM"
val (namespace, name) = splitNamespaceAndName(dependency.name.orEmpty())
val version = if (dependency.isProject) {
readPackageJson(dependency.packageJsonFile).version.orEmpty()
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/node/src/main/kotlin/pnpm/Pnpm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Pnpm(
val moduleInfosForScope = scopes.associateWith { scope -> listModules(workingDir, scope) }

return workspaceModuleDirs.map { projectDir ->
val project = parseProject(projectDir.resolve("package.json"), analysisRoot, managerName)
val project = parseProject(projectDir.resolve("package.json"), analysisRoot, projectType)

Check warning on line 90 in plugins/package-managers/node/src/main/kotlin/pnpm/Pnpm.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/node/src/main/kotlin/pnpm/Pnpm.kt#L90

Added line #L90 was not covered by tests

val scopeNames = scopes.mapTo(mutableSetOf()) { scope ->
val scopeName = scope.descriptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal class PnpmDependencyHandler(private val pnpm: Pnpm) : DependencyHandler
}

override fun identifierFor(dependency: Dependency): Identifier {
val type = pnpm.managerName.takeIf { dependency.isProject() } ?: "NPM"
val type = if (dependency.isProject()) pnpm.projectType else "NPM"
val namespace = dependency.from.substringBeforeLast("/", "")
val name = dependency.from.substringAfterLast("/")
val version = if (dependency.isProject()) {
Expand Down
4 changes: 2 additions & 2 deletions plugins/package-managers/node/src/main/kotlin/yarn/Yarn.kt
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ open class Yarn(
val issues = mutableListOf<Issue>()

val project = runCatching {
parseProject(projectDir.resolve("package.json"), analysisRoot, managerName)
parseProject(projectDir.resolve("package.json"), analysisRoot, projectType)

Check warning on line 183 in plugins/package-managers/node/src/main/kotlin/yarn/Yarn.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/node/src/main/kotlin/yarn/Yarn.kt#L183

Added line #L183 was not covered by tests
}.getOrElse {
issues += createAndLogIssue(
source = managerName,
Expand Down Expand Up @@ -231,7 +231,7 @@ open class Yarn(
): NpmModuleInfo? {
val moduleInfo = parsePackageJson(moduleDir, scopes)
val dependencies = mutableSetOf<NpmModuleInfo>()
val packageType = managerName.takeIf { moduleDir.realFile() in projectDirs } ?: "NPM"
val packageType = if (moduleDir.realFile() in projectDirs) projectType else "NPM"

val moduleId = splitNamespaceAndName(moduleInfo.name).let { (namespace, name) ->
Identifier(packageType, namespace, name, moduleInfo.version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ class Yarn2(

val id = Identifier("Yarn2", namespace, name, version)
allProjects += id to Project(
id = id.copy(type = managerName),
id = id.copy(type = projectType),

Check warning on line 413 in plugins/package-managers/node/src/main/kotlin/yarn2/Yarn2.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/node/src/main/kotlin/yarn2/Yarn2.kt#L413

Added line #L413 was not covered by tests
definitionFilePath = VersionControlSystem.getPathInfo(projectFile).path,
declaredLicenses = declaredLicenses,
vcs = additionalData.vcsFromPackage,
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/nuget/src/main/kotlin/NuGet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class NuGet(
override fun resolveDependencies(definitionFile: File, labels: Map<String, String>): List<ProjectAnalyzerResult> {
val result = NuGetInspector.inspect(definitionFile, nugetConfig)

val project = result.toOrtProject(managerName, analysisRoot, definitionFile)
val project = result.toOrtProject(projectType, analysisRoot, definitionFile)
val packages = result.dependencies.toOrtPackages()

return listOf(ProjectAnalyzerResult(project, packages, collectTopLevelIssues(result)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ private fun List<NuGetInspector.Party>.toAuthors(): Set<String> =
}

internal fun NuGetInspector.Result.toOrtProject(
managerName: String,
projectType: String,
analysisRoot: File,
definitionFile: File
): Project {
val id = Identifier(
type = managerName,
type = projectType,

Check warning on line 59 in plugins/package-managers/nuget/src/main/kotlin/utils/NuGetInspectorExtensions.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/nuget/src/main/kotlin/utils/NuGetInspectorExtensions.kt#L59

Added line #L59 was not covered by tests
namespace = "",
name = definitionFile.relativeTo(analysisRoot).invariantSeparatorsPath,
version = ""
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/pub/src/main/kotlin/Pub.kt
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ class Pub(

return Project(
id = Identifier(
type = managerName,
type = projectType,
namespace = "",
name = pubspec.name,
version = pubspec.version.orEmpty()
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/python/src/main/kotlin/Pip.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Pip(
override fun resolveDependencies(definitionFile: File, labels: Map<String, String>): List<ProjectAnalyzerResult> {
val result = runPythonInspector(definitionFile) { detectPythonVersion(definitionFile.parentFile) }

val project = result.toOrtProject(managerName, analysisRoot, definitionFile)
val project = result.toOrtProject(projectType, analysisRoot, definitionFile)
val packages = result.packages.toOrtPackages()

return listOf(ProjectAnalyzerResult(project, packages))
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/python/src/main/kotlin/Poetry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Poetry(

val project = Project.EMPTY.copy(
id = Identifier(
type = managerName,
type = projectType,
namespace = "",
name = definitionFile.relativeTo(analysisRoot).path,
version = VersionControlSystem.getCloneInfo(definitionFile.parentFile).revision
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ import org.ossreviewtoolkit.model.VcsType
private const val TYPE = "PyPI"

internal fun PythonInspector.Result.toOrtProject(
managerName: String,
projectType: String,
analysisRoot: File,
definitionFile: File
): Project {
val id = resolveIdentifier(managerName, analysisRoot, definitionFile)
val id = resolveIdentifier(projectType, analysisRoot, definitionFile)

Check warning on line 43 in plugins/package-managers/python/src/main/kotlin/utils/PythonInspectorExtensions.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/python/src/main/kotlin/utils/PythonInspectorExtensions.kt#L43

Added line #L43 was not covered by tests

val setupProject = projects.find { it.path.endsWith("/setup.py") }
val projectData = setupProject?.packageData?.singleOrNull()
Expand All @@ -61,7 +61,7 @@ internal fun PythonInspector.Result.toOrtProject(
}

private fun PythonInspector.Result.resolveIdentifier(
managerName: String,
projectType: String,
analysisRoot: File,
definitionFile: File
): Identifier {
Expand Down Expand Up @@ -103,7 +103,7 @@ private fun PythonInspector.Result.resolveIdentifier(
val projectVersion = setupVersion.ifEmpty { requirementsVersion }

return Identifier(
type = managerName,
type = projectType,

Check warning on line 106 in plugins/package-managers/python/src/main/kotlin/utils/PythonInspectorExtensions.kt

View check run for this annotation

Codecov / codecov/patch

plugins/package-managers/python/src/main/kotlin/utils/PythonInspectorExtensions.kt#L106

Added line #L106 was not covered by tests
namespace = "",
name = projectName,
version = projectVersion
Expand Down
12 changes: 6 additions & 6 deletions plugins/package-managers/sbt/src/main/kotlin/Sbt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,26 @@ class Sbt(
// definition file paths.
val workingDir = getCommonParentFile(definitionFiles)

logger.info { "Determined '$workingDir' as the $managerName project root directory." }
logger.info { "Determined '$workingDir' as the $projectType project root directory." }

val sbtVersion = options[OPTION_SBT_VERSION]
val sbtVersions = getBuildSbtVersions(workingDir)
when {
sbtVersion != null -> logger.info { "Using configured custom $managerName version $sbtVersion." }
sbtVersion != null -> logger.info { "Using configured custom $projectType version $sbtVersion." }

sbtVersions.isEmpty() ->
logger.info { "The build does not configure any $managerName version to be used." }
logger.info { "The build does not configure any $projectType version to be used." }

sbtVersions.size == 1 ->
logger.info { "The build configures $managerName version ${sbtVersions.first()} to be used." }
logger.info { "The build configures $projectType version ${sbtVersions.first()} to be used." }

else ->
logger.warn { "The build configures multiple different $managerName versions to be used: $sbtVersions" }
logger.warn { "The build configures multiple different $projectType versions to be used: $sbtVersions" }
}

val lowestSbtVersion = sbtVersion?.let { Semver(it) } ?: sbtVersions.firstOrNull() ?: getGlobalSbtVersion()
require(lowestSbtVersion?.isLowerThan(Semver(LOWEST_SUPPORTED_SBT_VERSION)) != true) {
"Build $managerName version $lowestSbtVersion is lower than version $LOWEST_SUPPORTED_SBT_VERSION."
"Build $projectType version $lowestSbtVersion is lower than version $LOWEST_SUPPORTED_SBT_VERSION."
}

// TODO: Consider auto-detecting the Java version based on the SBT version. See:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class SpdxDocumentFile(
*/
private fun SpdxPackage.toIdentifier() =
Identifier(
type = managerName,
type = projectType,
namespace = listOfNotNull(supplier, originator).firstOrNull()
?.withoutPrefix(SpdxConstants.ORGANIZATION).orEmpty().sanitize(),
name = name.sanitize(),
Expand Down
2 changes: 1 addition & 1 deletion plugins/package-managers/stack/src/main/kotlin/Stack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Stack(
else -> throw IOException("Multiple *.cabal files found in '$cabalFiles'.")
}

val projectPackage = parseCabalFile(cabalFile.readText(), managerName)
val projectPackage = parseCabalFile(cabalFile.readText(), projectType)

return Project(
id = projectPackage.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class SwiftPm(
val vcsInfo = VersionControlSystem.forDirectory(definitionFile.parentFile)?.getInfo().orEmpty()

val projectIdentifier = Identifier(
type = managerName,
type = projectType,
version = vcsInfo.revision,
namespace = "",
name = getFallbackProjectName(analysisRoot, definitionFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Unmanaged(
}

Identifier(
type = managerName,
type = projectType,
namespace = "",
name = definitionFile.name,
version = ""
Expand All @@ -89,7 +89,7 @@ class Unmanaged(
val manifestPath = vcsInfo.url.parseRepoManifestPath()

Identifier(
type = managerName,
type = projectType,
namespace = manifestPath?.substringBeforeLast('/').orEmpty(),
name = manifestPath?.substringAfterLast('/')?.removeSuffix(".xml")
?: vcsInfo.url.split('/').last().removeSuffix(".git"),
Expand All @@ -100,7 +100,7 @@ class Unmanaged(
else -> {
// For all non-GitRepo VCSes derive the name from the VCS URL.
Identifier(
type = managerName,
type = projectType,
namespace = "",
name = vcsInfo.url.split('/').last().removeSuffix(".git"),
version = vcsInfo.revision
Expand Down

0 comments on commit dd9079f

Please sign in to comment.