Skip to content

Commit 02fb740

Browse files
committed
feat(utils): Add sourceCodeOrigin to the package configuration
The source code origin defines whether the package configuration applies to any source artifact or VCS location. See [1]. [1]: #9918 (comment) Signed-off-by: Nicolas Nobelis <[email protected]>
1 parent 7bdac39 commit 02fb740

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

model/src/main/kotlin/config/PackageConfiguration.kt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import org.ossreviewtoolkit.model.Package
2727
import org.ossreviewtoolkit.model.Project
2828
import org.ossreviewtoolkit.model.Provenance
2929
import org.ossreviewtoolkit.model.RepositoryProvenance
30+
import org.ossreviewtoolkit.model.SourceCodeOrigin
3031
import org.ossreviewtoolkit.model.UnknownProvenance
3132
import org.ossreviewtoolkit.model.VcsInfo
3233
import org.ossreviewtoolkit.model.VcsType
@@ -66,11 +67,19 @@ data class PackageConfiguration(
6667
* License finding curations.
6768
*/
6869
@JsonInclude(JsonInclude.Include.NON_EMPTY)
69-
val licenseFindingCurations: List<LicenseFindingCuration> = emptyList()
70+
val licenseFindingCurations: List<LicenseFindingCuration> = emptyList(),
71+
72+
/**
73+
* The source code origin this configuration applies to.
74+
*/
75+
@JsonInclude(JsonInclude.Include.NON_NULL)
76+
val sourceCodeOrigin: SourceCodeOrigin? = null
7077
) {
7178
init {
72-
require((sourceArtifactUrl == null) xor (vcs == null)) {
73-
"A package configuration must either set the 'sourceArtifactUrl' or the 'vcs' property."
79+
require(
80+
listOfNotNull(sourceArtifactUrl, vcs, sourceCodeOrigin).size == 1
81+
) {
82+
"A package configuration must set exactly one of 'sourceArtifactUrl', 'vcs' or 'sourceCodeOrigin'."
7483
}
7584
}
7685

@@ -84,6 +93,13 @@ data class PackageConfiguration(
8493
return false
8594
}
8695

96+
if (sourceCodeOrigin != null) {
97+
return when (sourceCodeOrigin) {
98+
SourceCodeOrigin.VCS -> provenance is RepositoryProvenance
99+
SourceCodeOrigin.ARTIFACT -> provenance is ArtifactProvenance
100+
}
101+
}
102+
87103
return when (provenance) {
88104
is UnknownProvenance -> false
89105
is ArtifactProvenance -> sourceArtifactUrl != null && sourceArtifactUrl == provenance.sourceArtifact.url

model/src/test/kotlin/config/PackageConfigurationTest.kt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919

2020
package org.ossreviewtoolkit.model.config
2121

22+
import io.kotest.assertions.throwables.shouldThrow
2223
import io.kotest.core.spec.style.WordSpec
2324
import io.kotest.matchers.shouldBe
2425

2526
import org.ossreviewtoolkit.model.ArtifactProvenance
2627
import org.ossreviewtoolkit.model.Identifier
2728
import org.ossreviewtoolkit.model.RemoteArtifact
2829
import org.ossreviewtoolkit.model.RepositoryProvenance
30+
import org.ossreviewtoolkit.model.SourceCodeOrigin
2931
import org.ossreviewtoolkit.model.VcsInfo
3032
import org.ossreviewtoolkit.model.VcsType
3133

@@ -155,5 +157,68 @@ class PackageConfigurationTest : WordSpec({
155157
)
156158
) shouldBe true
157159
}
160+
161+
"return true if vcs source code origin and identifier are equal" {
162+
val config =
163+
PackageConfiguration(
164+
id = Identifier.EMPTY.copy(name = "some-name"),
165+
sourceCodeOrigin = SourceCodeOrigin.VCS
166+
)
167+
168+
config.matches(
169+
config.id,
170+
RepositoryProvenance(
171+
vcsInfo = VcsInfo(
172+
type = VcsType.GIT,
173+
url = "ssh://git@host/repo.git",
174+
revision = ""
175+
),
176+
resolvedRevision = "12345678"
177+
)
178+
) shouldBe true
179+
}
180+
181+
"return true if artifact source code origin and identifier are equal" {
182+
val config =
183+
PackageConfiguration(
184+
id = Identifier.EMPTY.copy(name = "some-name"),
185+
sourceCodeOrigin = SourceCodeOrigin.ARTIFACT
186+
)
187+
188+
config.matches(
189+
config.id,
190+
ArtifactProvenance(
191+
sourceArtifact = RemoteArtifact.EMPTY.copy(
192+
url = "https://host/path/some-other-file.zip"
193+
)
194+
)
195+
) shouldBe true
196+
}
197+
198+
"return false if only source code origin is not equal" {
199+
val config =
200+
PackageConfiguration(
201+
id = Identifier.EMPTY.copy(name = "some-name"),
202+
sourceCodeOrigin = SourceCodeOrigin.ARTIFACT
203+
)
204+
205+
config.matches(
206+
config.id,
207+
RepositoryProvenance(
208+
vcsInfo = VcsInfo(
209+
type = VcsType.GIT,
210+
url = "ssh://git@host/repo.git",
211+
revision = ""
212+
),
213+
resolvedRevision = "12345678"
214+
)
215+
) shouldBe false
216+
}
217+
218+
"fail if the package configuration has only an identifier" {
219+
shouldThrow<IllegalArgumentException> {
220+
PackageConfiguration(id = Identifier.EMPTY.copy(name = "some-name"))
221+
}
222+
}
158223
}
159224
})

0 commit comments

Comments
 (0)