-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added summary section to IaC test output
- Loading branch information
Showing
9 changed files
with
209 additions
and
75 deletions.
There are no files selected for viewing
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
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,13 @@ | ||
import chalk, { Chalk, ColorSupport } from 'chalk'; | ||
import { SEVERITY } from '../../../snyk-test/common'; | ||
|
||
export const severityColor: { | ||
[severity in SEVERITY]: Chalk & { | ||
supportsColor: ColorSupport; | ||
}; | ||
} = { | ||
critical: chalk.hex('#AB191A'), | ||
high: chalk.hex('#CE501A'), | ||
medium: chalk.hex('#D68000'), | ||
low: chalk.hex('#88879E'), | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import chalk from 'chalk'; | ||
import { EOL } from 'os'; | ||
import { rightPadWithSpaces } from '../../../right-pad'; | ||
import { SEVERITY } from '../../../snyk-test/common'; | ||
import { color, icon } from '../../../theme'; | ||
import { IacOutputMeta } from '../../../types'; | ||
import { severityColor } from './color-utils'; | ||
import { IacTestData } from './types'; | ||
|
||
const PAD_LENGTH = 19; // chars to align | ||
const INDENT = ' '; | ||
|
||
export function formatIacTestSummary( | ||
testData: IacTestData, | ||
outputMeta: IacOutputMeta, | ||
): string { | ||
const title = chalk.bold.white('Test Summary'); | ||
const summarySections: string[] = [title]; | ||
|
||
summarySections.push(formatTestMetaSection(outputMeta)); | ||
|
||
summarySections.push(formatCountsSection(testData)); | ||
|
||
return summarySections.join(EOL.repeat(2)); | ||
} | ||
|
||
function formatTestMetaSection(iacMeta: IacOutputMeta): string { | ||
const metaSectionProperties: [string, string][] = []; | ||
|
||
if (iacMeta.orgName) { | ||
metaSectionProperties.push(['Organization', iacMeta.orgName]); | ||
} | ||
|
||
const metaSection = metaSectionProperties | ||
.map(([key, value]) => | ||
rightPadWithSpaces(`${INDENT}${key}: ${value}`, PAD_LENGTH), | ||
) | ||
.join(EOL); | ||
|
||
return metaSection; | ||
} | ||
|
||
function formatCountsSection(testData: IacTestData): string { | ||
const filesWithIssues = testData.results.filter( | ||
(result) => result.result.cloudConfigResults.length, | ||
).length; | ||
const filesWithoutIssues = testData.results.length - filesWithIssues; | ||
|
||
const countsSectionProperties: string[] = []; | ||
|
||
countsSectionProperties.push( | ||
`${chalk.bold( | ||
color.status.success(icon.VALID), | ||
)} Files without issues: ${chalk.white.bold(`${filesWithoutIssues}`)}`, | ||
); | ||
|
||
countsSectionProperties.push( | ||
`${chalk.bold( | ||
color.status.error(icon.ISSUE), | ||
)} Files with issues: ${chalk.white.bold(`${filesWithIssues}`)}`, | ||
); | ||
|
||
countsSectionProperties.push( | ||
`${INDENT}Ignored issues: ${chalk.white.bold(`${testData.ignoreCount}`)}`, | ||
); | ||
|
||
let totalIssuesCount = 0; | ||
|
||
const issueCountsBySeverities: { [key in SEVERITY | 'none']: number } = { | ||
critical: 0, | ||
high: 0, | ||
medium: 0, | ||
low: 0, | ||
none: 0, | ||
}; | ||
|
||
testData.results.forEach((iacTestResponse) => { | ||
totalIssuesCount += iacTestResponse.result.cloudConfigResults.length; | ||
iacTestResponse.result.cloudConfigResults.forEach((iacIssue) => { | ||
issueCountsBySeverities[iacIssue.severity]++; | ||
}); | ||
}); | ||
|
||
countsSectionProperties.push( | ||
`${INDENT}Total issues: ${chalk.white.bold( | ||
`${totalIssuesCount}`, | ||
)} [ ${severityColor.critical( | ||
`${issueCountsBySeverities.critical} critical`, | ||
)}, ${severityColor.high( | ||
`${issueCountsBySeverities.high} high`, | ||
)}, ${severityColor.medium( | ||
`${issueCountsBySeverities.medium} medium`, | ||
)}, ${severityColor.low(`${issueCountsBySeverities.low} low`)} ]`, | ||
); | ||
|
||
return countsSectionProperties.join(EOL); | ||
} |
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,6 @@ | ||
import { IacTestResponse } from '../../../snyk-test/iac-test-result'; | ||
|
||
export interface IacTestData { | ||
ignoreCount: number; | ||
results: IacTestResponse[]; | ||
} |
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