Skip to content

Commit

Permalink
fix: error "Cannot read properties of undefined (reading 'url')" when…
Browse files Browse the repository at this point in the history
… mapping metadata.externalReferences (#115)

* fix: error "Cannot read properties of undefined (reading 'url')" when mapping metadata.externalReferences

* generated js

Co-authored-by: David J. M. Karlsen <[email protected]>
Co-authored-by: Kjetil Oen <[email protected]>
  • Loading branch information
3 people authored Oct 4, 2022
1 parent 97cc244 commit e9e55c8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
6 changes: 5 additions & 1 deletion __tests__/data/valid-bom-1.4.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"vendor": "Awesome Vendor",
"name": "Awesome Tool",
"version": "9.1.2",
"externalReferences" : [
{ "url" : "https://awesome.com", "type" : "example" }
],
"hashes": [
{
"alg": "SHA-1",
Expand Down Expand Up @@ -141,6 +144,7 @@
}
},
{
"bom-ref": "pkg:npm/acme/[email protected]",
"type": "library",
"supplier": {
"name": "Example, Inc.",
Expand Down Expand Up @@ -170,7 +174,7 @@
{
"ref": "pkg:npm/acme/[email protected]",
"dependsOn": [
"pkg:npm/acme/component@1.0.0"
"pkg:npm/acme/mylibrary@1.0.0"
]
}
]
Expand Down
14 changes: 12 additions & 2 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {RunOptions, RunTarget} from 'github-action-ts-run-api'
import {Bom, Component} from '@cyclonedx/cyclonedx-library/src/models'
import {Component} from '@cyclonedx/cyclonedx-library/src/models'
import {expect, test, afterEach, jest} from '@jest/globals'
import {map, parseSbomFile, process, run, SBom} from '../src/main'
import {map, parseSbomFile, run, SBom} from '../src/main'
import {Manifest, Snapshot} from '@github/dependency-submission-toolkit'

describe('Parse', () => {
Expand All @@ -28,6 +28,16 @@ describe('Map to GH dep submission', () => {
jest.resetModules()
})

test('should map external references to detector', () => {
const bomFile = '__tests__/data/valid-bom-1.4.json'
const bom: SBom = parseSbomFile(bomFile)
const snapshot: Snapshot = map(bom, bomFile)

expect(snapshot.detector.name).toBe('Awesome Tool')
expect(snapshot.detector.version).toBe('9.1.2')
expect(snapshot.detector.url).toBe('https://awesome.com')
})

test('testCycloneDXMavenDropwizardExample', () => {
const bomfile: string = '__tests__/data/dropwizard-1.3.15-sbom.json'
const bom: SBom = parseSbomFile(bomfile)
Expand Down
4 changes: 2 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.

4 changes: 2 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ function map(sbom, sbomFilename) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
//const bom: SBom = sbom as SBom
const detectors = Array.from(sbom.metadata.tools.values()).map(tool => {
var _a, _b, _c;
var _a, _b, _c, _d;
return {
name: (_a = tool.name) !== null && _a !== void 0 ? _a : 'unknown',
version: (_b = tool.version) !== null && _b !== void 0 ? _b : 'unknown',
url: ((_c = tool.externalReferences) === null || _c === void 0 ? void 0 : _c.values[0].url) || 'https://'
url: ((_d = (_c = tool.externalReferences) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.url) || 'https://'
};
});
const detector = (_a = detectors.pop()) !== null && _a !== void 0 ? _a : { name: '', url: '', version: '' };
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function map(sbom: SBom, sbomFilename?: string): Snapshot {
return {
name: tool.name ?? 'unknown',
version: tool.version ?? 'unknown',
url: tool.externalReferences?.values[0].url || 'https://'
url: tool.externalReferences?.[0]?.url || 'https://'
} as Detector
})
const detector = detectors.pop() ?? {name: '', url: '', version: ''}
Expand Down

0 comments on commit e9e55c8

Please sign in to comment.