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 function to get feature flags from a snyk org #364

Merged
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
40 changes: 40 additions & 0 deletions src/lib/get-feature-flag-for-snyk-org.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { requestsManager } from 'snyk-request-manager';
import * as debugLib from 'debug';

const debug = debugLib('snyk:get-feature-flag');

export async function getFeatureFlag(
requestManager: requestsManager,
featureFlagName: string,
orgId: string,
): Promise<boolean> {
debug(
`Checking if feature flag ${featureFlagName} is enabled for Snyk org ${orgId}`,
);
try {
const url = `cli-config/feature-flags/${featureFlagName}?org=${orgId}`;
const res = await requestManager.request({
verb: 'get',
url: url,
useRESTApi: false,
});
debug(`Feature flag ${featureFlagName} is enabled for Org ${orgId}`);
return res.data['ok'];
} catch (err) {
if (err instanceof Error) {
//Currently this is the only way to distinguish between an actual 403 and a 403 that is returned when an org hasn't got that FF enabled
if (JSON.stringify(err).search('"ok":false') > 0) {
debug(
`Feature flag ${featureFlagName} is not enabled for Org ${orgId}, please advise with your Snyk representative`,
);
} else {
debug(
`Could not fetch the ${featureFlagName} feature flag for ${orgId}\n ${JSON.stringify(
err,
)}`,
);
}
}
return false;
}
}
48 changes: 48 additions & 0 deletions test/lib/get-feature-flag.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { requestsManager } from 'snyk-request-manager';
import { getFeatureFlag } from '../../src/lib/get-feature-flag-for-snyk-org';

jest.unmock('snyk-request-manager');
jest.requireActual('snyk-request-manager');
const orgId = process.env.TEST_ORG_ID as string;

describe('getFeatureFlag', () => {
const requestManager = new requestsManager({
userAgentPrefix: 'snyk-api-import:tests',
});
afterAll(async () => {
jest.clearAllMocks();
}, 1000);

it('get feature flag for org - mock', async () => {
jest.spyOn(requestManager, 'request').mockResolvedValueOnce({
data: {
ok: true,
},
});

const res = await getFeatureFlag(
requestManager,
'existingFeatureFlag',
'someOrgId',
);
expect(res).toBeTruthy();
});

it('Error if the request fails with generic 403 - not a real Snyk org', async () => {
const res = await getFeatureFlag(
requestManager,
'nonEnabledFeatureFlag',
'0000',
);
expect(res).toBeFalsy();
}, 20000);

it('Error if the request fails with a 403 that indicates the FF is not enabled for a real org', async () => {
const res = await getFeatureFlag(
requestManager,
'nonEnabledFeatureFlag',
orgId,
);
expect(res).toBeFalsy();
}, 20000);
});