Skip to content

Commit e339e62

Browse files
committed
Update findings, add getFindingsData to return
all findings info in the findings array
1 parent b4d2968 commit e339e62

File tree

3 files changed

+102
-4
lines changed

3 files changed

+102
-4
lines changed

src/Findings.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,16 @@ import {
4242
MdBlock,
4343
MdDoc,
4444
sortBlocks,
45-
iterateBlocks
45+
iterateBlocks,
46+
mdBlockToMd,
47+
mdDocToMd
4648
} from './mdModel'
4749
import { flipObject, camelCaseToText, toCamelCase } from './utils'
4850

51+
export const FINDING_SECTIONS_PROPS = FINDING_SECTIONS.map((x) =>
52+
x.toLowerCase()
53+
)
54+
4955
export type FindingMetadata = {
5056
id?: string
5157
impact: string
@@ -270,12 +276,38 @@ export const reindexFindings = (doc: MdDoc): MdDoc => {
270276
return doc
271277
}
272278

273-
export const getFindings = (doc: MdDoc) => {
279+
const getFindingsArray = (doc: MdDoc) => {
274280
const findings: any[] = []
275281
iterateFindings(doc, (f: any) => findings.push(f))
282+
return findings
283+
}
284+
285+
export const getFindings = (doc: MdDoc) => {
286+
const findings = getFindingsArray(doc)
276287
return findings.map(({ metadata }) => metadata)
277288
}
278289

290+
export const getFindingsData = (doc: MdDoc) => {
291+
const findings = getFindingsArray(doc)
292+
return findings.map(({ metadata, children }) => {
293+
const data = FINDING_SECTIONS_PROPS.reduce(
294+
(v: { [key: string]: any }, a) => {
295+
v[a] = ''
296+
const ch = children.filter(
297+
(c: any) => a === c.metadata.title.toLowerCase()
298+
)
299+
if (ch.length) {
300+
ch[0].metadata.hideMdTitle = true
301+
v[a] = mdDocToMd(ch)
302+
}
303+
return v
304+
},
305+
{}
306+
)
307+
return { metadata, ...data }
308+
})
309+
}
310+
279311
export const findingListFieds = [ID, TITLE, TOTAL_RISK, FIXED]
280312

281313
export const FINDING_LIST_TITLES = findingFields.reduce(

src/mdModel.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ export const mdBlockToMd = (block: MdBlock): string => {
6868
if (isHeadingType(blockType)) {
6969
const markup = '#'.repeat(parseInt(blockType.replace('h', '')))
7070
const title = metadata.title || ''
71-
resultMd = [`${markup} ${title}`, '', ...resultMd]
71+
resultMd = !metadata.hideMdTitle
72+
? [`${markup} ${title}`, '', ...resultMd]
73+
: resultMd
7274
} else {
7375
delete metadata.type
7476
resultMd = [

src/test/findings.test.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import {
1515
FINDING_MODEL,
1616
sortFindingFields,
1717
isAllowedInfoImpact,
18-
createFindigsExampleMetadata
18+
createFindigsExampleMetadata,
19+
getFindingsData,
20+
FINDING_SECTIONS_PROPS
1921
} from '../Findings'
2022
import {
2123
ALLOWED_INFO_IMPACT,
@@ -442,4 +444,66 @@ describe('findings', () => {
442444
expect(findings.length).toBe(total)
443445
})
444446
})
447+
448+
const testFindingData = (finding: any) => {
449+
for (const s of ['metadata', ...FINDING_SECTIONS_PROPS]) {
450+
it(`should have a ${s} proerty`, () => {
451+
expect(finding.hasOwnProperty(s)).toBe(true)
452+
})
453+
}
454+
}
455+
456+
describe.only('getFindingsData', () => {
457+
describe('test finding', () => {
458+
const description = 'This is a test..'
459+
const recommendation = 'This is a recomendation example'
460+
const status = 'Finding STATUS'
461+
const fmd = `# TEST
462+
--- finding
463+
464+
id: xxx-001
465+
title: Untitled finding
466+
likelihood: low
467+
impact: low
468+
risk: low
469+
resolution: open
470+
status: ⚠
471+
location: test
472+
fixed: false
473+
474+
#### Description
475+
476+
${description}
477+
478+
#### Recommendation
479+
480+
${recommendation}
481+
482+
#### Status
483+
484+
${status}
485+
486+
/--
487+
`
488+
489+
const props: { [key: string]: any } = {
490+
description,
491+
recommendation,
492+
status
493+
}
494+
const data: { [key: string]: any } = getFindingsData(MdToObj()(fmd))[0]
495+
for (const p in props) {
496+
it('should have the section content', () => {
497+
expect(data[p]).toContain(props[p])
498+
})
499+
}
500+
})
501+
502+
describe('example test', () => {
503+
const findings = getFindingsData(MdToObj()(example))
504+
for (const finding of findings) {
505+
testFindingData(finding)
506+
}
507+
})
508+
})
445509
})

0 commit comments

Comments
 (0)