Skip to content

Commit e85e477

Browse files
committed
PackageUrlFactory::makeFromComponent() checksums and download_url
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 50a6694 commit e85e477

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

Diff for: HISTORY.md

+4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ All notable changes to this project will be documented in this file.
1414
* `Serialize.JsonSerializer.normalizerFactory`
1515
* `Serialize.XmlBaseSerializer.normalizerFactory`,
1616
`Serialize.XmlSerializer.normalizerFactory`
17+
* Factory for `PackageURL` from `Models.Component` can handle additional data sources. (via [#146])
18+
* `Models.Component.hashes` map -> `PackageURL.qualifiers.checksum` list
19+
* `Models.Component.externalReferences[distribution].url` -> `PackageURL.qualifiers.download_url`
1720

1821
[#145]: https://github.com/CycloneDX/cyclonedx-javascript-library/pull/145
22+
[#146]: https://github.com/CycloneDX/cyclonedx-javascript-library/pull/146
1923

2024
## 1.1.0 - 2022-07-29
2125

Diff for: src/factories/packageUrl.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,37 @@ export class PackageUrlFactory {
3232
return this.#type
3333
}
3434

35-
makeFromComponent (component: Component): PackageURL | undefined {
35+
makeFromComponent (component: Component, sort: boolean = false): PackageURL | undefined {
36+
/**
37+
* For the list/spec of the well-known keys, see
38+
* {@link https://github.com/package-url/purl-spec/blob/master/PURL-SPECIFICATION.rst#known-qualifiers-keyvalue-pairs}
39+
*/
3640
const qualifiers: PackageURL['qualifiers'] = {}
3741
let subpath: PackageURL['subpath']
3842

39-
for (const e of component.externalReferences) {
40-
if (e.type === ExternalReferenceType.VCS) {
41-
[qualifiers.vcs_url, subpath] = e.url.toString().split('#', 2)
42-
break
43+
const extRefs = component.externalReferences
44+
for (const extRef of (sort ? extRefs.sorted() : extRefs)) {
45+
switch (extRef.type) {
46+
case ExternalReferenceType.VCS:
47+
[qualifiers.vcs_url, subpath] = extRef.url.toString().split('#', 2)
48+
break
49+
case ExternalReferenceType.Distribution:
50+
qualifiers.download_url = extRef.url.toString()
51+
break
4352
}
4453
}
4554

55+
const hashes = component.hashes
56+
if (hashes.size > 0) {
57+
qualifiers.checksum = Array.from(
58+
sort ? hashes.sorted() : hashes,
59+
([hashAlgo, hashCont]) => `${hashAlgo.toLowerCase()}:${hashCont.toLowerCase()}`
60+
).join(',')
61+
}
62+
4663
try {
64+
// Do not beautify the parameters here, because that is in the domain of PackageURL and its representation.
65+
// No need to convert an empty `subpath` string to `undefined` and such.
4766
return new PackageURL(
4867
this.#type,
4968
component.group,

Diff for: tests/integration/Factories.PackageUrlFactory.test.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ suite('Factories.PackageUrlFactory', () => {
6262
assert.deepStrictEqual(actual, expected)
6363
})
6464

65-
test('vcs-url without subpath', () => {
65+
test('extRef[vcs] -> qualifiers.vcs-url without subpath', () => {
6666
const component = new Models.Component(
6767
Enums.ComponentType.Library,
6868
`name-${salt}`,
@@ -77,7 +77,7 @@ suite('Factories.PackageUrlFactory', () => {
7777
assert.deepStrictEqual(actual, expected)
7878
})
7979

80-
test('vcs-url with subpath', () => {
80+
test('extRef[vcs] -> qualifiers.vcs-url with subpath', () => {
8181
const component = new Models.Component(
8282
Enums.ComponentType.Library,
8383
`name-${salt}`,
@@ -91,5 +91,19 @@ suite('Factories.PackageUrlFactory', () => {
9191
const actual = sut.makeFromComponent(component)
9292
assert.deepStrictEqual(actual, expected)
9393
})
94+
95+
test('extRef[distribution] -> qualifiers.download_url', () => {
96+
assert.strictEqual(false, true, 'TODO')
97+
})
98+
99+
test('hashes -> qualifiers.checksum', () => {
100+
assert.strictEqual(false, true, 'TODO')
101+
})
102+
103+
test('sorted', () => {
104+
assert.strictEqual(false, true, 'TODO')
105+
})
94106
})
107+
108+
95109
})

0 commit comments

Comments
 (0)