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

feat: add org name to snyk code test [NEBULA-195] #2764

Merged
merged 1 commit into from
Feb 23, 2022
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
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