diff --git a/analyzer/src/funTest/kotlin/curation/ClearlyDefinedPackageCurationProviderFunTest.kt b/analyzer/src/funTest/kotlin/curation/ClearlyDefinedPackageCurationProviderFunTest.kt index b4f4a42ec5cdb..6267288600fdf 100644 --- a/analyzer/src/funTest/kotlin/curation/ClearlyDefinedPackageCurationProviderFunTest.kt +++ b/analyzer/src/funTest/kotlin/curation/ClearlyDefinedPackageCurationProviderFunTest.kt @@ -28,6 +28,7 @@ import io.kotest.matchers.shouldNot import org.ossreviewtoolkit.clients.clearlydefined.ClearlyDefinedService.Server import org.ossreviewtoolkit.model.Identifier +import org.ossreviewtoolkit.model.Package import org.ossreviewtoolkit.utils.spdx.toSpdx class ClearlyDefinedPackageCurationProviderFunTest : WordSpec({ @@ -35,9 +36,9 @@ class ClearlyDefinedPackageCurationProviderFunTest : WordSpec({ val provider = ClearlyDefinedPackageCurationProvider() "return an existing curation for the javax.servlet-api Maven package" { - val identifier = Identifier("Maven:javax.servlet:javax.servlet-api:3.1.0") + val packages = createPackagesFromIds("Maven:javax.servlet:javax.servlet-api:3.1.0") - val curations = provider.getCurationsFor(listOf(identifier)) + val curations = provider.getCurationsFor(packages) curations should haveSize(1) curations.values.flatten().first().data.concludedLicense shouldBe @@ -45,18 +46,18 @@ class ClearlyDefinedPackageCurationProviderFunTest : WordSpec({ } "return an existing curation for the slf4j-log4j12 Maven package" { - val identifier = Identifier("Maven:org.slf4j:slf4j-log4j12:1.7.30") + val packages = createPackagesFromIds("Maven:org.slf4j:slf4j-log4j12:1.7.30") - val curations = provider.getCurationsFor(listOf(identifier)) + val curations = provider.getCurationsFor(packages) curations should haveSize(1) curations.values.flatten().first().data.vcs?.revision shouldBe "0b97c416e42a184ff9728877b461c616187c58f7" } "return no curation for a non-existing dummy NPM package" { - val identifier = Identifier("NPM:@scope:name:1.2.3") + val packages = createPackagesFromIds("NPM:@scope:name:1.2.3") - val curations = provider.getCurationsFor(listOf(identifier)) + val curations = provider.getCurationsFor(packages) curations should beEmpty() } @@ -66,18 +67,18 @@ class ClearlyDefinedPackageCurationProviderFunTest : WordSpec({ val provider = ClearlyDefinedPackageCurationProvider(Server.DEVELOPMENT) "return an existing curation for the platform-express NPM package" { - val identifier = Identifier("NPM:@nestjs:platform-express:6.2.3") + val packages = createPackagesFromIds("NPM:@nestjs:platform-express:6.2.3") - val curations = provider.getCurationsFor(listOf(identifier)) + val curations = provider.getCurationsFor(packages) curations should haveSize(1) curations.values.flatten().first().data.concludedLicense shouldBe "Apache-1.0".toSpdx() } "return no curation for a non-existing dummy Maven package" { - val identifier = Identifier("Maven:group:name:1.2.3") + val packages = createPackagesFromIds("Maven:group:name:1.2.3") - val curations = provider.getCurationsFor(listOf(identifier)) + val curations = provider.getCurationsFor(packages) curations should beEmpty() } @@ -92,20 +93,23 @@ class ClearlyDefinedPackageCurationProviderFunTest : WordSpec({ val provider = ClearlyDefinedPackageCurationProvider(config) // Use an id which is known to have non-empty results from an earlier test. - val identifier = Identifier("Maven:org.slf4j:slf4j-log4j12:1.7.30") + val packages = createPackagesFromIds("Maven:org.slf4j:slf4j-log4j12:1.7.30") - val curations = provider.getCurationsFor(listOf(identifier)) + val curations = provider.getCurationsFor(packages) curations should beEmpty() } "be retrieved for packages without a namespace" { val provider = ClearlyDefinedPackageCurationProvider() - val identifier = Identifier("NPM::acorn:0.6.0") + val packages = createPackagesFromIds("NPM::acorn:0.6.0") - val curations = provider.getCurationsFor(listOf(identifier)) + val curations = provider.getCurationsFor(packages) curations shouldNot beEmpty() } } }) + +private fun createPackagesFromIds(vararg ids: String) = + ids.map { Package.EMPTY.copy(id = Identifier(it)) } diff --git a/analyzer/src/funTest/kotlin/curation/OrtConfigPackageCurationProviderFunTest.kt b/analyzer/src/funTest/kotlin/curation/OrtConfigPackageCurationProviderFunTest.kt index 6e870030dc5c5..89a04c658a3dc 100644 --- a/analyzer/src/funTest/kotlin/curation/OrtConfigPackageCurationProviderFunTest.kt +++ b/analyzer/src/funTest/kotlin/curation/OrtConfigPackageCurationProviderFunTest.kt @@ -27,13 +27,15 @@ import io.kotest.matchers.should import io.kotest.matchers.shouldNot import org.ossreviewtoolkit.model.Identifier +import org.ossreviewtoolkit.model.Package class OrtConfigPackageCurationProviderFunTest : StringSpec({ "provider can load curations from the ort-config repository" { val azureCore = Identifier("NuGet::Azure.Core:1.22.0") val azureCoreAmqp = Identifier("NuGet::Azure.Core.Amqp:1.2.0") + val packages = createPackagesFromIds(azureCore, azureCoreAmqp) - val curations = OrtConfigPackageCurationProvider().getCurationsFor(listOf(azureCore, azureCoreAmqp)) + val curations = OrtConfigPackageCurationProvider().getCurationsFor(packages) curations.shouldContainKeys(azureCore, azureCoreAmqp) curations.getValue(azureCore) shouldNot beEmpty() @@ -41,10 +43,13 @@ class OrtConfigPackageCurationProviderFunTest : StringSpec({ } "provider does not fail for packages which have no curations" { - val id = Identifier("Some:Bogus:Package:Id") + val packages = createPackagesFromIds(Identifier("Some:Bogus:Package:Id")) - val curations = OrtConfigPackageCurationProvider().getCurationsFor(listOf(id)) + val curations = OrtConfigPackageCurationProvider().getCurationsFor(packages) curations should beEmptyMap() } }) + +private fun createPackagesFromIds(vararg ids: Identifier) = + ids.map { Package.EMPTY.copy(id = it) } diff --git a/analyzer/src/main/kotlin/AnalyzerResultBuilder.kt b/analyzer/src/main/kotlin/AnalyzerResultBuilder.kt index 99e85d3707728..ef0270e7f7140 100644 --- a/analyzer/src/main/kotlin/AnalyzerResultBuilder.kt +++ b/analyzer/src/main/kotlin/AnalyzerResultBuilder.kt @@ -98,7 +98,7 @@ class AnalyzerResultBuilder(private val curationProvider: PackageCurationProvide * independently of a [ProjectAnalyzerResult]. */ fun addPackages(packageSet: Set): AnalyzerResultBuilder { - val (curations, duration) = measureTimedValue { curationProvider.getCurationsFor(packageSet.map { it.id }) } + val (curations, duration) = measureTimedValue { curationProvider.getCurationsFor(packageSet) } logger.debug { "Getting package curations took $duration." } diff --git a/analyzer/src/main/kotlin/PackageCurationProvider.kt b/analyzer/src/main/kotlin/PackageCurationProvider.kt index 20bdaa9b00553..a394dc7c562c8 100644 --- a/analyzer/src/main/kotlin/PackageCurationProvider.kt +++ b/analyzer/src/main/kotlin/PackageCurationProvider.kt @@ -20,6 +20,7 @@ package org.ossreviewtoolkit.analyzer import org.ossreviewtoolkit.model.Identifier +import org.ossreviewtoolkit.model.Package import org.ossreviewtoolkit.model.PackageCuration import org.ossreviewtoolkit.model.config.PackageCurationProviderConfiguration import org.ossreviewtoolkit.utils.common.ConfigurablePluginFactory @@ -64,10 +65,10 @@ fun interface PackageCurationProvider { } /** - * Return all available [PackageCuration]s for the provided [pkgIds], associated by the package's [Identifier]. Each - * list of curations must be non-empty; if no curation is available for a package, the returned map must not contain - * a key for that package's identifier at all. + * Return all available [PackageCuration]s for the provided [packages], associated by the package's [Identifier]. + * Each list of curations must be non-empty; if no curation is available for a package, the returned map must not + * contain a key for that package's identifier at all. */ // TODO: Maybe make this a suspend function, then all implementing classes could deal with coroutines more easily. - fun getCurationsFor(pkgIds: Collection): Map> + fun getCurationsFor(packages: Collection): Map> } diff --git a/analyzer/src/main/kotlin/curation/ClearlyDefinedPackageCurationProvider.kt b/analyzer/src/main/kotlin/curation/ClearlyDefinedPackageCurationProvider.kt index 06e13c669a63a..f1df028084cdd 100644 --- a/analyzer/src/main/kotlin/curation/ClearlyDefinedPackageCurationProvider.kt +++ b/analyzer/src/main/kotlin/curation/ClearlyDefinedPackageCurationProvider.kt @@ -38,6 +38,7 @@ import org.ossreviewtoolkit.clients.clearlydefined.getCurationsChunked import org.ossreviewtoolkit.clients.clearlydefined.getDefinitionsChunked import org.ossreviewtoolkit.model.Hash import org.ossreviewtoolkit.model.Identifier +import org.ossreviewtoolkit.model.Package import org.ossreviewtoolkit.model.PackageCuration import org.ossreviewtoolkit.model.PackageCurationData import org.ossreviewtoolkit.model.RemoteArtifact @@ -97,11 +98,11 @@ class ClearlyDefinedPackageCurationProvider( ClearlyDefinedService.create(config.serverUrl, client ?: OkHttpClientHelper.buildClient()) } - override fun getCurationsFor(pkgIds: Collection): Map> { - val coordinatesToIds = pkgIds.mapNotNull { pkgId -> - pkgId.toClearlyDefinedTypeAndProvider()?.let { (type, provider) -> - val namespace = pkgId.namespace.takeUnless { it.isEmpty() } - Coordinates(type, provider, namespace, pkgId.name, pkgId.version) to pkgId + override fun getCurationsFor(packages: Collection): Map> { + val coordinatesToIds = packages.mapNotNull { pkg -> + pkg.toClearlyDefinedTypeAndProvider()?.let { (type, provider) -> + val namespace = pkg.id.namespace.takeUnless { it.isEmpty() } + Coordinates(type, provider, namespace, pkg.id.name, pkg.id.version) to pkg.id } }.toMap() diff --git a/analyzer/src/main/kotlin/curation/CompositePackageCurationProvider.kt b/analyzer/src/main/kotlin/curation/CompositePackageCurationProvider.kt index 9e15a728436d5..808be2c1df9a0 100644 --- a/analyzer/src/main/kotlin/curation/CompositePackageCurationProvider.kt +++ b/analyzer/src/main/kotlin/curation/CompositePackageCurationProvider.kt @@ -21,6 +21,7 @@ package org.ossreviewtoolkit.analyzer.curation import org.ossreviewtoolkit.analyzer.PackageCurationProvider import org.ossreviewtoolkit.model.Identifier +import org.ossreviewtoolkit.model.Package import org.ossreviewtoolkit.model.PackageCuration /** @@ -28,11 +29,11 @@ import org.ossreviewtoolkit.model.PackageCuration * single curation provider. All matching curations of all providers are provided in order of declaration. */ class CompositePackageCurationProvider(private val providers: List) : PackageCurationProvider { - override fun getCurationsFor(pkgIds: Collection): Map> { + override fun getCurationsFor(packages: Collection): Map> { val allCurations = mutableMapOf>() providers.forEach { provider -> - provider.getCurationsFor(pkgIds).forEach { (pkgId, curations) -> + provider.getCurationsFor(packages).forEach { (pkgId, curations) -> allCurations.getOrPut(pkgId) { mutableListOf() } += curations } } diff --git a/analyzer/src/main/kotlin/curation/OrtConfigPackageCurationProvider.kt b/analyzer/src/main/kotlin/curation/OrtConfigPackageCurationProvider.kt index f10760b72ad0f..f488eef49538d 100644 --- a/analyzer/src/main/kotlin/curation/OrtConfigPackageCurationProvider.kt +++ b/analyzer/src/main/kotlin/curation/OrtConfigPackageCurationProvider.kt @@ -28,6 +28,7 @@ import org.ossreviewtoolkit.analyzer.PackageCurationProvider import org.ossreviewtoolkit.analyzer.PackageCurationProviderFactory import org.ossreviewtoolkit.downloader.vcs.Git import org.ossreviewtoolkit.model.Identifier +import org.ossreviewtoolkit.model.Package import org.ossreviewtoolkit.model.PackageCuration import org.ossreviewtoolkit.model.VcsInfo import org.ossreviewtoolkit.model.VcsType @@ -60,9 +61,9 @@ open class OrtConfigPackageCurationProvider : PackageCurationProvider { } } - override fun getCurationsFor(pkgIds: Collection) = - pkgIds.mapNotNull { pkgId -> - getCurationsFor(pkgId).takeUnless { it.isEmpty() }?.let { pkgId to it } + override fun getCurationsFor(packages: Collection) = + packages.mapNotNull { pkg -> + getCurationsFor(pkg.id).takeUnless { it.isEmpty() }?.let { pkg.id to it } }.toMap() private fun getCurationsFor(pkgId: Identifier): List { diff --git a/analyzer/src/main/kotlin/curation/SimplePackageCurationProvider.kt b/analyzer/src/main/kotlin/curation/SimplePackageCurationProvider.kt index 8a9fbde69dbcb..d28a0f2d9b08e 100644 --- a/analyzer/src/main/kotlin/curation/SimplePackageCurationProvider.kt +++ b/analyzer/src/main/kotlin/curation/SimplePackageCurationProvider.kt @@ -20,15 +20,15 @@ package org.ossreviewtoolkit.analyzer.curation import org.ossreviewtoolkit.analyzer.PackageCurationProvider -import org.ossreviewtoolkit.model.Identifier +import org.ossreviewtoolkit.model.Package import org.ossreviewtoolkit.model.PackageCuration /** * A [PackageCurationProvider] that provides the specified [packageCurations]. */ open class SimplePackageCurationProvider(val packageCurations: List) : PackageCurationProvider { - override fun getCurationsFor(pkgIds: Collection) = - pkgIds.mapNotNull { pkgId -> - packageCurations.filter { it.isApplicable(pkgId) }.takeUnless { it.isEmpty() }?.let { pkgId to it } + override fun getCurationsFor(packages: Collection) = + packages.mapNotNull { pkg -> + packageCurations.filter { it.isApplicable(pkg.id) }.takeUnless { it.isEmpty() }?.let { pkg.id to it } }.toMap() } diff --git a/analyzer/src/main/kotlin/curation/Sw360PackageCurationProvider.kt b/analyzer/src/main/kotlin/curation/Sw360PackageCurationProvider.kt index 757bc5bacadd4..5f56a4acb1938 100644 --- a/analyzer/src/main/kotlin/curation/Sw360PackageCurationProvider.kt +++ b/analyzer/src/main/kotlin/curation/Sw360PackageCurationProvider.kt @@ -37,6 +37,7 @@ import org.ossreviewtoolkit.analyzer.PackageCurationProviderFactory import org.ossreviewtoolkit.model.Hash import org.ossreviewtoolkit.model.HashAlgorithm import org.ossreviewtoolkit.model.Identifier +import org.ossreviewtoolkit.model.Package import org.ossreviewtoolkit.model.PackageCuration import org.ossreviewtoolkit.model.PackageCurationData import org.ossreviewtoolkit.model.RemoteArtifact @@ -97,9 +98,9 @@ class Sw360PackageCurationProvider(config: Sw360StorageConfiguration) : PackageC private val connectionFactory = createConnection(config) private val releaseClient = connectionFactory.releaseAdapter - override fun getCurationsFor(pkgIds: Collection) = - pkgIds.mapNotNull { pkgId -> - getCurationsFor(pkgId).takeUnless { it.isEmpty() }?.let { pkgId to it } + override fun getCurationsFor(packages: Collection) = + packages.mapNotNull { pkg -> + getCurationsFor(pkg.id).takeUnless { it.isEmpty() }?.let { pkg.id to it } }.toMap() private fun getCurationsFor(pkgId: Identifier): List { diff --git a/analyzer/src/test/kotlin/curation/ClearlyDefinedPackageCurationProviderTest.kt b/analyzer/src/test/kotlin/curation/ClearlyDefinedPackageCurationProviderTest.kt index 96ef6f608ff2d..e7d14f2850a0a 100644 --- a/analyzer/src/test/kotlin/curation/ClearlyDefinedPackageCurationProviderTest.kt +++ b/analyzer/src/test/kotlin/curation/ClearlyDefinedPackageCurationProviderTest.kt @@ -32,6 +32,7 @@ import io.kotest.matchers.should import java.time.Duration import org.ossreviewtoolkit.model.Identifier +import org.ossreviewtoolkit.model.Package import org.ossreviewtoolkit.utils.ort.OkHttpClientHelper /** @@ -68,9 +69,10 @@ class ClearlyDefinedPackageCurationProviderTest : WordSpec({ } val provider = ClearlyDefinedPackageCurationProvider("http://localhost:${server.port()}", client) - val ids = listOf(Identifier("Maven:some-ns:some-component:1.2.3")) + val id = Identifier("Maven:some-ns:some-component:1.2.3") + val packages = listOf(Package.EMPTY.copy(id = id)) - provider.getCurationsFor(ids) should beEmpty() + provider.getCurationsFor(packages) should beEmpty() } } }) diff --git a/analyzer/src/test/kotlin/curation/FilePackageCurationProviderTest.kt b/analyzer/src/test/kotlin/curation/FilePackageCurationProviderTest.kt index 42cf6a2a694a2..74a3fa1fabca5 100644 --- a/analyzer/src/test/kotlin/curation/FilePackageCurationProviderTest.kt +++ b/analyzer/src/test/kotlin/curation/FilePackageCurationProviderTest.kt @@ -21,14 +21,17 @@ package org.ossreviewtoolkit.analyzer.curation import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.StringSpec +import io.kotest.inspectors.forAll import io.kotest.matchers.collections.beEmpty import io.kotest.matchers.collections.haveSize +import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder import io.kotest.matchers.should import io.kotest.matchers.shouldBe import java.io.File import org.ossreviewtoolkit.model.Identifier +import org.ossreviewtoolkit.model.Package class FilePackageCurationProviderTest : StringSpec() { private val curationsDir = File("src/test/assets/package-curations-dir") @@ -47,11 +50,13 @@ class FilePackageCurationProviderTest : StringSpec() { Identifier("maven", "org.ossreviewtoolkit", "example", "1.0"), Identifier("maven", "org.foo", "bar", "0.42") ) + val packages = idsWithExistingCurations.map { Package.EMPTY.copy(id = it) } - idsWithExistingCurations.forEach { - val curations = provider.getCurationsFor(listOf(it)).values.flatten() + val curations = provider.getCurationsFor(packages) - curations should haveSize(1) + curations.keys shouldContainExactlyInAnyOrder idsWithExistingCurations + curations.values.forAll { + it should haveSize(1) } } @@ -65,11 +70,13 @@ class FilePackageCurationProviderTest : StringSpec() { Identifier("maven", "org.ossreviewtoolkit", "example", "1.0"), Identifier("maven", "org.foo", "bar", "0.42") ) + val packages = idsWithExistingCurations.map { Package.EMPTY.copy(id = it) } - idsWithExistingCurations.forEach { - val curations = provider.getCurationsFor(listOf(it)).values.flatten() + val curations = provider.getCurationsFor(packages) - curations should haveSize(1) + curations.keys shouldContainExactlyInAnyOrder idsWithExistingCurations + curations.values.forAll { + it should haveSize(1) } } @@ -77,7 +84,8 @@ class FilePackageCurationProviderTest : StringSpec() { val provider = FilePackageCurationProvider(curationsFile) val identifier = Identifier("maven", "org.hamcrest", "hamcrest-core", "1.3") - val curations = provider.getCurationsFor(listOf(identifier)).values.flatten() + val packages = listOf(Package.EMPTY.copy(id = identifier)) + val curations = provider.getCurationsFor(packages).values.flatten() curations should haveSize(4) curations.forEach { @@ -95,9 +103,9 @@ class FilePackageCurationProviderTest : StringSpec() { val idMaxVersion = Identifier("npm", "", "ramda", "0.25.0") val idOutVersion = Identifier("npm", "", "ramda", "0.26.0") - val curationsMinVersion = provider.getCurationsFor(listOf(idMinVersion)).values.flatten() - val curationsMaxVersion = provider.getCurationsFor(listOf(idMaxVersion)).values.flatten() - val curationsOutVersion = provider.getCurationsFor(listOf(idOutVersion)).values.flatten() + val curationsMinVersion = provider.getCurationsFor(createPackagesFromIds(idMinVersion)).values.flatten() + val curationsMaxVersion = provider.getCurationsFor(createPackagesFromIds(idMaxVersion)).values.flatten() + val curationsOutVersion = provider.getCurationsFor(createPackagesFromIds(idOutVersion)).values.flatten() curationsMinVersion should haveSize(1) (provider.packageCurations - curationsMinVersion).forEach { @@ -129,3 +137,6 @@ class FilePackageCurationProviderTest : StringSpec() { } } } + +private fun createPackagesFromIds(vararg ids: Identifier) = + ids.map { Package.EMPTY.copy(id = it) } diff --git a/cli/src/main/kotlin/commands/UploadCurationsCommand.kt b/cli/src/main/kotlin/commands/UploadCurationsCommand.kt index 8916128af616b..5400eaf7226e6 100644 --- a/cli/src/main/kotlin/commands/UploadCurationsCommand.kt +++ b/cli/src/main/kotlin/commands/UploadCurationsCommand.kt @@ -44,6 +44,7 @@ import org.ossreviewtoolkit.clients.clearlydefined.HarvestStatus import org.ossreviewtoolkit.clients.clearlydefined.Patch import org.ossreviewtoolkit.clients.clearlydefined.callBlocking import org.ossreviewtoolkit.clients.clearlydefined.getDefinitionsChunked +import org.ossreviewtoolkit.model.Package import org.ossreviewtoolkit.model.PackageCuration import org.ossreviewtoolkit.model.PackageCurationData import org.ossreviewtoolkit.model.readValueOrDefault @@ -84,9 +85,8 @@ class UploadCurationsCommand : OrtCommand( }.values val curationsToCoordinates = curations.mapNotNull { curation -> - curation.id.toClearlyDefinedCoordinates()?.let { coordinates -> - curation to coordinates - } + val pkg = Package.EMPTY.copy(id = curation.id) + pkg.toClearlyDefinedCoordinates()?.let { curation to it } }.toMap() val definitions = service.getDefinitionsChunked(curationsToCoordinates.values) @@ -146,7 +146,8 @@ class UploadCurationsCommand : OrtCommand( } private fun PackageCuration.toContributionPatch(): ContributionPatch? { - val coordinates = id.toClearlyDefinedCoordinates() ?: return null + val pkg = Package.EMPTY.copy(id = id) + val coordinates = pkg.toClearlyDefinedCoordinates() ?: return null val info = ContributionInfo( // The exact values to use here are unclear; use what is mostly used at @@ -165,7 +166,7 @@ private fun PackageCuration.toContributionPatch(): ContributionPatch? { val described = CurationDescribed( projectWebsite = data.homepageUrl?.let { URI(it) }, - sourceLocation = id.toClearlyDefinedSourceLocation(data.vcs, data.sourceArtifact) + sourceLocation = pkg.toClearlyDefinedSourceLocation(data.vcs, data.sourceArtifact) ) val curation = Curation( diff --git a/model/src/main/kotlin/utils/Extensions.kt b/model/src/main/kotlin/utils/Extensions.kt index 612fd0fe7993b..5901c1df20645 100644 --- a/model/src/main/kotlin/utils/Extensions.kt +++ b/model/src/main/kotlin/utils/Extensions.kt @@ -36,12 +36,12 @@ internal fun TextLocation.prependPath(prefix: String): String = if (prefix.isBlank()) path else "${prefix.removeSuffix("/")}/$path" /** - * Map an [Identifier] to a ClearlyDefined [ComponentType] and [Provider]. Note that an + * Map an [Package] to a ClearlyDefined [ComponentType] and [Provider]. Note that an * [identifier's type][Identifier.type] in ORT currently implies a default provider. Return null if a mapping is not * possible. */ -fun Identifier.toClearlyDefinedTypeAndProvider(): Pair? = - when (type) { +fun Package.toClearlyDefinedTypeAndProvider(): Pair? = + when (id.type) { "Bower" -> ComponentType.GIT to Provider.GITHUB "CocoaPods" -> ComponentType.POD to Provider.COCOAPODS "Composer" -> ComponentType.COMPOSER to Provider.PACKAGIST @@ -57,24 +57,24 @@ fun Identifier.toClearlyDefinedTypeAndProvider(): Pair? } /** - * Map an ORT [Identifier] to ClearlyDefined [Coordinates], or to null if mapping is not possible. + * Map an ORT [Package] to ClearlyDefined [Coordinates], or to null if mapping is not possible. */ -fun Identifier.toClearlyDefinedCoordinates(): Coordinates? = +fun Package.toClearlyDefinedCoordinates(): Coordinates? = toClearlyDefinedTypeAndProvider()?.let { (type, provider) -> Coordinates( type = type, provider = provider, - namespace = namespace.takeUnless { it.isEmpty() }, - name = name, - revision = version.takeUnless { it.isEmpty() } + namespace = id.namespace.takeUnless { it.isEmpty() }, + name = id.name, + revision = id.version.takeUnless { it.isEmpty() } ) } /** - * Create a ClearlyDefined [SourceLocation] from an [Identifier]. Prefer a [VcsInfoCurationData], but eventually fall + * Create a ClearlyDefined [SourceLocation] from a [Package]. Prefer a [VcsInfoCurationData], but eventually fall * back to a [RemoteArtifact], or return null if not enough information is available. */ -fun Identifier.toClearlyDefinedSourceLocation( +fun Package.toClearlyDefinedSourceLocation( vcs: VcsInfoCurationData?, sourceArtifact: RemoteArtifact? ): SourceLocation? { @@ -100,10 +100,10 @@ fun Identifier.toClearlyDefinedSourceLocation( sourceArtifact != null -> { toClearlyDefinedTypeAndProvider()?.let { (_, provider) -> SourceLocation( - name = name, - namespace = namespace.takeUnless { it.isEmpty() }, + name = id.name, + namespace = id.namespace.takeUnless { it.isEmpty() }, provider = provider, - revision = version, + revision = id.version, type = ComponentType.SOURCE_ARCHIVE, url = sourceArtifact.url ) diff --git a/scanner/src/main/kotlin/storages/ClearlyDefinedStorage.kt b/scanner/src/main/kotlin/storages/ClearlyDefinedStorage.kt index 25f6b23b7628f..155e67d6bf0dd 100644 --- a/scanner/src/main/kotlin/storages/ClearlyDefinedStorage.kt +++ b/scanner/src/main/kotlin/storages/ClearlyDefinedStorage.kt @@ -76,8 +76,8 @@ private fun VcsInfo.toVcsInfoCurationData(): VcsInfoCurationData = VcsInfoCurati private fun packageCoordinates(pkg: Package): Coordinates { val vcs = pkg.vcs.takeUnless { it.url.isEmpty() } val sourceArtifact = pkg.sourceArtifact.takeUnless { it.url.isEmpty() } - val sourceLocation = pkg.id.toClearlyDefinedSourceLocation(vcs?.toVcsInfoCurationData(), sourceArtifact) - return sourceLocation?.toCoordinates() ?: pkg.id.toClearlyDefinedCoordinates() + val sourceLocation = pkg.toClearlyDefinedSourceLocation(vcs?.toVcsInfoCurationData(), sourceArtifact) + return sourceLocation?.toCoordinates() ?: pkg.toClearlyDefinedCoordinates() ?: throw IllegalArgumentException("Unable to create ClearlyDefined coordinates for $pkg.") }