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

[IDEA] added option postProcess #672

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ new CycloneDxWebpackPlugin(options?: object)
| **`rootComponentType`** | `{string}` | `"application"` | Set the RootComponent's type.<br/>See [the list of valid values](https://cyclonedx.org/docs/1.4/json/#metadata_component_type). Supported values depend on [CycloneDX-javascript-library]'s enum `ComponentType`. |
| **`rootComponentName`** | optional `{string}` | `undefined` | If `rootComponentAutodetect` is disabled, then this value is assumed as the "name" of the `package.json`. |
| **`rootComponentVersion`** | optional `{string}` | `undefined` | If `rootComponentAutodetect` is disabled, then this value is assumed as the "version" of the `package.json`. |
| **`postProcess`** | optional `(bom: BOM)=>void` | `undefined` | If `postProcess` is given, bom will be passed to it and can be altered prior to serialization. |

### Example

Expand Down
16 changes: 15 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ export interface CycloneDxWebpackPluginOptions {
* @default undefined
*/
rootComponentVersion?: CycloneDxWebpackPlugin['rootComponentVersion']

/**
* If this function is given, bom will be passed to it and can be altered prior to serialization.
*
* @default undefined
*/
postProcess?: (bom: CDX.Models.Bom) => void
}

/** @public */
Expand All @@ -108,6 +115,7 @@ export class CycloneDxWebpackPlugin {
rootComponentType: CDX.Models.Component['type']
rootComponentName: CDX.Models.Component['name'] | undefined
rootComponentVersion: CDX.Models.Component['version'] | undefined
postProcess: ((bom: CDX.Models.Bom) => void) | undefined

constructor ({
specVersion = CDX.Spec.Version.v1dot4,
Expand All @@ -118,7 +126,8 @@ export class CycloneDxWebpackPlugin {
rootComponentAutodetect = true,
rootComponentType = CDX.Enums.ComponentType.Application,
rootComponentName = undefined,
rootComponentVersion = undefined
rootComponentVersion = undefined,
postProcess = undefined
}: CycloneDxWebpackPluginOptions = {}) {
this.specVersion = specVersion
this.reproducibleResults = reproducibleResults
Expand All @@ -131,6 +140,7 @@ export class CycloneDxWebpackPlugin {
this.rootComponentType = rootComponentType
this.rootComponentName = rootComponentName
this.rootComponentVersion = rootComponentVersion
this.postProcess = postProcess
}

apply (compiler: Compiler): void {
Expand Down Expand Up @@ -263,6 +273,10 @@ export class CycloneDxWebpackPlugin {
bom.metadata.component.purl = cdxPurlFactory.makeFromComponent(bom.metadata.component)
bom.metadata.component.bomRef.value = bom.metadata.component.purl?.toString()
}

if (typeof this.postProcess === 'function') {
this.postProcess(bom)
}
}

* #makeTools (builder: CDX.Builders.FromNodePackageJson.ToolBuilder): Generator<CDX.Models.Tool> {
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/__snapshots__/index.test.js.snap

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

Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,10 @@ module.exports = function (webpackEnv) {
}),
new CycloneDxWebpackPlugin({
outputLocation: '.bom',
reproducibleResults: true
reproducibleResults: true,
postProcess: (bom) => {
Copy link
Member

Choose a reason for hiding this comment

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

❌ do not change existing tests.
write your own test case !

bom.metadata.component.author = 'testing-post-process-option'
}
})
].filter(Boolean),
// Turn off performance processing because we utilize
Expand Down