-
-
Notifications
You must be signed in to change notification settings - Fork 165
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
Follow CycloneDX 1.5 spec for SPDX license expressions #975
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
553c94f
Reuse same object on retry.
validide 9f84449
Merge branch 'CycloneDX:master' into master
validide 58170a1
Merge branch 'CycloneDX:master' into master
validide 9c47c1d
Add license expression handling.
validide 002e541
Refactoring
validide c7907fa
Forgot to save,
validide 9cbb828
Improved log message.
validide c4ef4cb
Fixed failing tests.
validide a229a7c
Small fixes.
validide File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ tmp/ | |
|
||
# BOM generated by CDXGEN during local development | ||
bom.json | ||
bomresults | ||
|
||
# Logs | ||
logs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -274,6 +274,71 @@ function toBase64(hexString) { | |
return Buffer.from(hexString, "hex").toString("base64"); | ||
} | ||
|
||
/** | ||
* Return the current timestamp in YYYY-MM-DDTHH:MM:SSZ format. | ||
* | ||
* @returns {string} ISO formatted timestamp, without milliseconds. | ||
*/ | ||
export function getTimestamp() { | ||
return new Date().toISOString().split(".")[0] + "Z"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the record, I think jsonschema date-time is wrong to leave out millseconds and move away from ISO8601 format. |
||
} | ||
|
||
/** | ||
* Method to determine if a license is a valid SPDX license expression | ||
* | ||
* @param {string} license License string | ||
* @returns {boolean} true if the license is a valid SPDX license expression | ||
* @see https://spdx.dev/learn/handling-license-info/ | ||
**/ | ||
export function isSpdxLicenseExpression(license) { | ||
if (!license) { | ||
return false; | ||
} | ||
|
||
if (/[(\s]+/g.test(license)) { | ||
return true; | ||
} | ||
|
||
if (license.endsWith("+")) { | ||
return true; // GPL-2.0+ means GPL-2.0 or any later version, at the licensee’s option. | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* 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 | ||
* | ||
* @param {Array} licenses Array of licenses | ||
* @returns {Array} CycloneDX 1.5 compliant license array | ||
*/ | ||
export function adjustLicenseInformation(licenses) { | ||
if (!licenses || !Array.isArray(licenses)) { | ||
return []; | ||
} | ||
|
||
const expressions = licenses.filter((f) => { | ||
return f.expression; | ||
}); | ||
if (expressions.length >= 1) { | ||
if (expressions.length > 1) { | ||
console.warn("multiple license expressions found", expressions); | ||
} | ||
return [{ expression: expressions[0].expression }]; | ||
} else { | ||
return licenses.map((l) => { | ||
if (typeof l.license === "object") { | ||
return l; | ||
} else { | ||
return { license: l }; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Performs a lookup + validation of the license specified in the | ||
* package. If the license is a valid SPDX license ID, set the 'id' | ||
|
@@ -286,8 +351,8 @@ export function getLicenses(pkg) { | |
if (!Array.isArray(license)) { | ||
license = [license]; | ||
} | ||
return license | ||
.map((l) => { | ||
return adjustLicenseInformation( | ||
license.map((l) => { | ||
let licenseContent = {}; | ||
if (typeof l === "string" || l instanceof String) { | ||
if ( | ||
|
@@ -309,6 +374,8 @@ export function getLicenses(pkg) { | |
licenseContent.name = "CUSTOM"; | ||
} | ||
licenseContent.url = l; | ||
} else if (isSpdxLicenseExpression(l)) { | ||
licenseContent.expression = l; | ||
} else { | ||
licenseContent.name = l; | ||
} | ||
|
@@ -322,7 +389,7 @@ export function getLicenses(pkg) { | |
} | ||
return licenseContent; | ||
}) | ||
.map((l) => ({ license: l })); | ||
); | ||
} else { | ||
const knownLicense = getKnownLicense(undefined, pkg); | ||
if (knownLicense) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!