From d8eff80ec89517ee657fdc0192d0d7ebab9bb3fc Mon Sep 17 00:00:00 2001 From: Igor Brovtsin Date: Thu, 28 Mar 2024 17:42:06 +0100 Subject: [PATCH] fix(maven): return coordinates-only `Package` on `parsePackage` exception This allows the dependency to still be in the `packages` list of the report even if POM resolution failed for some reason. Signed-off-by: Igor Brovtsin --- .../kotlin/utils/MavenDependencyHandler.kt | 21 +++++++++++++++- .../utils/MavenDependencyHandlerTest.kt | 24 +++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/plugins/package-managers/maven/src/main/kotlin/utils/MavenDependencyHandler.kt b/plugins/package-managers/maven/src/main/kotlin/utils/MavenDependencyHandler.kt index 355fdf1f53c9a..6a1560f7c202b 100644 --- a/plugins/package-managers/maven/src/main/kotlin/utils/MavenDependencyHandler.kt +++ b/plugins/package-managers/maven/src/main/kotlin/utils/MavenDependencyHandler.kt @@ -28,6 +28,8 @@ import org.ossreviewtoolkit.model.Identifier import org.ossreviewtoolkit.model.Issue import org.ossreviewtoolkit.model.Package import org.ossreviewtoolkit.model.PackageLinkage +import org.ossreviewtoolkit.model.RemoteArtifact +import org.ossreviewtoolkit.model.VcsInfo import org.ossreviewtoolkit.model.createAndLogIssue import org.ossreviewtoolkit.model.utils.DependencyHandler import org.ossreviewtoolkit.plugins.packagemanagers.maven.Maven @@ -112,7 +114,24 @@ class MavenDependencyHandler( message = "Could not get package information for dependency '" + "${dependency.artifact.identifier()}': ${e.collectMessages()}" ) - }.getOrNull() + }.getOrElse { + Package( + id = dependency.artifact.run { + Identifier( + type = "Maven", + namespace = groupId, + name = artifactId, + version = version + ) + }, + binaryArtifact = RemoteArtifact.EMPTY, + declaredLicenses = emptySet(), + description = "", + homepageUrl = "", + sourceArtifact = RemoteArtifact.EMPTY, + vcs = VcsInfo.EMPTY + ) + } } /** diff --git a/plugins/package-managers/maven/src/test/kotlin/utils/MavenDependencyHandlerTest.kt b/plugins/package-managers/maven/src/test/kotlin/utils/MavenDependencyHandlerTest.kt index 80bda9ce17d4c..5900afe90bf9a 100644 --- a/plugins/package-managers/maven/src/test/kotlin/utils/MavenDependencyHandlerTest.kt +++ b/plugins/package-managers/maven/src/test/kotlin/utils/MavenDependencyHandlerTest.kt @@ -24,6 +24,7 @@ import io.kotest.matchers.collections.beEmpty import io.kotest.matchers.collections.containExactly import io.kotest.matchers.collections.haveSize import io.kotest.matchers.nulls.beNull +import io.kotest.matchers.nulls.shouldNotBeNull import io.kotest.matchers.should import io.kotest.matchers.shouldBe import io.kotest.matchers.string.contain @@ -46,7 +47,9 @@ import org.ossreviewtoolkit.model.Identifier import org.ossreviewtoolkit.model.Issue import org.ossreviewtoolkit.model.Package import org.ossreviewtoolkit.model.PackageLinkage +import org.ossreviewtoolkit.model.RemoteArtifact import org.ossreviewtoolkit.model.Severity +import org.ossreviewtoolkit.model.VcsInfo class MavenDependencyHandlerTest : WordSpec({ beforeSpec { @@ -204,7 +207,7 @@ class MavenDependencyHandlerTest : WordSpec({ handler.linkageFor(dependency) shouldBe PackageLinkage.PROJECT_DYNAMIC } - "handle an exception from MavenSupport" { + "return coordinates-only Package when an exception is raised from MavenSupport" { val exception = ProjectBuildingException( "BrokenProject", "Cannot parse pom.", IOException("General failure when reading hard disk.") @@ -219,7 +222,24 @@ class MavenDependencyHandlerTest : WordSpec({ every { dependency.repositories } returns repos every { handler.support.parsePackage(artifact, repos) } throws exception - handler.createPackage(dependency, issues) should beNull() + val pkg = handler.createPackage(dependency, issues) + pkg.shouldNotBeNull() + + with(pkg.id) { + type shouldBe "Maven" + namespace shouldBe artifact.groupId + name shouldBe artifact.artifactId + version shouldBe artifact.version + } + + with(pkg) { + binaryArtifact shouldBe RemoteArtifact.EMPTY + declaredLicenses should beEmpty() + description shouldBe "" + homepageUrl shouldBe "" + sourceArtifact shouldBe RemoteArtifact.EMPTY + vcs shouldBe VcsInfo.EMPTY + } issues should haveSize(1) with(issues[0]) {