Skip to content

Commit 9327862

Browse files
committed
fix errors
1 parent 1ffa410 commit 9327862

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

dist/index.js

Lines changed: 22 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,25 @@ function Capitalize(str: string): string {
1919
return modStr
2020
}
2121

22+
function assert(expr: unknown, msg?: string): asserts expr {
23+
if (!expr) throw new Error(msg)
24+
}
25+
2226
type ODictByString = {
2327
[key: string]: object
2428
}
2529

2630
export class RlJsonReportProcessor {
31+
debug: boolean
2732
filename: string
2833
data: ODictByString
2934

3035
name: string
3136
purl: string
3237

33-
assessments: ODictByString // report/metadata.assessments
38+
metadata: ODictByString // report.metadata
39+
40+
assessments: ODictByString // report.metadata.assessments
3441
violations: ODictByString // report.metadata.violations
3542
components: ODictByString // report.metadata.components
3643
vulnerabilities: ODictByString // report.metadata.vulnerabilities
@@ -39,26 +46,31 @@ export class RlJsonReportProcessor {
3946
indent: string = ' '
4047
out: string[]
4148

42-
constructor(filename: string) {
43-
this.filename = filename
49+
constructor(filename: string, debug: boolean = false) {
4450
this.viols = []
4551
this.out = []
52+
this.debug = debug
53+
54+
this.filename = filename
4655
this.data = JSON.parse(fs.readFileSync(this.filename, 'utf-8'))
4756

4857
this.name = this.jpath2string(this.data, 'report.info.file.name') || '<no name>'
4958
this.purl = this.jpath2string(this.data, 'report.info.file.identity.purl') || '<no purl>'
50-
51-
this.assessments = this.jpath2dict(this.data, 'report.metadata.assessments')
52-
this.violations = this.jpath2dict(this.data, 'report.metadata.violations')
53-
this.components = this.jpath2dict(this.data, 'report.metadata.components')
54-
this.vulnerabilities = this.jpath2dict(this.data, 'report.metadata.vulnerabilities')
59+
console.log(`# filePath: ${this.filename} purl: ${this.purl}`)
60+
61+
this.metadata = this.jpath2dict(this.data, 'report.metadata')
62+
this.assessments = this.jpath2dict(this.metadata, 'assessments')
63+
assert(this.assessments, 'has no data')
64+
this.violations = this.jpath2dict(this.metadata, 'violations')
65+
this.components = this.jpath2dict(this.metadata, 'components')
66+
this.vulnerabilities = this.jpath2dict(this.metadata, 'vulnerabilities')
5567
}
5668

5769
jpath2string(data: ODictByString, path_str: string): string {
5870
const path_list: string[] = path_str.split('.')
5971
let z: ODictByString = data
6072
for (const item of path_list) {
61-
z = data[item] as ODictByString // the last item is actually a string
73+
z = z[item] as ODictByString // the last item is actually a string
6274
}
6375
const u = z as unknown
6476
return u as string
@@ -68,7 +80,7 @@ export class RlJsonReportProcessor {
6880
const path_list: string[] = path_str.split('.')
6981
let z: ODictByString = data
7082
for (const item of path_list) {
71-
z = data[item] as ODictByString // the last item is actually a string
83+
z = z[item] as ODictByString // the last item is actually a string[]
7284
}
7385
const u = z as unknown
7486
return u as string[]
@@ -78,17 +90,18 @@ export class RlJsonReportProcessor {
7890
const path_list: string[] = path_str.split('.')
7991
let z: ODictByString = data
8092
for (const item of path_list) {
81-
z = data[item] as ODictByString // the last item is actually a string
93+
z = z[item] as ODictByString // the last item is actually a number
8294
}
8395
const u = z as unknown
8496
return u as number
8597
}
8698

8799
jpath2dict(data: ODictByString, path_str: string): ODictByString {
88100
const path_list: string[] = path_str.split('.')
101+
89102
let z: ODictByString = data
90103
for (const item of path_list) {
91-
z = data[item] as ODictByString // the last item is actually a string
104+
z = z[item] as ODictByString
92105
}
93106
return z
94107
}
@@ -185,7 +198,7 @@ export class RlJsonReportProcessor {
185198
const lines: string[] = []
186199

187200
const url: string = `https://www.cve.org/CVERecord?id=${cve}`
188-
const baseScore = this.jpath2number(this.vulnerabilities, 'cve.cvss.baseScore')
201+
const baseScore = this.jpath2number(this.vulnerabilities, `${cve}.cvss.baseScore`)
189202
let severity: string = this.cveSeverity(baseScore)
190203
severity = this.colorSeverity(severity)
191204

0 commit comments

Comments
 (0)