Skip to content

Commit

Permalink
refactor(gradle): Move OrtDependency extension functions to the model
Browse files Browse the repository at this point in the history
Prepare for reuse and also perform some minor generalizations. As part of
that, hard-code the "Gradle" project type in order to not be dependent on
a package manager instance.

Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
  • Loading branch information
sschuberth committed Sep 4, 2024
1 parent 97a81dd commit cb15705
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2021 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
*
* 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
*
* https://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.plugins.packagemanagers.gradlemodel

import OrtDependency

/**
* Determine the type of this Gradle dependency. In case of a project, it is "Gradle". Otherwise it is "Maven" unless
* there is no POM, then it is "Unknown".
*/
fun OrtDependency.dependencyType(): String =
if (isProjectDependency()) {
"Gradle"
} else {
pomFile?.let { "Maven" } ?: "Unknown"
}

/**
* Return true if this Gradle dependency refers to another project, or false if it refers to a package.
*/
fun OrtDependency.isProjectDependency() = localPath != null
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import org.ossreviewtoolkit.model.PackageLinkage
import org.ossreviewtoolkit.model.Severity
import org.ossreviewtoolkit.model.createAndLogIssue
import org.ossreviewtoolkit.model.utils.DependencyHandler
import org.ossreviewtoolkit.plugins.packagemanagers.gradlemodel.dependencyType
import org.ossreviewtoolkit.plugins.packagemanagers.gradlemodel.isProjectDependency
import org.ossreviewtoolkit.plugins.packagemanagers.maven.utils.MavenSupport
import org.ossreviewtoolkit.plugins.packagemanagers.maven.utils.identifier
import org.ossreviewtoolkit.utils.common.collectMessages
Expand Down Expand Up @@ -117,21 +119,4 @@ internal class GradleDependencyHandler(
}
}
}

/**
* 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 OrtDependency.dependencyType(): String =
if (isProjectDependency()) {
managerName
} else {
pomFile?.let { "Maven" } ?: "Unknown"
}
}

/**
* Return a flag whether this dependency references another project in the current build.
*/
private fun OrtDependency.isProjectDependency() = localPath != null
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import org.ossreviewtoolkit.model.Scope
import org.ossreviewtoolkit.model.Severity
import org.ossreviewtoolkit.model.VcsInfo
import org.ossreviewtoolkit.model.utils.DependencyGraphBuilder
import org.ossreviewtoolkit.plugins.packagemanagers.gradlemodel.dependencyType
import org.ossreviewtoolkit.plugins.packagemanagers.maven.utils.MavenSupport

/**
Expand Down Expand Up @@ -131,7 +132,7 @@ class GradleDependencyHandlerTest : WordSpec({

val scopes = graph.createScopes()

scopeDependencies(scopes, scope).single { it.id.type == NAME }
scopeDependencies(scopes, scope).single { it.id.type == "Gradle" }
}

"collect information about packages" {
Expand Down Expand Up @@ -312,23 +313,20 @@ class GradleDependencyHandlerTest : WordSpec({
val issues = mutableListOf<Issue>()

every { maven.parsePackage(any(), any(), useReposFromDependencies = false) } throws exception
val handler = GradleDependencyHandler(NAME, maven)
val handler = GradleDependencyHandler("Gradle", maven)

handler.createPackage(dep, issues) should beNull()

issues should haveSize(1)
with(issues.first()) {
source shouldBe NAME
source shouldBe "Gradle"
severity shouldBe Severity.ERROR
message should contain("${dep.groupId}:${dep.artifactId}:${dep.version}")
}
}
}
})

/** The name of the package manager. */
private const val NAME = "GradleTest"

/** Remote repositories used by the test. */
private val remoteRepositories = listOf(mockk<RemoteRepository>())

Expand Down Expand Up @@ -361,7 +359,7 @@ private fun createDependency(
* this class.
*/
private fun createGraphBuilder(): DependencyGraphBuilder<OrtDependency> {
val dependencyHandler = GradleDependencyHandler(NAME, createMavenSupport())
val dependencyHandler = GradleDependencyHandler("Gradle", createMavenSupport())
dependencyHandler.repositories = remoteRepositories
return DependencyGraphBuilder(dependencyHandler)
}
Expand Down Expand Up @@ -389,20 +387,10 @@ private fun createMavenSupport(): MavenSupport {
return maven
}

/**
* Determine the type of the [Identifier] for this dependency.
*/
private fun OrtDependency.type(): String =
if (localPath != null) {
NAME
} else {
"Maven"
}

/**
* Returns an [Identifier] for this [OrtDependency].
*/
private fun OrtDependency.toId() = Identifier(type(), groupId, artifactId, version)
private fun OrtDependency.toId() = Identifier(dependencyType(), groupId, artifactId, version)

/**
* Return the package references from the given [scopes] associated with the scope with the given [scopeName].
Expand Down

0 comments on commit cb15705

Please sign in to comment.