Skip to content

Commit

Permalink
Merge pull request #14 from lseppala/lsep/purl-qualifiers
Browse files Browse the repository at this point in the history
Fix encoding of PURL qualifiers
  • Loading branch information
lseppala authored Jun 2, 2023
2 parents 5a8ce4a + 5961fd4 commit d05bcf1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
49 changes: 41 additions & 8 deletions componentDetection.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
import ComponentDetection from './componentDetection';
import fs from 'fs';
import ComponentDetection from "./componentDetection";
import fs from "fs";

test('Downloads CLI', async () => {
test("Downloads CLI", async () => {
await ComponentDetection.downloadLatestRelease();
expect(fs.existsSync(ComponentDetection.componentDetectionPath));
});

test('Runs CLI', async () => {
test("Runs CLI", async () => {
await ComponentDetection.downloadLatestRelease();
await ComponentDetection.runComponentDetection('./test');
await ComponentDetection.runComponentDetection("./test");
expect(fs.existsSync(ComponentDetection.outputPath));
});

test('Parses CLI output', async () => {
test("Parses CLI output", async () => {
await ComponentDetection.downloadLatestRelease();
await ComponentDetection.runComponentDetection('./test');
await ComponentDetection.runComponentDetection("./test");
var manifests = await ComponentDetection.getManifestsFromResults();
expect(manifests?.length == 2);
});
});

describe("ComponentDetection.makePackageUrl", () => {
test("returns a valid package url from saturated object", () => {
const packageUrl = ComponentDetection.makePackageUrl({
Scheme: "pkg",
Type: "npm",
Namespace: "github",
Name: "component-detection-action",
Version: "0.0.2",
Qualifiers: {
arch: "amd64",
os: "linux",
},
});
expect(packageUrl).toBe(
"pkg:npm/github/[email protected]?arch=amd64&os=linux"
);
});

test("returns valid package url without dangling ? with empty qualifers", () => {
const packageUrl = ComponentDetection.makePackageUrl({
Scheme: "pkg",
Type: "npm",
Namespace: "github",
Name: "component-detection-action",
Version: "0.0.2",
Qualifiers: { },
});
expect(packageUrl).toBe(
"pkg:npm/github/[email protected]"
);
});
});
11 changes: 8 additions & 3 deletions componentDetection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default class ComponentDetection {
return pkg.isDevelopmentDependency ? 'development' : 'runtime'
}

private static makePackageUrl(packageUrlJson: any): string {
public static makePackageUrl(packageUrlJson: any): string {
var packageUrl = `${packageUrlJson.Scheme}:${packageUrlJson.Type}/`;
if (packageUrlJson.Namespace) {
packageUrl += `${packageUrlJson.Namespace.replaceAll("@", "%40")}/`;
Expand All @@ -128,8 +128,13 @@ export default class ComponentDetection {
if (packageUrlJson.Version) {
packageUrl += `@${packageUrlJson.Version}`;
}
if (packageUrlJson.Qualifiers) {
packageUrl += `?${packageUrlJson.Qualifiers}`;
if (typeof packageUrlJson.Qualifiers === "object"
&& packageUrlJson.Qualifiers !== null
&& Object.keys(packageUrlJson.Qualifiers).length > 0) {
const qualifierString = Object.entries(packageUrlJson.Qualifiers)
.map(([key, value]) => `${key}=${value}`)
.join("&");
packageUrl += `?${qualifierString}`;
}
return packageUrl;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/componentDetection.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

0 comments on commit d05bcf1

Please sign in to comment.