Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[COD-152] feat: analytics added to snykcode flow #1730

Merged
merged 1 commit into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: I have a vague memory of these things being required somewhere else. Did you discuss it with somebody who has more BI insights?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, those keys needs to be defined in registry src/lib/analytics/cli.ts, because we are not collecting free-form analytics keys anymore… for reasons

// 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