Skip to content

Commit

Permalink
feat: add org name to snyk code test
Browse files Browse the repository at this point in the history
  • Loading branch information
patricia-v committed Feb 22, 2022
1 parent 0d6a99c commit 33097ce
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/lib/plugins/sast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export const codePlugin: EcosystemPlugin = {
}
const numOfIssues = sarifTypedResult!.runs?.[0].results?.length || 0;
analytics.add('sast-issues-found', numOfIssues);
const meta = getMeta(options, path);
let newOrg = options.org;
if (!newOrg && sastSettings.org) {
newOrg = sastSettings.org;
}
const meta = getMeta({ ...options, org: newOrg }, path);
const prefix = getPrefix(path);
let readableResult = getCodeDisplayedOutput(
sarifTypedResult!,
Expand Down
1 change: 1 addition & 0 deletions src/lib/plugins/sast/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface SastSettings {
error?: string;
userMessage?: string;
localCodeEngine: LocalCodeEngine;
org?: string;
}

export interface TrackUsageResponse {
Expand Down
71 changes: 71 additions & 0 deletions test/jest/unit/snyk-code/snyk-code-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,77 @@ describe('Test snyk code', () => {
}
});

describe('Default org test in CLI output', () => {
beforeAll(() => {
userConfig.set('org', 'defaultOrg');
});

afterAll(() => {
userConfig.set('org', undefined);
});

it('should show the default org in the output when org is not provided', async () => {
const options: ArgsOptions = {
path: '',
traverseNodeModules: false,
showVulnPaths: 'none',
code: true,
_: [],
_doubleDashArgs: [],
};

analyzeFoldersMock.mockResolvedValue(sampleAnalyzeFoldersResponse);
isSastEnabledForOrgSpy.mockResolvedValueOnce({
sastEnabled: true,
localCodeEngine: {
enabled: false,
},
org: 'defaultOrg',
});
trackUsageSpy.mockResolvedValue({});

try {
await snykTest('some/path', options);
} catch (error) {
const errMessage = stripAscii(stripAnsi(error.message.trim()));

expect(error.code).toBe('VULNS');
expect(errMessage).toMatch(/Organization:\s+defaultOrg/);
}
});

it('should show the provided org in the output when org is provided', async () => {
const options: ArgsOptions = {
path: '',
traverseNodeModules: false,
showVulnPaths: 'none',
code: true,
_: [],
_doubleDashArgs: [],
org: 'otherOrg',
};

analyzeFoldersMock.mockResolvedValue(sampleAnalyzeFoldersResponse);
isSastEnabledForOrgSpy.mockResolvedValueOnce({
sastEnabled: true,
localCodeEngine: {
enabled: false,
},
org: 'defaultOrg',
});
trackUsageSpy.mockResolvedValue({});

try {
await snykTest('some/path', options);
} catch (error) {
const errMessage = stripAscii(stripAnsi(error.message.trim()));

expect(error.code).toBe('VULNS');
expect(errMessage).toMatch(/Organization:\s+otherOrg/);
}
});
});

it.each([
['sarif', { sarif: true }],
['json', { json: true }],
Expand Down

0 comments on commit 33097ce

Please sign in to comment.