-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-js-packages): implement runner for npm audit
- Loading branch information
Showing
8 changed files
with
468 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { executeRunner } from './lib/runner'; | ||
|
||
executeRunner(); | ||
await executeRunner(); |
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
81 changes: 81 additions & 0 deletions
81
packages/plugin-js-packages/src/lib/runner/audit/transform.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,81 @@ | ||
import type { AuditOutput, Issue, IssueSeverity } from '@code-pushup/models'; | ||
import { objectToEntries } from '@code-pushup/utils'; | ||
import { | ||
PackageAuditLevel, | ||
PackageDependency, | ||
packageAuditLevels, | ||
} from '../../config'; | ||
import { NpmAuditResultJson, Vulnerabilities } from './types'; | ||
|
||
export function auditResultToAuditOutput( | ||
result: NpmAuditResultJson, | ||
dependenciesType: PackageDependency, | ||
auditLevelMapping: Record<PackageAuditLevel, IssueSeverity>, | ||
): AuditOutput { | ||
const issues = vulnerabilitiesToIssues( | ||
result.vulnerabilities, | ||
auditLevelMapping, | ||
); | ||
return { | ||
slug: `npm-audit-${dependenciesType}`, | ||
score: result.metadata.vulnerabilities.total === 0 ? 1 : 0, | ||
value: result.metadata.vulnerabilities.total, | ||
displayValue: vulnerabilitiesToDisplayValue( | ||
result.metadata.vulnerabilities, | ||
), | ||
...(issues.length > 0 && { details: { issues } }), | ||
}; | ||
} | ||
|
||
export function vulnerabilitiesToDisplayValue( | ||
vulnerabilities: Record<PackageAuditLevel | 'total', number>, | ||
): string { | ||
if (vulnerabilities.total === 0) { | ||
return 'passed'; | ||
} | ||
|
||
const displayValue = packageAuditLevels | ||
.map(level => | ||
vulnerabilities[level] > 0 ? `${vulnerabilities[level]} ${level}` : '', | ||
) | ||
.filter(text => text !== '') | ||
.join(', '); | ||
return `${displayValue} ${ | ||
vulnerabilities.total === 1 ? 'vulnerability' : 'vulnerabilities' | ||
}`; | ||
} | ||
|
||
export function vulnerabilitiesToIssues( | ||
vulnerabilities: Vulnerabilities, | ||
auditLevelMapping: Record<PackageAuditLevel, IssueSeverity>, | ||
): Issue[] { | ||
if (Object.keys(vulnerabilities).length === 0) { | ||
return []; | ||
} | ||
|
||
return objectToEntries(vulnerabilities).map<Issue>(([, detail]) => { | ||
// Advisory details via can refer to another vulnerability | ||
// For now, only direct context is supported | ||
if ( | ||
Array.isArray(detail.via) && | ||
detail.via.length > 0 && | ||
typeof detail.via[0] === 'object' | ||
) { | ||
return { | ||
message: `${detail.name} dependency has a vulnerability "${ | ||
detail.via[0].title | ||
}" for versions ${detail.range}. Fix is ${ | ||
detail.fixAvailable ? '' : 'not ' | ||
}available. More information [here](${detail.via[0].url})`, | ||
severity: auditLevelMapping[detail.severity], | ||
}; | ||
} | ||
|
||
return { | ||
message: `${detail.name} dependency has a vulnerability for versions ${ | ||
detail.range | ||
}. Fix is ${detail.fixAvailable ? '' : 'not '}available.`, | ||
severity: auditLevelMapping[detail.severity], | ||
}; | ||
}); | ||
} |
Oops, something went wrong.