Skip to content

Commit

Permalink
Better track project dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
bigdaz committed Nov 6, 2023
1 parent c92313a commit 442319f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gradle.dependencygraph.extractor

import org.gradle.api.GradleException
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
import org.gradle.api.artifacts.result.ResolvedComponentResult
import org.gradle.api.artifacts.result.ResolvedDependencyResult
import org.gradle.api.internal.artifacts.DefaultProjectComponentIdentifier
Expand Down Expand Up @@ -160,17 +161,17 @@ abstract class DependencyExtractor :
val rootSource = DependencySource(rootId, rootPath)
val resolvedConfiguration = ResolvedConfiguration(rootSource, details.configurationName)

for (directDependency in getResolvedDependencies(rootComponent)) {
for (dependencyComponent in getResolvedDependencies(rootComponent)) {
val directDep = createComponentNode(
componentId(directDependency),
componentId(dependencyComponent),
rootSource,
true,
directDependency,
dependencyComponent,
repositoryLookup
)
resolvedConfiguration.addDependency(directDep)

walkComponentDependencies(directDependency, directDep.source, repositoryLookup, resolvedConfiguration)
walkComponentDependencies(dependencyComponent, directDep.source, repositoryLookup, resolvedConfiguration)
}

resolvedConfigurations.add(resolvedConfiguration)
Expand All @@ -185,11 +186,11 @@ abstract class DependencyExtractor :
val componentSource = getSource(component, parentSource)
val direct = componentSource != parentSource

val dependencyComponents = getResolvedDependencies(component)
for (dependencyComponent in dependencyComponents) {
for (dependencyComponent in getResolvedDependencies(component)) {
val dependencyId = componentId(dependencyComponent)
if (!resolvedConfiguration.hasDependency(dependencyId)) {
val dependencyNode = createComponentNode(dependencyId, componentSource, direct, dependencyComponent, repositoryLookup)
val dependencyNode =
createComponentNode(dependencyId, componentSource, direct, dependencyComponent, repositoryLookup)
resolvedConfiguration.addDependency(dependencyNode)

walkComponentDependencies(dependencyComponent, componentSource, repositoryLookup, resolvedConfiguration)
Expand All @@ -209,13 +210,15 @@ abstract class DependencyExtractor :
return component.dependencies.filterIsInstance<ResolvedDependencyResult>().map { it.selected }.filter { it != component }
}

private fun createComponentNode(componentId: String, source: DependencySource, direct: Boolean, component: ResolvedComponentResult, repositoryLookup: RepositoryUrlLookup): ResolvedDependency {
private fun createComponentNode(componentId: String, source: DependencySource, isDirectDependency: Boolean, component: ResolvedComponentResult, repositoryLookup: RepositoryUrlLookup): ResolvedDependency {
val componentDependencies = component.dependencies.filterIsInstance<ResolvedDependencyResult>().map { componentId(it.selected) }
val repositoryUrl = repositoryLookup.doLookup(component)
val isProjectDependency = component.id is ProjectComponentIdentifier
return ResolvedDependency(
componentId,
source,
direct,
isDirectDependency,
isProjectDependency,
coordinates(component),
repositoryUrl,
componentDependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ private const val DEFAULT_MAVEN_REPOSITORY_URL = "https://repo.maven.apache.org/
data class ResolvedDependency(
val id: String,
val source: DependencySource,
val direct: Boolean,
val isDirect: Boolean,
val isProject: Boolean,
val coordinates: DependencyCoordinates,
val repositoryUrl: String?,
val dependencies: List<String>
Expand All @@ -17,7 +18,7 @@ data class ResolvedDependency(
PackageURLBuilder
.aPackageURL()
.withType("maven")
.withNamespace(coordinates.group.ifEmpty { coordinates.module }) // TODO: This is a sign of broken mapping from component -> PURL
.withNamespace(coordinates.group.ifEmpty { coordinates.module })
.withName(coordinates.module)
.withVersion(coordinates.version)
.also {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GitHubRepositorySnapshotBuilder(
for (resolutionRoot in resolvedConfigurations) {
for (dependency in resolutionRoot.allDependencies) {
// Ignore project dependencies (transitive deps of projects will be reported with project)
if (isProject(dependency)) continue
if (dependency.isProject) continue

dependencyCollector.addResolved(dependency)
}
Expand All @@ -51,11 +51,6 @@ class GitHubRepositorySnapshotBuilder(
}
}

// TODO:DAZ Model this better
private fun isProject(dependency: ResolvedDependency): Boolean {
return dependency.id.startsWith("project ")
}

fun buildSnapshot(manifest: GitHubManifest): GitHubRepositorySnapshot {
return GitHubRepositorySnapshot(
job = job,
Expand Down Expand Up @@ -90,7 +85,7 @@ class GitHubRepositorySnapshotBuilder(
}

private fun relationship(component: ResolvedDependency) =
if (component.direct) GitHubDependency.Relationship.direct else GitHubDependency.Relationship.indirect
if (component.isDirect) GitHubDependency.Relationship.direct else GitHubDependency.Relationship.indirect

private class GitHubDependencyBuilder(val package_url: String) {
var relationship: GitHubDependency.Relationship = GitHubDependency.Relationship.indirect
Expand Down

0 comments on commit 442319f

Please sign in to comment.