Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter license when undefined to fix error in /discussions/1255 #1256

Merged
merged 8 commits into from
Jul 18, 2024
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,6 @@ roots/
.python-version
build/
.mise.toml

# Ignore IntelliJ IDEA module file
cdxgen.iml
3 changes: 2 additions & 1 deletion types/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export function isSpdxLicenseExpression(license: string): boolean;
* Convert the array of licenses to a CycloneDX 1.5 compliant license array.
* This should return an array containing:
* - one or more SPDX license if no expression is present
* - the first license expression if at least one is present
* - the license of the expression if one expression is present
* - a unified conditional 'OR' license expression if more then one expression is present
*
* @param {Array} licenses Array of licenses
* @returns {Array} CycloneDX 1.5 compliant license array
Expand Down
2 changes: 1 addition & 1 deletion types/utils.d.ts.map

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

70 changes: 36 additions & 34 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,43 +512,45 @@ export function getLicenses(pkg) {
license = [license];
}
return adjustLicenseInformation(
license.map((l) => {
let licenseContent = {};
if (typeof l === "string" || l instanceof String) {
if (
spdxLicenses.some((v) => {
return l === v;
})
) {
licenseContent.id = l;
licenseContent.url = `https://opensource.org/licenses/${l}`;
} else if (l.startsWith("http")) {
const knownLicense = getKnownLicense(l, pkg);
if (knownLicense) {
licenseContent.id = knownLicense.id;
licenseContent.name = knownLicense.name;
}
// We always need a name to avoid validation errors
// Issue: #469
if (!licenseContent.name && !licenseContent.id) {
licenseContent.name = "CUSTOM";
license
.filter((l) => l !== undefined)
Copy link
Contributor Author

@fitzmx6 fitzmx6 Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from formatting this was my only real change in this file

.map((l) => {
let licenseContent = {};
if (typeof l === "string" || l instanceof String) {
if (
spdxLicenses.some((v) => {
return l === v;
})
) {
licenseContent.id = l;
licenseContent.url = `https://opensource.org/licenses/${l}`;
} else if (l.startsWith("http")) {
const knownLicense = getKnownLicense(l, pkg);
if (knownLicense) {
licenseContent.id = knownLicense.id;
licenseContent.name = knownLicense.name;
}
// We always need a name to avoid validation errors
// Issue: #469
if (!licenseContent.name && !licenseContent.id) {
licenseContent.name = "CUSTOM";
}
licenseContent.url = l;
} else if (isSpdxLicenseExpression(l)) {
licenseContent.expression = l;
} else {
licenseContent.name = l;
}
licenseContent.url = l;
} else if (isSpdxLicenseExpression(l)) {
licenseContent.expression = l;
} else if (Object.keys(l).length) {
licenseContent = l;
} else {
licenseContent.name = l;
return undefined;
}
} else if (Object.keys(l).length) {
licenseContent = l;
} else {
return undefined;
}
if (!licenseContent.id) {
addLicenseText(pkg, l, licenseContent);
}
return licenseContent;
}),
if (!licenseContent.id) {
addLicenseText(pkg, l, licenseContent);
}
return licenseContent;
}),
);
}
const knownLicense = getKnownLicense(undefined, pkg);
Expand Down
63 changes: 38 additions & 25 deletions utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
findLicenseId,
getCratesMetadata,
getDartMetadata,
getGoPkgLicense,
getLicenses,
getMvnMetadata,
getNugetMetadata,
getPyMetadata,
getRepoLicense,
guessPypiMatchingVersion,
hasAnyProjectType,
isValidIriReference,
Expand Down Expand Up @@ -2246,76 +2248,82 @@ test("parsePomMetadata", async () => {
const data = await getMvnMetadata(deps);
expect(data.length).toEqual(deps.length);
});
/*

test("get repo license", async () => {
let license = await utils.getRepoLicense(
"https://github.com/ShiftLeftSecurity/sast-scan"
let license = await getRepoLicense(
"https://github.com/ShiftLeftSecurity/sast-scan",
{
group: "ShiftLeftSecurity",
name: "sast-scan",
},
);
expect(license).toEqual({
id: "GPL-3.0-or-later",
url: "https://github.com/ShiftLeftSecurity/sast-scan/blob/master/LICENSE"
id: "Apache-2.0",
url: "https://github.com/ShiftLeftSecurity/sast-scan/blob/master/LICENSE",
});

license = await utils.getRepoLicense("https://github.com/cyclonedx/cdxgen", {
group: "",
name: "cdxgen"
license = await getRepoLicense("https://github.com/cyclonedx/cdxgen", {
group: "cyclonedx",
name: "cdxgen",
});
expect(license).toEqual({
id: "Apache-2.0",
url: "https://github.com/cyclonedx/cdxgen/blob/master/LICENSE"
url: "https://github.com/CycloneDX/cdxgen/blob/master/LICENSE",
});

license = await utils.getRepoLicense("https://cloud.google.com/go", {
// These tests are disabled because they are returning undefined
/*
license = await getRepoLicense("https://cloud.google.com/go", {
group: "cloud.google.com",
name: "go"
});
expect(license).toEqual("Apache-2.0");

license = await utils.getRepoLicense(undefined, {
license = await getRepoLicense(undefined, {
group: "github.com/ugorji",
name: "go"
});
expect(license).toEqual({
id: "MIT",
url: "https://github.com/ugorji/go/blob/master/LICENSE"
});
*/
});

test("get go pkg license", async () => {
jest.setTimeout(120000);
let license = await utils.getGoPkgLicense({
let license = await getGoPkgLicense({
group: "github.com/Azure/azure-amqp-common-go",
name: "v2"
name: "v2",
});
expect(license).toEqual([
{
id: "MIT",
url: "https://pkg.go.dev/github.com/Azure/azure-amqp-common-go/v2?tab=licenses"
}
url: "https://pkg.go.dev/github.com/Azure/azure-amqp-common-go/v2?tab=licenses",
},
]);

license = await utils.getGoPkgLicense({
license = await getGoPkgLicense({
group: "go.opencensus.io",
name: "go.opencensus.io"
name: "go.opencensus.io",
});
expect(license).toEqual([
{
id: "Apache-2.0",
url: "https://pkg.go.dev/go.opencensus.io?tab=licenses"
}
url: "https://pkg.go.dev/go.opencensus.io?tab=licenses",
},
]);

license = await utils.getGoPkgLicense({
license = await getGoPkgLicense({
group: "github.com/DataDog",
name: "zstd"
name: "zstd",
});
expect(license).toEqual([
{
id: "BSD-3-Clause",
url: "https://pkg.go.dev/github.com/DataDog/zstd?tab=licenses"
}
url: "https://pkg.go.dev/github.com/DataDog/zstd?tab=licenses",
},
]);
});
*/

test("get licenses", () => {
let licenses = getLicenses({ license: "MIT" });
Expand Down Expand Up @@ -2398,6 +2406,11 @@ test("get licenses", () => {
expression: "GPL-3.0-only WITH Classpath-exception-2.0",
},
]);

licenses = getLicenses({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the test for my code change. The rest of the changes are me uncommenting some other tests and fixing them so they pass.

license: undefined,
});
expect(licenses).toEqual(undefined);
});

test("parsePkgJson", async () => {
Expand Down
Loading