-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
feat: add copyright to evidence collection #1338
Draft
Frozen-byte
wants to merge
1
commit into
CycloneDX:master
Choose a base branch
from
Frozen-byte:with_copyright_evidences
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -111,6 +111,9 @@ export class Extractor { | |
component.evidence = new CDX.Models.ComponentEvidence({ | ||
licenses: new CDX.Models.LicenseRepository(this.getLicenseEvidence(dirname(pkg.path), logger)) | ||
}) | ||
for (const line of this.getCopyrightEvidence(dirname(pkg.path), logger)) { | ||
component.evidence.copyright.add(line) | ||
} | ||
} | ||
|
||
component.purl = this.#purlFactory.makeFromComponent(component) | ||
|
@@ -130,6 +133,46 @@ export class Extractor { | |
} | ||
} | ||
|
||
readonly #COPYRIGHT_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$|^COPYRIGHTNOTICE$/i | ||
|
||
public * getCopyrightEvidence (packageDir: string, logger?: WebpackLogger): Generator<string> { | ||
let pcis | ||
try { | ||
pcis = readdirSync(packageDir, {withFileTypes: true}) | ||
} catch (e) { | ||
logger?.warn('collecting license evidence in', packageDir, 'failed:', e) | ||
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. "license evidence"? Probably a copy/paste error |
||
return | ||
} | ||
for (const pci of pcis) { | ||
if ( | ||
!pci.isFile() || | ||
!this.#COPYRIGHT_FILENAME_PATTERN.test(pci.name) | ||
) { | ||
continue | ||
} | ||
const fp = join(packageDir, pci.name) | ||
try { | ||
// Add copyright evidence | ||
const linesStartingWithCopyright = readFileSync(fp).toString('utf-8') | ||
.split(/\r\n?|\n/) | ||
.map(line => line.trimStart()) | ||
.filter(trimmedLine => { | ||
return trimmedLine.startsWith('opyright', 1) && // include copyright statements | ||
!trimmedLine.startsWith('opyright notice', 1) && // exclude lines from license text | ||
!trimmedLine.startsWith('opyright and related rights', 1) && | ||
!trimmedLine.startsWith('opyright license to reproduce', 1) | ||
}) | ||
.filter((value, index, list) => index === 0 || value !== list[0]) // remove duplicates | ||
|
||
for (const line of linesStartingWithCopyright) { | ||
yield line | ||
} | ||
} catch (e) { // may throw if `readFileSync()` fails | ||
logger?.warn('collecting copyright evidences from', fp, 'failed:', e) | ||
} | ||
} | ||
} | ||
|
||
readonly #LICENSE_FILENAME_PATTERN = /^(?:UN)?LICEN[CS]E|.\.LICEN[CS]E$|^NOTICE$/i | ||
|
||
public * getLicenseEvidence (packageDir: string, logger?: WebpackLogger): Generator<CDX.Models.License> { | ||
|
@@ -150,6 +193,7 @@ export class Extractor { | |
|
||
const contentType = getMimeForTextFile(pci.name) | ||
if (contentType === undefined) { | ||
logger?.warn(`could not determine content-type for ${pci.name}`) | ||
continue | ||
} | ||
|
||
|
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.
I'd like use the same Method as lin 112, but sadly the Stringable is not exported via CDX.Models and the only way I found to create that Set is with the Component Evidence constructor. Maybe someone else have a more elegant way to iterate.
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.
why would you need the
Stringable
being exported?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.
Basically I think its more elegant to use something like the following:
The
SortableStringables
is nowhere exported via the CDX object. Importing directly from'_helpers/sortable'
will not work since its not exported in any barrel.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.
I see.
If you feel that it is a good idea to export the
SortableStringables
, then feel free to write an issue for it in the library's repository: https://github.com/CycloneDX/cyclonedx-javascript-libraryThere 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.
How about adding and exporting a dedicated type for the purpose
new CDX.Models.CopyrightRepository
?The thing is, that the
SortableStringables
is internal for a good reason. it is just no stable/public interface, yet.see CycloneDX/cyclonedx-javascript-library#1192
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.
@Frozen-byte with version 7.1.0 of the JS library, the needed model was introduced.
see https://github.com/CycloneDX/cyclonedx-javascript-library/releases/tag/v7.1.0
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.
this dependency is intended to be bumped to
^7.0
via #1331please remember to bump to
^7.1
with this PR 👍