From 6718cd38baafb0a2f468d3b86a71457bd67f425a Mon Sep 17 00:00:00 2001 From: Karthik Jeeyar Date: Tue, 9 Apr 2024 22:38:07 +0530 Subject: [PATCH] fix(pipelines): Handle invalid response on acs image scan tab gracefully (#26) --- .changeset/gentle-bugs-yawn.md | 5 +++++ .../ImageScan/ImageScanSummary.tsx | 12 ++++++------ .../utils/__tests__/acs-filter-utils.test.ts | 19 +++++++++++++++++++ .../Output/utils/acs-filter-utils.ts | 4 ++-- 4 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 .changeset/gentle-bugs-yawn.md diff --git a/.changeset/gentle-bugs-yawn.md b/.changeset/gentle-bugs-yawn.md new file mode 100644 index 0000000..fd8cdf6 --- /dev/null +++ b/.changeset/gentle-bugs-yawn.md @@ -0,0 +1,5 @@ +--- +"@aonic-ui/pipelines": patch +--- + +Handle the invalid data for acs image scan tab diff --git a/packages/pipelines/src/components/Output/Tabs/AdvancedClusterSecurity/ImageScan/ImageScanSummary.tsx b/packages/pipelines/src/components/Output/Tabs/AdvancedClusterSecurity/ImageScan/ImageScanSummary.tsx index f2305d7..1d56464 100644 --- a/packages/pipelines/src/components/Output/Tabs/AdvancedClusterSecurity/ImageScan/ImageScanSummary.tsx +++ b/packages/pipelines/src/components/Output/Tabs/AdvancedClusterSecurity/ImageScan/ImageScanSummary.tsx @@ -14,10 +14,10 @@ const ImageScanSummary: React.FC = () => { const { acsImageScanResult } = useACSContext(); const getResultsSummary = (data: ACSImageScanResult) => ({ - Critical: data.result.summary.CRITICAL, - Important: data.result.summary.IMPORTANT, - Moderate: data.result.summary.MODERATE, - Low: data.result.summary.LOW, + Critical: data.result?.summary?.CRITICAL ?? 0, + Important: data.result?.summary?.IMPORTANT ?? 0, + Moderate: data.result?.summary?.MODERATE ?? 0, + Low: data.result?.summary?.LOW ?? 0, }); const resultSummary = React.useMemo( @@ -96,7 +96,7 @@ const ImageScanSummary: React.FC = () => { @@ -105,7 +105,7 @@ const ImageScanSummary: React.FC = () => { diff --git a/packages/pipelines/src/components/Output/utils/__tests__/acs-filter-utils.test.ts b/packages/pipelines/src/components/Output/utils/__tests__/acs-filter-utils.test.ts index 99deaf8..4e09dae 100644 --- a/packages/pipelines/src/components/Output/utils/__tests__/acs-filter-utils.test.ts +++ b/packages/pipelines/src/components/Output/utils/__tests__/acs-filter-utils.test.ts @@ -119,6 +119,25 @@ describe('ACS filter utils', () => { }); describe('filterVulnerabilities', () => { + test('should return empty array for invalid data', () => { + const invalidACSImageScanData = { + result: { + summary: acsImageScanResult.result.summary, + }, + } as ACSImageScanResult; + + expect( + filterVulnerabilities(invalidACSImageScanData, { + statusFilters: [ACS_STATUS.Fixable], + }), + ).toHaveLength(0); + + expect( + filterVulnerabilities({} as ACSImageScanResult, { + statusFilters: [ACS_STATUS.Fixable], + }), + ).toHaveLength(0); + }); test('should filter vulnerabilities by status', () => { expect( filterVulnerabilities(acsImageScanResult, { diff --git a/packages/pipelines/src/components/Output/utils/acs-filter-utils.ts b/packages/pipelines/src/components/Output/utils/acs-filter-utils.ts index c215288..8511261 100644 --- a/packages/pipelines/src/components/Output/utils/acs-filter-utils.ts +++ b/packages/pipelines/src/components/Output/utils/acs-filter-utils.ts @@ -38,8 +38,8 @@ export const filterVulnerabilities = ( componentFilters = [], severityFilters = [], } = filters; - return !isEmpty(acsImageScanResult) - ? acsImageScanResult?.result?.vulnerabilities?.filter( + return !isEmpty(acsImageScanResult) && !!acsImageScanResult?.result?.vulnerabilities + ? acsImageScanResult.result.vulnerabilities?.filter( (vul: Vulnerability) => filterByStatus(vul, statusFilters) && filterData(vul, cveIdFilters, 'cveId') &&