Skip to content

Commit

Permalink
feat: add analytics to code client
Browse files Browse the repository at this point in the history
  • Loading branch information
j-sp4 committed Mar 30, 2021
1 parent 7361000 commit 143c363
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/lib/plugins/sast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { EcosystemPlugin } from '../../ecosystems/types';
import { FailedToRunTestError } from '../../errors/failed-to-run-test-error';
import { jsonStringifyLargeObject } from '../../json';
import * as analytics from '../../analytics';

const debug = debugLib('snyk-code-test');

Expand All @@ -25,6 +26,7 @@ export const codePlugin: EcosystemPlugin = {
async test(paths, options) {
try {
await validateCodeTest(options);
analytics.add('sast-scan', true);
// Currently code supports only one path
const path = paths[0];
const sarifTypedResult = await getCodeAnalysisAndParseResults(
Expand All @@ -43,6 +45,7 @@ export const codePlugin: EcosystemPlugin = {
);

const numOfIssues = sarifTypedResult.runs?.[0].results?.length || 0;
analytics.add('sast-issues-found', numOfIssues);
if (numOfIssues > 0) {
hasIssues(readableResult);
}
Expand Down
31 changes: 29 additions & 2 deletions test/jest/unit/snyk-code-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { config as userConfig } from '../../../src/lib/user-config';
import * as analysis from '../../../src/lib/plugins/sast/analysis';
import { Options, TestOptions } from '../../../src/lib/types';
import * as ecosystems from '../../../src/lib/ecosystems';
import { FailedToRunTestError } from '../../../src/lib/errors/failed-to-run-test-error';
import * as analytics from '../../../src/lib/analytics';

const { getCodeAnalysisAndParseResults } = analysis;
const osName = require('os-name');

Expand Down Expand Up @@ -121,7 +122,7 @@ describe('Test snyk code', () => {
}
});

it('succeed testing with correct error messages - with sarif output', async () => {
it('succeed testing with correct exit code - with sarif output', async () => {
const options: Options & TestOptions = {
path: '',
traverseNodeModules: false,
Expand All @@ -145,6 +146,32 @@ describe('Test snyk code', () => {
}
});

it('succeed testing with correct exit code - and analytics added', async () => {
const analyticSend = jest.spyOn(analytics, 'add');

const options: Options & TestOptions = {
path: '',
traverseNodeModules: false,
showVulnPaths: 'none',
code: true,
};

analyzeFoldersMock.mockResolvedValue(sampleAnalyzeFoldersResponse);
isFeatureFlagSupportedForOrgSpy.mockResolvedValueOnce({ ok: true });

try {
await ecosystems.testEcosystem('code', ['some/path'], options);
} catch (error) {
const errMessage = stripAscii(stripAnsi(error.message.trim()));
const expectedOutput = stripAscii(stripAnsi(testOutput.trim()));

// exit code 1
expect(error.code).toBe('VULNS');
expect(errMessage).toBe(expectedOutput);
expect(analyticSend).toBeCalledTimes(2);
}
});

it.each([
[{ code: 401 }, `Unauthorized: ${failedCodeTestMessage}`],
[{ code: 500 }, failedCodeTestMessage],
Expand Down

0 comments on commit 143c363

Please sign in to comment.