Skip to content

Commit f68d737

Browse files
committed
fix(spdx): Do not hard-code "SpdxDocumentFile" as the package type
Only the project type should be "SpdxDocumentFile". The package type should be deduced from the PURL, if any, as `SpdxPackage` itself has no dedicated field for the package's type. This helps to maintain the package type e.g. if an SPDX document is used to describe Maven packages. Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
1 parent 4d3b016 commit f68d737

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

plugins/package-managers/spdx/src/funTest/assets/projects/synthetic/spdx-project-cyclic-expected-output.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ project:
2929
- id: "SpdxDocumentFile::xyz:0.1.0"
3030
- id: "SpdxDocumentFile::zlib:1.2.11"
3131
linkage: "STATIC"
32-
- id: "SpdxDocumentFile:OpenSSL Development Team:openssl:1.1.1g"
32+
- id: "a-name:OpenSSL Development Team:openssl:1.1.1g"
3333
- name: "test"
3434
dependencies:
3535
- id: "SpdxDocumentFile::curl:7.70.0"
@@ -160,7 +160,7 @@ packages:
160160
url: "<REPLACE_URL_PROCESSED>"
161161
revision: "<REPLACE_REVISION>"
162162
path: "plugins/package-managers/spdx/src/funTest/assets/projects/synthetic/libs/zlib"
163-
- id: "SpdxDocumentFile:OpenSSL Development Team:openssl:1.1.1g"
163+
- id: "a-name:OpenSSL Development Team:openssl:1.1.1g"
164164
purl: "pkg:a-name/openssl@1.1.1g"
165165
cpe: "cpe:2.3:a:a-name:openssl:1.1.1g:*:*:*:*:*:*:*"
166166
authors:

plugins/package-managers/spdx/src/funTest/assets/projects/synthetic/spdx-project-xyz-expected-output.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ project:
2424
dependencies:
2525
- id: "SpdxDocumentFile::zlib:1.2.11"
2626
linkage: "STATIC"
27-
- id: "SpdxDocumentFile:OpenSSL Development Team:openssl:1.1.1g"
27+
- id: "a-name:OpenSSL Development Team:openssl:1.1.1g"
2828
- name: "test"
2929
dependencies:
3030
- id: "SpdxDocumentFile::curl:7.70.0"
@@ -95,7 +95,7 @@ packages:
9595
url: "<REPLACE_URL_PROCESSED>"
9696
revision: "<REPLACE_REVISION>"
9797
path: "plugins/package-managers/spdx/src/funTest/assets/projects/synthetic/libs/zlib"
98-
- id: "SpdxDocumentFile:OpenSSL Development Team:openssl:1.1.1g"
98+
- id: "a-name:OpenSSL Development Team:openssl:1.1.1g"
9999
purl: "pkg:a-name/openssl@1.1.1g"
100100
cpe: "cpe:2.3:a:a-name:openssl:1.1.1g:*:*:*:*:*:*:*"
101101
authors:

plugins/package-managers/spdx/src/funTest/kotlin/SpdxDocumentFileFunTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class SpdxDocumentFileFunTest : WordSpec({
7777
val curlId = Identifier("SpdxDocumentFile::curl:7.70.0")
7878

7979
val opensslPackageFile = projectDir / "libs" / "openssl" / "package.spdx.yml"
80-
val opensslId = Identifier("SpdxDocumentFile:OpenSSL Development Team:openssl:1.1.1g")
80+
val opensslId = Identifier("a-name:OpenSSL Development Team:openssl:1.1.1g")
8181

8282
val zlibPackageFile = projectDir / "libs" / "zlib" / "package.spdx.yml"
8383
val zlibId = Identifier("SpdxDocumentFile::zlib:1.2.11")
@@ -165,7 +165,7 @@ class SpdxDocumentFileFunTest : WordSpec({
165165

166166
"retrieve transitive dependencies" {
167167
val idCurl = Identifier("SpdxDocumentFile::curl:7.70.0")
168-
val idOpenSsl = Identifier("SpdxDocumentFile:OpenSSL Development Team:openssl:1.1.1g")
168+
val idOpenSsl = Identifier("a-name:OpenSSL Development Team:openssl:1.1.1g")
169169
val idZlib = Identifier("SpdxDocumentFile::zlib:1.2.11")
170170
val idMyLib = Identifier("SpdxDocumentFile::my-lib:8.88.8")
171171

@@ -203,7 +203,7 @@ class SpdxDocumentFileFunTest : WordSpec({
203203

204204
"retrieve nested DEPENDS_ON dependencies" {
205205
val idCurl = Identifier("SpdxDocumentFile::curl:7.70.0")
206-
val idOpenSsl = Identifier("SpdxDocumentFile:OpenSSL Development Team:openssl:1.1.1g")
206+
val idOpenSsl = Identifier("a-name:OpenSSL Development Team:openssl:1.1.1g")
207207
val idZlib = Identifier("SpdxDocumentFile::zlib:1.2.11")
208208

209209
val projectFile = projectDir / "DEPENDS_ON-packages" / "project-xyz.spdx.yml"
@@ -309,7 +309,7 @@ class SpdxDocumentFileFunTest : WordSpec({
309309
packageIds should containExactlyInAnyOrder(
310310
Identifier("SpdxDocumentFile::curl:7.70.0"),
311311
Identifier("SpdxDocumentFile::my-lib:8.88.8"),
312-
Identifier("SpdxDocumentFile:OpenSSL Development Team:openssl:1.1.1g")
312+
Identifier("a-name:OpenSSL Development Team:openssl:1.1.1g")
313313
)
314314
}
315315

plugins/package-managers/spdx/src/main/kotlin/SpdxDocumentFile.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import org.ossreviewtoolkit.utils.spdxdocument.model.SpdxPackage
7070
import org.ossreviewtoolkit.utils.spdxdocument.model.SpdxRelationship
7171

7272
private const val PROJECT_TYPE = "SpdxDocumentFile"
73+
private const val PACKAGE_TYPE = "SpdxDocumentFile"
7374

7475
private const val DEFAULT_SCOPE_NAME = "default"
7576

@@ -279,9 +280,9 @@ class SpdxDocumentFile(override val descriptor: PluginDescriptor = SpdxDocumentF
279280
/**
280281
* Create an [Identifier] out of this [SpdxPackage].
281282
*/
282-
private fun SpdxPackage.toIdentifier() =
283+
private fun SpdxPackage.toIdentifier(type: String) =
283284
Identifier(
284-
type = projectType,
285+
type = type,
285286
namespace = listOfNotNull(supplier, originator).firstOrNull()
286287
?.withoutPrefix(SpdxConstants.ORGANIZATION).orEmpty().sanitize(),
287288
name = name.sanitize(),
@@ -306,10 +307,11 @@ class SpdxDocumentFile(override val descriptor: PluginDescriptor = SpdxDocumentF
306307
val isBinaryArtifact = generatedFromRelations.any { it.spdxElementId == spdxId }
307308
&& generatedFromRelations.none { it.relatedSpdxElement == spdxId }
308309

309-
val id = toIdentifier()
310-
val artifact = getRemoteArtifact()
310+
val purlReference = locateExternalReference(SpdxExternalReference.Type.Purl)
311+
val id = toIdentifier(purlReference?.toPackageUrl()?.type ?: PACKAGE_TYPE)
311312

312-
val purl = locateExternalReference(SpdxExternalReference.Type.Purl)
313+
val artifact = getRemoteArtifact()
314+
val purl = purlReference
313315
?: artifact
314316
?.let { if (it.hash.algorithm in HashAlgorithm.VERIFIABLE) it else it.copy(hash = Hash.NONE) }
315317
?.let { id.toPurl(ArtifactProvenance(it)) }
@@ -560,7 +562,7 @@ class SpdxDocumentFile(override val descriptor: PluginDescriptor = SpdxDocumentF
560562
)
561563

562564
val project = Project(
563-
id = projectPackage.toIdentifier(),
565+
id = projectPackage.toIdentifier(projectType),
564566
cpe = projectPackage.locateCpe(),
565567
definitionFilePath = VersionControlSystem.getPathInfo(definitionFile).path,
566568
authors = projectPackage.originator.wrapPresentInSet(),

0 commit comments

Comments
 (0)