Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(analyzer): Add a function to create dependency graph projects #9426

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions analyzer/src/main/kotlin/PackageManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@
import org.ossreviewtoolkit.model.config.PackageManagerConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
import org.ossreviewtoolkit.model.createAndLogIssue
import org.ossreviewtoolkit.model.utils.DependencyGraphBuilder
import org.ossreviewtoolkit.utils.common.Options
import org.ossreviewtoolkit.utils.common.VCS_DIRECTORIES
import org.ossreviewtoolkit.utils.common.collapseWhitespace
import org.ossreviewtoolkit.utils.common.collectMessages
import org.ossreviewtoolkit.utils.common.isSymbolicLink
import org.ossreviewtoolkit.utils.ort.DeclaredLicenseProcessor
import org.ossreviewtoolkit.utils.ort.ORT_CONFIG_FILENAME
import org.ossreviewtoolkit.utils.ort.normalizeVcsUrl
import org.ossreviewtoolkit.utils.ort.showStackTrace
import org.ossreviewtoolkit.utils.spdx.SpdxOperator

typealias ManagedProjectFiles = Map<PackageManagerFactory, List<File>>
typealias ProjectResults = Map<File, List<ProjectAnalyzerResult>>
Expand Down Expand Up @@ -259,6 +262,36 @@
*/
protected open fun afterResolution(definitionFiles: List<File>) {}

/**
* Conveniently create a [Project] for implementations that use the [DependencyGraphBuilder].
*/
fun DependencyGraphBuilder<*>.createProject(
namespace: String,
name: String,
version: String,
definitionFile: File,
projectDir: File = definitionFile.parentFile,
authors: Set<String> = emptySet(),
homepageUrl: String = "",
declaredLicenses: Set<String> = emptySet(),
licenseOperator: SpdxOperator = SpdxOperator.AND,
vcsFromProject: VcsInfo = VcsInfo.EMPTY,
vararg vcsFallbackUrls: String
): Project {
Comment on lines +268 to +280

Check warning

Code scanning / detekt

The more parameters a function has the more complex it is. Long parameter lists are often used to control complex algorithms and violate the Single Responsibility Principle. Prefer functions with short parameter lists. Warning

The function createProject(namespace: String, name: String, version: String, definitionFile: File, projectDir: File, authors: Set, homepageUrl: String, declaredLicenses: Set, licenseOperator: SpdxOperator, vcsFromProject: VcsInfo, vcsFallbackUrls: String) has too many parameters. The current threshold is set to 8.
val id = Identifier(projectType, namespace, name, version)
return Project(
id = id,
definitionFilePath = VersionControlSystem.getPathInfo(definitionFile).path,
authors = authors,
declaredLicenses = declaredLicenses,
declaredLicensesProcessed = DeclaredLicenseProcessor.process(declaredLicenses, operator = licenseOperator),
vcs = vcsFromProject,
vcsProcessed = processProjectVcs(projectDir, vcsFromProject, *vcsFallbackUrls),
homepageUrl = homepageUrl,
scopeNames = scopesFor(id)
)
}

/**
* Generate the final result to be returned by this package manager. This function is called at the very end of the
* execution of this package manager (after [afterResolution]) with the [projectResults] created for the single
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,9 @@ import org.gradle.tooling.model.build.BuildEnvironment
import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory
import org.ossreviewtoolkit.analyzer.PackageManager
import org.ossreviewtoolkit.analyzer.PackageManagerResult
import org.ossreviewtoolkit.downloader.VersionControlSystem
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Issue
import org.ossreviewtoolkit.model.Project
import org.ossreviewtoolkit.model.ProjectAnalyzerResult
import org.ossreviewtoolkit.model.Severity
import org.ossreviewtoolkit.model.VcsInfo
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
import org.ossreviewtoolkit.model.config.PackageManagerConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
Expand Down Expand Up @@ -243,30 +239,14 @@ class GradleInspector(
createAndLogIssue(source = managerName, message = it, severity = Severity.WARNING)
}

val projectId = Identifier(
type = "Gradle",
namespace = dependencyTreeModel.group,
name = dependencyTreeModel.name,
version = dependencyTreeModel.version
)
val project = with(dependencyTreeModel) { graphBuilder.createProject(group, name, version, definitionFile) }

dependencyTreeModel.configurations.filterNot {
excludes.isScopeExcluded(it.name)
}.forEach { configuration ->
graphBuilder.addDependencies(projectId, configuration.name, configuration.dependencies)
graphBuilder.addDependencies(project.id, configuration.name, configuration.dependencies)
}

val project = Project(
id = projectId,
definitionFilePath = VersionControlSystem.getPathInfo(definitionFile).path,
authors = emptySet(),
declaredLicenses = emptySet(),
vcs = VcsInfo.EMPTY,
vcsProcessed = processProjectVcs(definitionFile.parentFile),
homepageUrl = "",
scopeNames = graphBuilder.scopesFor(projectId)
)

val result = ProjectAnalyzerResult(project, emptySet(), issues)
return listOf(result)
}
Expand Down
26 changes: 4 additions & 22 deletions plugins/package-managers/gradle/src/main/kotlin/Gradle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,9 @@ import org.gradle.tooling.model.build.BuildEnvironment
import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory
import org.ossreviewtoolkit.analyzer.PackageManager
import org.ossreviewtoolkit.analyzer.PackageManagerResult
import org.ossreviewtoolkit.downloader.VersionControlSystem
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Issue
import org.ossreviewtoolkit.model.Project
import org.ossreviewtoolkit.model.ProjectAnalyzerResult
import org.ossreviewtoolkit.model.Severity
import org.ossreviewtoolkit.model.VcsInfo
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
import org.ossreviewtoolkit.model.config.PackageManagerConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
Expand Down Expand Up @@ -292,30 +288,16 @@ class Gradle(
"The Gradle project '$projectName' uses the following Maven repositories: $repositories"
}

val projectId = Identifier(
type = managerName,
namespace = dependencyTreeModel.group,
name = dependencyTreeModel.name,
version = dependencyTreeModel.version
)
val project = with(dependencyTreeModel) {
graphBuilder.createProject(group, name, version, definitionFile)
}

dependencyTreeModel.configurations.filterNot {
excludes.isScopeExcluded(it.name)
}.forEach { configuration ->
graphBuilder.addDependencies(projectId, configuration.name, configuration.dependencies)
graphBuilder.addDependencies(project.id, configuration.name, configuration.dependencies)
}

val project = Project(
id = projectId,
definitionFilePath = VersionControlSystem.getPathInfo(definitionFile).path,
authors = emptySet(),
declaredLicenses = emptySet(),
vcs = VcsInfo.EMPTY,
vcsProcessed = processProjectVcs(definitionFile.parentFile),
homepageUrl = "",
scopeNames = graphBuilder.scopesFor(projectId)
)

dependencyTreeModel.errors.mapTo(issues) {
createAndLogIssue(source = managerName, message = it, severity = Severity.ERROR)
}
Expand Down
63 changes: 30 additions & 33 deletions plugins/package-managers/maven/src/main/kotlin/Maven.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory
import org.ossreviewtoolkit.analyzer.PackageManager
import org.ossreviewtoolkit.analyzer.PackageManagerResult
import org.ossreviewtoolkit.downloader.VersionControlSystem
import org.ossreviewtoolkit.model.DependencyGraph
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Project
import org.ossreviewtoolkit.model.ProjectAnalyzerResult
import org.ossreviewtoolkit.model.Severity
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
Expand All @@ -48,8 +45,9 @@
import org.ossreviewtoolkit.plugins.packagemanagers.maven.utils.parseAuthors
import org.ossreviewtoolkit.plugins.packagemanagers.maven.utils.parseLicenses
import org.ossreviewtoolkit.plugins.packagemanagers.maven.utils.parseVcsInfo
import org.ossreviewtoolkit.plugins.packagemanagers.maven.utils.processDeclaredLicenses

Check warning on line 48 in plugins/package-managers/maven/src/main/kotlin/Maven.kt

View workflow job for this annotation

GitHub Actions / qodana-scan

Unused import directive

Unused import directive
import org.ossreviewtoolkit.utils.common.searchUpwardsForSubdirectory
import org.ossreviewtoolkit.utils.spdx.SpdxOperator

/**
* The [Maven](https://maven.apache.org/) package manager for Java.
Expand Down Expand Up @@ -112,25 +110,6 @@

override fun resolveDependencies(definitionFile: File, labels: Map<String, String>): List<ProjectAnalyzerResult> {
val workingDir = definitionFile.parentFile
val projectBuildingResult = mvn.buildMavenProject(definitionFile)
val mavenProject = projectBuildingResult.project
val projectId = Identifier(
type = managerName,
namespace = mavenProject.groupId,
name = mavenProject.artifactId,
version = mavenProject.version
)

projectBuildingResult.dependencies.filterNot {
excludes.isScopeExcluded(it.dependency.scope)
}.forEach { node ->
graphBuilder.addDependency(DependencyGraph.qualifyScope(projectId, node.dependency.scope), node)
}

val declaredLicenses = parseLicenses(mavenProject)
val declaredLicensesProcessed = processDeclaredLicenses(declaredLicenses)

val vcsFromPackage = parseVcsInfo(mavenProject)

// If running in SBT mode expect that POM files were generated in a "target" subdirectory and that the correct
// project directory is the parent directory of this.
Expand All @@ -140,21 +119,39 @@
workingDir
}

val projectBuildingResult = mvn.buildMavenProject(definitionFile)
val mavenProject = projectBuildingResult.project

val declaredLicenses = parseLicenses(mavenProject)

val vcsFromProject = parseVcsInfo(mavenProject)

val browsableScmUrl = getOriginalScm(mavenProject)?.url
val homepageUrl = mavenProject.url
val vcsFallbackUrls = listOfNotNull(browsableScmUrl, homepageUrl).toTypedArray()

val project = Project(
id = projectId,
definitionFilePath = VersionControlSystem.getPathInfo(definitionFile).path,
authors = parseAuthors(mavenProject),
declaredLicenses = declaredLicenses,
declaredLicensesProcessed = declaredLicensesProcessed,
vcs = vcsFromPackage,
vcsProcessed = processProjectVcs(projectDir, vcsFromPackage, *vcsFallbackUrls),
homepageUrl = homepageUrl.orEmpty(),
scopeNames = graphBuilder.scopesFor(projectId)
)
val project = with(mavenProject) {
graphBuilder.createProject(
namespace = groupId,
name = artifactId,
version = version,
definitionFile = definitionFile,
authors = parseAuthors(mavenProject),
homepageUrl = homepageUrl.orEmpty(),
vcsFallbackUrls = vcsFallbackUrls,
declaredLicenses = declaredLicenses,
// See http://maven.apache.org/ref/3.6.3/maven-model/maven.html#project saying: "If multiple licenses
// are listed, it is assumed that the user can select any of them, not that they must accept all."
licenseOperator = SpdxOperator.OR,
vcsFromProject = vcsFromProject
)
}

projectBuildingResult.dependencies.filterNot {
excludes.isScopeExcluded(it.dependency.scope)
}.forEach { node ->
graphBuilder.addDependency(DependencyGraph.qualifyScope(project.id, node.dependency.scope), node)
}

val issues = graphBuilder.packages().mapNotNull { pkg ->
if (pkg.description == "POM was created by Sonatype Nexus") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import org.ossreviewtoolkit.model.utils.parseRepoManifestPath
import org.ossreviewtoolkit.utils.common.splitOnWhitespace
import org.ossreviewtoolkit.utils.common.withoutPrefix
import org.ossreviewtoolkit.utils.ort.DeclaredLicenseProcessor

Check warning on line 35 in plugins/package-managers/maven/src/main/kotlin/utils/MavenParsers.kt

View workflow job for this annotation

GitHub Actions / qodana-scan

Unused import directive

Unused import directive
import org.ossreviewtoolkit.utils.ort.ProcessedDeclaredLicense

Check warning on line 36 in plugins/package-managers/maven/src/main/kotlin/utils/MavenParsers.kt

View workflow job for this annotation

GitHub Actions / qodana-scan

Unused import directive

Unused import directive
import org.ossreviewtoolkit.utils.spdx.SpdxOperator

Check warning on line 37 in plugins/package-managers/maven/src/main/kotlin/utils/MavenParsers.kt

View workflow job for this annotation

GitHub Actions / qodana-scan

Unused import directive

Unused import directive

// See http://maven.apache.org/pom.html#SCM.
private val SCM_REGEX = Regex("scm:(?<type>[^:@]+):(?<url>.+)")
Expand Down Expand Up @@ -199,8 +199,3 @@
}
}
}

internal fun processDeclaredLicenses(licenses: Set<String>): ProcessedDeclaredLicense =
// See http://maven.apache.org/ref/3.6.3/maven-model/maven.html#project which says: "If multiple licenses
// are listed, it is assumed that the user can select any of them, not that they must accept all."
DeclaredLicenseProcessor.process(licenses, operator = SpdxOperator.OR)
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ import org.ossreviewtoolkit.utils.common.DiskCache
import org.ossreviewtoolkit.utils.common.collectMessages
import org.ossreviewtoolkit.utils.common.gibibytes
import org.ossreviewtoolkit.utils.common.searchUpwardsForSubdirectory
import org.ossreviewtoolkit.utils.ort.DeclaredLicenseProcessor
import org.ossreviewtoolkit.utils.ort.OrtAuthenticator
import org.ossreviewtoolkit.utils.ort.OrtProxySelector
import org.ossreviewtoolkit.utils.ort.downloadText
import org.ossreviewtoolkit.utils.ort.okHttpClient
import org.ossreviewtoolkit.utils.ort.ortDataDirectory
import org.ossreviewtoolkit.utils.ort.showStackTrace
import org.ossreviewtoolkit.utils.spdx.SpdxOperator

fun Artifact.identifier() = "$groupId:$artifactId:$version"

Expand Down Expand Up @@ -490,7 +492,10 @@ class MavenSupport(private val workspaceReader: WorkspaceReader) {
}

val declaredLicenses = parseLicenses(mavenProject)
val declaredLicensesProcessed = processDeclaredLicenses(declaredLicenses)

// See http://maven.apache.org/ref/3.6.3/maven-model/maven.html#project saying: "If multiple licenses
// are listed, it is assumed that the user can select any of them, not that they must accept all."
val declaredLicensesProcessed = DeclaredLicenseProcessor.process(declaredLicenses, operator = SpdxOperator.OR)

val binaryRemoteArtifact = localProject?.let {
RemoteArtifact.EMPTY
Expand Down
Loading