Skip to content

Commit d05bcf1

Browse files
authored
Merge pull request #14 from lseppala/lsep/purl-qualifiers
Fix encoding of PURL qualifiers
2 parents 5a8ce4a + 5961fd4 commit d05bcf1

File tree

5 files changed

+58
-15
lines changed

5 files changed

+58
-15
lines changed

componentDetection.test.ts

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,53 @@
1-
import ComponentDetection from './componentDetection';
2-
import fs from 'fs';
1+
import ComponentDetection from "./componentDetection";
2+
import fs from "fs";
33

4-
test('Downloads CLI', async () => {
4+
test("Downloads CLI", async () => {
55
await ComponentDetection.downloadLatestRelease();
66
expect(fs.existsSync(ComponentDetection.componentDetectionPath));
77
});
88

9-
test('Runs CLI', async () => {
9+
test("Runs CLI", async () => {
1010
await ComponentDetection.downloadLatestRelease();
11-
await ComponentDetection.runComponentDetection('./test');
11+
await ComponentDetection.runComponentDetection("./test");
1212
expect(fs.existsSync(ComponentDetection.outputPath));
1313
});
1414

15-
test('Parses CLI output', async () => {
15+
test("Parses CLI output", async () => {
1616
await ComponentDetection.downloadLatestRelease();
17-
await ComponentDetection.runComponentDetection('./test');
17+
await ComponentDetection.runComponentDetection("./test");
1818
var manifests = await ComponentDetection.getManifestsFromResults();
1919
expect(manifests?.length == 2);
20-
});
20+
});
21+
22+
describe("ComponentDetection.makePackageUrl", () => {
23+
test("returns a valid package url from saturated object", () => {
24+
const packageUrl = ComponentDetection.makePackageUrl({
25+
Scheme: "pkg",
26+
Type: "npm",
27+
Namespace: "github",
28+
Name: "component-detection-action",
29+
Version: "0.0.2",
30+
Qualifiers: {
31+
arch: "amd64",
32+
os: "linux",
33+
},
34+
});
35+
expect(packageUrl).toBe(
36+
"pkg:npm/github/component-detection-action@0.0.2?arch=amd64&os=linux"
37+
);
38+
});
39+
40+
test("returns valid package url without dangling ? with empty qualifers", () => {
41+
const packageUrl = ComponentDetection.makePackageUrl({
42+
Scheme: "pkg",
43+
Type: "npm",
44+
Namespace: "github",
45+
Name: "component-detection-action",
46+
Version: "0.0.2",
47+
Qualifiers: { },
48+
});
49+
expect(packageUrl).toBe(
50+
"pkg:npm/github/component-detection-action@0.0.2"
51+
);
52+
});
53+
});

componentDetection.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export default class ComponentDetection {
119119
return pkg.isDevelopmentDependency ? 'development' : 'runtime'
120120
}
121121

122-
private static makePackageUrl(packageUrlJson: any): string {
122+
public static makePackageUrl(packageUrlJson: any): string {
123123
var packageUrl = `${packageUrlJson.Scheme}:${packageUrlJson.Type}/`;
124124
if (packageUrlJson.Namespace) {
125125
packageUrl += `${packageUrlJson.Namespace.replaceAll("@", "%40")}/`;
@@ -128,8 +128,13 @@ export default class ComponentDetection {
128128
if (packageUrlJson.Version) {
129129
packageUrl += `@${packageUrlJson.Version}`;
130130
}
131-
if (packageUrlJson.Qualifiers) {
132-
packageUrl += `?${packageUrlJson.Qualifiers}`;
131+
if (typeof packageUrlJson.Qualifiers === "object"
132+
&& packageUrlJson.Qualifiers !== null
133+
&& Object.keys(packageUrlJson.Qualifiers).length > 0) {
134+
const qualifierString = Object.entries(packageUrlJson.Qualifiers)
135+
.map(([key, value]) => `${key}=${value}`)
136+
.join("&");
137+
packageUrl += `?${qualifierString}`;
133138
}
134139
return packageUrl;
135140
}

dist/componentDetection.d.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)