Skip to content

Commit b253a24

Browse files
author
Kamil Bielecki
committed
feat: Extend Identifier with fromPurl() method
This commit adds method to Identifier object that allows creation of new Identifier object from PURL. Signed-off-by: Kamil Bielecki <kamil.bielecki@pl.bosch.com>
1 parent db2bb19 commit b253a24

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ mordant = "3.0.2"
5555
okhttp = "5.3.2"
5656
postgres = "42.7.8"
5757
postgresEmbedded = "1.1.1"
58+
purlkt = "0.0.7"
5859
reflections = "0.10.2"
5960
retrofit = "3.0.0"
6061
s3 = "2.40.1"
@@ -167,6 +168,7 @@ okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
167168
okhttp-loggingInterceptor = { module = "com.squareup.okhttp3:logging-interceptor", version.ref = "okhttp" }
168169
postgres = { module = "org.postgresql:postgresql", version.ref = "postgres" }
169170
postgresEmbedded = { module = "com.opentable.components:otj-pg-embedded", version.ref = "postgresEmbedded" }
171+
purlkt = { module = "space.iseki.purlkt:purlkt", version.ref = "purlkt"}
170172
reflections = { module = "org.reflections:reflections", version.ref = "reflections" }
171173
retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofit" }
172174
retrofit-converter-jackson = { module = "com.squareup.retrofit2:converter-jackson", version.ref = "retrofit" }

model/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ dependencies {
4141
implementation(libs.bundles.hoplite)
4242
implementation(libs.hikari)
4343
implementation(libs.postgres)
44+
implementation(libs.purlkt)
4445
implementation(libs.semver4j)
4546
implementation(libs.tika)
4647

model/src/main/kotlin/Identifier.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import com.fasterxml.jackson.annotation.JsonValue
2525
import org.ossreviewtoolkit.utils.common.AlphaNumericComparator
2626
import org.ossreviewtoolkit.utils.common.encodeOr
2727

28+
import space.iseki.purl.PUrl
29+
import space.iseki.purl.PUrlParsingException
30+
2831
/**
2932
* A unique identifier for a software component.
3033
*/
@@ -66,6 +69,16 @@ data class Identifier(
6669
// This comparator is consistent with `equals()` as all properties are taken into account.
6770
private val COMPARATOR = compareBy<Identifier>({ it.type }, { it.namespace }, { it.name })
6871
.thenComparing({ it.version }, AlphaNumericComparator)
72+
73+
@Suppress("SwallowedException")
74+
fun fromPurl(purlString: String): Identifier {
75+
try {
76+
val purl = PUrl.parse(purlString)
77+
return Identifier(purl.type, purl.namespace.joinToString("."), purl.name, purl.version)
78+
} catch (ex: PUrlParsingException) {
79+
throw IllegalArgumentException(ex.message)
80+
}
81+
}
6982
}
7083

7184
private constructor(properties: List<String>) : this(

model/src/test/kotlin/IdentifierTest.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.ossreviewtoolkit.model
2121

2222
import io.kotest.assertions.assertSoftly
23+
import io.kotest.assertions.throwables.shouldThrow
2324
import io.kotest.core.spec.style.WordSpec
2425
import io.kotest.inspectors.forAll
2526
import io.kotest.matchers.maps.containExactly
@@ -135,4 +136,34 @@ class IdentifierTest : WordSpec({
135136
}
136137
}
137138
}
139+
140+
"Purl representation" should {
141+
"should accept only pkg type purls" {
142+
shouldThrow<IllegalArgumentException> {
143+
Identifier.fromPurl("nonpkg:someurl/example.com/invalid@1.1")
144+
}
145+
}
146+
147+
"be parsed correctly for purl with namespace" {
148+
val id = Identifier.fromPurl("pkg:github/example.com/valid-with-namespace@1.0")
149+
150+
with(id) {
151+
type shouldBe "github"
152+
namespace shouldBe "example.com"
153+
name shouldBe "valid-with-namespace"
154+
version shouldBe "1.0"
155+
}
156+
}
157+
158+
"be parsed correctly for purl without namespace" {
159+
val id = Identifier.fromPurl("pkg:pypi/valid-without-namespace@2.0")
160+
161+
with(id) {
162+
type shouldBe "pypi"
163+
namespace shouldBe ""
164+
name shouldBe "valid-without-namespace"
165+
version shouldBe "2.0"
166+
}
167+
}
168+
}
138169
})

0 commit comments

Comments
 (0)