-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: CVE List - related documents column (#243)
- Loading branch information
1 parent
8a4eee4
commit 8257938
Showing
13 changed files
with
205 additions
and
149 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
client/src/app/hooks/domain-controls/useSbomsOfVulnerability.ts
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 |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import React from "react"; | ||
|
||
import { VulnerabilityStatus } from "@app/api/models"; | ||
import { client } from "@app/axios-config/apiInit"; | ||
import { | ||
getSbom, | ||
SbomSummary, | ||
VulnerabilityAdvisorySummary, | ||
} from "@app/client"; | ||
import { useFetchVulnerabilityById } from "@app/queries/vulnerabilities"; | ||
|
||
interface SbomOfVulnerability { | ||
sbomId: string; | ||
advisory: VulnerabilityAdvisorySummary; | ||
status: VulnerabilityStatus; | ||
sbom?: SbomSummary; | ||
} | ||
|
||
interface SbomOfVulnerabilitySummary { | ||
total: number; | ||
status: { [key in VulnerabilityStatus]: number }; | ||
} | ||
|
||
const DEFAULT_SUMMARY: SbomOfVulnerabilitySummary = { | ||
total: 0, | ||
status: { affected: 0, fixed: 0, known_not_affected: 0, not_affected: 0 }, | ||
}; | ||
|
||
export const useSbomsOfVulnerability = (sbomId: string) => { | ||
const { | ||
vulnerability, | ||
isFetching: isFetchingAdvisories, | ||
fetchError: fetchErrorAdvisories, | ||
} = useFetchVulnerabilityById(sbomId); | ||
|
||
const [allSboms, setAllSboms] = React.useState<SbomOfVulnerability[]>([]); | ||
const [sbomsById, setSbomsById] = React.useState<Map<string, SbomSummary>>( | ||
new Map() | ||
); | ||
const [isFetchingSboms, setIsFetchingSboms] = React.useState(false); | ||
|
||
React.useEffect(() => { | ||
if (vulnerability?.advisories.length === 0) { | ||
return; | ||
} | ||
|
||
const sboms = (vulnerability?.advisories ?? []) | ||
.flatMap((advisory) => { | ||
return (advisory.sboms ?? []).flatMap((sbom) => { | ||
return sbom.status.map((status) => { | ||
const result: SbomOfVulnerability = { | ||
sbomId: sbom.id, | ||
status: status as VulnerabilityStatus, | ||
advisory: { ...advisory }, | ||
}; | ||
return result; | ||
}); | ||
}); | ||
}) | ||
// Remove duplicates if exists | ||
.reduce((prev, current) => { | ||
const exists = prev.find( | ||
(item) => | ||
item.sbomId === current.sbomId && | ||
item.advisory.uuid === current.advisory.uuid | ||
); | ||
if (!exists) { | ||
return [...prev, current]; | ||
} else { | ||
return prev; | ||
} | ||
}, [] as SbomOfVulnerability[]); | ||
|
||
setAllSboms(sboms); | ||
setIsFetchingSboms(true); | ||
|
||
Promise.all( | ||
sboms | ||
.map(async (item) => { | ||
const response = await getSbom({ | ||
client, | ||
path: { id: item.sbomId }, | ||
}); | ||
return response.data; | ||
}) | ||
.map((sbom) => sbom.catch(() => null)) | ||
).then((sboms) => { | ||
const validSboms = sboms.reduce((prev, current) => { | ||
if (current) { | ||
return [...prev, current]; | ||
} else { | ||
// Filter out error responses | ||
return prev; | ||
} | ||
}, [] as SbomSummary[]); | ||
|
||
const sbomsById = new Map<string, SbomSummary>(); | ||
validSboms.forEach((sbom) => sbomsById.set(sbom.id, sbom)); | ||
|
||
setSbomsById(sbomsById); | ||
setIsFetchingSboms(false); | ||
}); | ||
}, [vulnerability]); | ||
|
||
const allSbomsWithMappedData = React.useMemo(() => { | ||
return allSboms.map((item) => { | ||
const result: SbomOfVulnerability = { | ||
...item, | ||
sbom: sbomsById.get(item.sbomId), | ||
}; | ||
return result; | ||
}); | ||
}, [allSboms, sbomsById]); | ||
|
||
// Summary | ||
|
||
const sbomsSummary = React.useMemo(() => { | ||
return allSbomsWithMappedData.reduce((prev, current) => { | ||
if (current.status) { | ||
const status = current.status; | ||
return { | ||
...prev, | ||
total: prev.total + 1, | ||
status: { | ||
...prev.status, | ||
[status]: prev.status[status] + 1, | ||
}, | ||
}; | ||
} else { | ||
return prev; | ||
} | ||
}, DEFAULT_SUMMARY); | ||
}, [allSbomsWithMappedData]); | ||
|
||
return { | ||
isFetching: isFetchingAdvisories || isFetchingSboms, | ||
fetchError: fetchErrorAdvisories, | ||
sboms: allSbomsWithMappedData, | ||
summary: sbomsSummary, | ||
}; | ||
}; |
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
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
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
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
File renamed without changes.
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
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
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
Oops, something went wrong.