-
Notifications
You must be signed in to change notification settings - Fork 345
Generalize GradleDependencyGraphBuilder #3877
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
Merged
sschuberth
merged 6 commits into
oss-review-toolkit:master
from
boschglobal:oheger-bosch/analyzer/generalize_gradle_dependency_graph_builder
Apr 15, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8d096c1
Add DependencyHandler interface
oheger-bosch d3293d5
Add DependencyGraphBuilder class
oheger-bosch 3c06ebc
Add a DependencyHandler implementation for Gradle
oheger-bosch 9c7099d
Test DependencyGraphBuilder instead of GradleDependencyGraphBuilder
oheger-bosch affcfac
Gradle: Use the generic DependencyGraphBuilder
oheger-bosch 836610b
Delete obsolete GradleDependencyGraphBuilder class
oheger-bosch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
analyzer/src/main/kotlin/managers/GradleDependencyHandler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Copyright (C) 2021 Bosch.IO GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
package org.ossreviewtoolkit.analyzer.managers | ||
|
||
import Dependency | ||
|
||
import org.apache.maven.project.ProjectBuildingException | ||
|
||
import org.eclipse.aether.artifact.DefaultArtifact | ||
import org.eclipse.aether.repository.RemoteRepository | ||
|
||
import org.ossreviewtoolkit.analyzer.managers.utils.DependencyHandler | ||
import org.ossreviewtoolkit.analyzer.managers.utils.MavenSupport | ||
import org.ossreviewtoolkit.model.Identifier | ||
import org.ossreviewtoolkit.model.OrtIssue | ||
import org.ossreviewtoolkit.model.Package | ||
import org.ossreviewtoolkit.model.PackageLinkage | ||
import org.ossreviewtoolkit.model.Severity | ||
import org.ossreviewtoolkit.model.createAndLogIssue | ||
import org.ossreviewtoolkit.utils.collectMessagesAsString | ||
import org.ossreviewtoolkit.utils.showStackTrace | ||
|
||
/** | ||
* A specialized [DependencyHandler] implementation for Gradle's dependency model. | ||
*/ | ||
class GradleDependencyHandler( | ||
/** The name of the associated package manager. */ | ||
val managerName: String, | ||
mnonnenmacher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** The helper object to resolve packages via Maven. */ | ||
private val maven: MavenSupport, | ||
|
||
/** A list with repositories to use when resolving packages. */ | ||
private val repositories: List<RemoteRepository> | ||
) : DependencyHandler<Dependency> { | ||
override fun identifierFor(dependency: Dependency): String = | ||
"${dependency.dependencyType()}:${dependency.groupId}:${dependency.artifactId}:${dependency.version}" | ||
|
||
override fun dependenciesFor(dependency: Dependency): Collection<Dependency> = dependency.dependencies | ||
|
||
override fun issuesForDependency(dependency: Dependency): Collection<OrtIssue> = | ||
listOfNotNull( | ||
dependency.error?.let { | ||
createAndLogIssue( | ||
source = managerName, | ||
message = it, | ||
severity = Severity.ERROR | ||
) | ||
}, | ||
|
||
dependency.warning?.let { | ||
createAndLogIssue( | ||
source = managerName, | ||
message = it, | ||
severity = Severity.WARNING | ||
) | ||
} | ||
) | ||
|
||
override fun linkageFor(dependency: Dependency): PackageLinkage = dependency.linkage() | ||
|
||
override fun createPackage(identifier: String, dependency: Dependency, issues: MutableList<OrtIssue>): Package? { | ||
// Only look for a package if there was no error resolving the dependency and it is no project dependency. | ||
if (dependency.error != null || dependency.isProjectDependency()) return null | ||
|
||
return try { | ||
val artifact = DefaultArtifact( | ||
dependency.groupId, dependency.artifactId, dependency.classifier, | ||
dependency.extension, dependency.version | ||
) | ||
|
||
maven.parsePackage(artifact, repositories) | ||
} catch (e: ProjectBuildingException) { | ||
e.showStackTrace() | ||
|
||
issues += createAndLogIssue( | ||
source = managerName, | ||
message = "Could not get package information for dependency '$identifier': " + | ||
e.collectMessagesAsString() | ||
) | ||
|
||
Package.EMPTY.copy( | ||
id = Identifier( | ||
type = "Maven", | ||
namespace = dependency.groupId, | ||
name = dependency.artifactId, | ||
version = dependency.version | ||
) | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Determine the type of this dependency. This manager implementation uses Maven to resolve packages, so | ||
* the type of dependencies to packages is typically _Maven_ unless no pom is available. Only for module | ||
* dependencies, the type of this manager is used. | ||
*/ | ||
private fun Dependency.dependencyType(): String = | ||
if (isProjectDependency()) { | ||
managerName | ||
} else { | ||
pomFile?.let { "Maven" } ?: "Unknown" | ||
} | ||
} | ||
|
||
/** | ||
* Determine the [PackageLinkage] for this [Dependency]. | ||
*/ | ||
private fun Dependency.linkage() = | ||
if (isProjectDependency()) { | ||
PackageLinkage.PROJECT_DYNAMIC | ||
} else { | ||
PackageLinkage.DYNAMIC | ||
} | ||
|
||
/** | ||
* Return a flag whether this dependency references another project in the current build. | ||
*/ | ||
private fun Dependency.isProjectDependency() = localPath != null |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.