-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(model): Add functions to en-/decode provenance into PURL extras
In order to identify the source code origin for the package a PURL points to, additional qualifiers are required that describe the provenance. Support creating such PURLs so that e.g. source artifact locations can also be encoded as PURLs. Signed-off-by: Sebastian Schuberth <[email protected]>
- Loading branch information
1 parent
490a641
commit 758fd7a
Showing
3 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (C) 2024 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.model.utils | ||
|
||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.shouldBe | ||
|
||
import org.ossreviewtoolkit.model.ArtifactProvenance | ||
import org.ossreviewtoolkit.model.Hash | ||
import org.ossreviewtoolkit.model.HashAlgorithm | ||
import org.ossreviewtoolkit.model.Identifier | ||
import org.ossreviewtoolkit.model.RemoteArtifact | ||
import org.ossreviewtoolkit.model.RepositoryProvenance | ||
import org.ossreviewtoolkit.model.UnknownProvenance | ||
import org.ossreviewtoolkit.model.VcsInfo | ||
import org.ossreviewtoolkit.model.VcsType | ||
|
||
class PurlExtensionsTest : StringSpec({ | ||
"Artifact provenance can be converted to PURL extras and back" { | ||
val provenance = ArtifactProvenance( | ||
sourceArtifact = RemoteArtifact( | ||
url = "http://example.com/sources.zip", | ||
hash = Hash( | ||
value = "ddce269a1e3d054cae349621c198dd52", | ||
algorithm = HashAlgorithm.MD5 | ||
) | ||
) | ||
) | ||
val id = Identifier("Maven:com.example:sources:1.2.3") | ||
|
||
val extras = provenance.toPurlExtras() | ||
val purl = id.toPurl(extras.qualifiers, extras.subpath) | ||
|
||
purl shouldBe "pkg:maven/com.example/[email protected]?" + | ||
"download_url=http%3A%2F%2Fexample.com%2Fsources.zip&" + | ||
"checksum=md5%3Addce269a1e3d054cae349621c198dd52" | ||
purl.toProvenance() shouldBe provenance | ||
} | ||
|
||
"Repository provenance can be converted to PURL extras and back" { | ||
val provenance = RepositoryProvenance( | ||
vcsInfo = VcsInfo( | ||
type = VcsType.GIT, | ||
url = "https://github.com/apache/commons-text.git", | ||
revision = "7643b12421100d29fd2b78053e77bcb04a251b2e" | ||
), | ||
resolvedRevision = "7643b12421100d29fd2b78053e77bcb04a251b2e" | ||
) | ||
val id = Identifier("Maven:com.example:sources:1.2.3") | ||
|
||
val extras = provenance.toPurlExtras() | ||
val purl = id.toPurl(extras.qualifiers, extras.subpath) | ||
|
||
purl shouldBe "pkg:maven/com.example/[email protected]?" + | ||
"vcs_type=Git&" + | ||
"vcs_url=https%3A%2F%2Fgithub.com%2Fapache%2Fcommons-text.git&" + | ||
"vcs_revision=7643b12421100d29fd2b78053e77bcb04a251b2e&" + | ||
"resolved_revision=7643b12421100d29fd2b78053e77bcb04a251b2e" | ||
purl.toProvenance() shouldBe provenance | ||
} | ||
|
||
"A clean PURL has unknown provenance" { | ||
"pkg:npm/[email protected]".toProvenance() shouldBe UnknownProvenance | ||
} | ||
}) |