Skip to content

Commit

Permalink
feat: add table detail to vulnerabilities practice
Browse files Browse the repository at this point in the history
  • Loading branch information
vlasy committed Jan 10, 2020
1 parent 39e8998 commit b1fc789
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/practices/JavaScript/SecurityVulnerabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { IPractice } from '../IPractice';
import shell from 'shelljs';
import debug from 'debug';
import { sync as commandExistsSync } from 'command-exists';
import { PracticeBase } from '../PracticeBase';
import { ReportDetailType } from '../../reporters/ReporterData';

enum PackageManagerType {
unknown = 'unknown',
Expand All @@ -20,15 +22,15 @@ enum PackageManagerType {
reportOnlyOnce: true,
url: 'https://snyk.io/',
})
export class SecurityVulnerabilitiesPractice implements IPractice {
export class SecurityVulnerabilitiesPractice extends PracticeBase {
async isApplicable(ctx: PracticeContext): Promise<boolean> {
return (
ctx.projectComponent.language === ProgrammingLanguage.JavaScript || ctx.projectComponent.language === ProgrammingLanguage.TypeScript
);
}

async evaluate(ctx: PracticeContext): Promise<PracticeEvaluationResult> {
const npmCmd = 'npm audit --audit-level=high';
const npmCmd = 'npm audit --audit-level=high --json';
const yarnCmd = 'yarn audit --summary';
const getPackageManager = async () => {
const packageLockExists = await ctx.fileInspector?.exists('package-lock.json');
Expand Down Expand Up @@ -69,8 +71,25 @@ export class SecurityVulnerabilitiesPractice implements IPractice {
shell.cd(ctx.fileInspector?.basePath);
const result = shell.exec(packageManager === PackageManagerType.npm ? npmCmd : yarnCmd, { silent: true });
shell.cd(currentDir);
this.setData(result);
if (packageManager === PackageManagerType.npm && result.code > 0) return PracticeEvaluationResult.notPracticing;
if (result.code > 7) return PracticeEvaluationResult.notPracticing; // only other option is Yarn
return PracticeEvaluationResult.practicing;
}

setData(result: string, packageManager = PackageManagerType.npm): void {
if (packageManager !== PackageManagerType.npm) return; // TODO: yarn produces JSON-lines so skip it for now
const data = JSON.parse(result);
this.data.details = [
{
type: ReportDetailType.table,
headers: ['Action', 'Module', 'Version'],
data: data.actions.map((action: { action: string; module: string; target?: string }) => ({
action: action.action,
module: action.module,
version: action.target,
})),
},
];
}
}

0 comments on commit b1fc789

Please sign in to comment.