Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function validateFixCommandIsSupported(

const snykFixSupported = await isFeatureFlagSupportedForOrg(
snykFixFeatureFlag,
options.org,
options.org ?? '',
);

debug('Feature flag check returned: ', snykFixSupported);
Expand Down
24 changes: 21 additions & 3 deletions src/lib/feature-flags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,40 @@ import * as Debug from 'debug';
const debug = Debug('snyk-feature-flags');
export const SHOW_MAVEN_BUILD_SCOPE = 'show-maven-build-scope';
export const SHOW_NPM_SCOPE = 'show-npm-scope';
const _ffCache = new Map<string, Map<string, OrgFeatureFlagResponse>>();

export async function isFeatureFlagSupportedForOrg(
featureFlag: string,
org,
org: string,
): Promise<OrgFeatureFlagResponse> {
let cachedOrg = org ? _ffCache.get(org) : undefined;
const cachedFF = org ? cachedOrg?.get(featureFlag) : undefined;

if (cachedFF) {
return cachedFF;
}

const response = await makeRequest({
method: 'GET',
headers: {
Authorization: getAuthHeader(),
},
qs: assembleQueryString({ org }),
qs: org ? assembleQueryString({ org }) : undefined,
url: `${config.API}/cli-config/feature-flags/${featureFlag}`,
gzip: true,
json: true,
});

const body = response.body as OrgFeatureFlagResponse;

if (body.code === 403 || body.code === 200) {
if (!cachedOrg) {
cachedOrg = new Map();
_ffCache.set(org, cachedOrg);
}
cachedOrg.set(featureFlag, body);
}

return (response as any).body;
}

Expand All @@ -35,7 +53,7 @@ export async function hasFeatureFlag(
): Promise<boolean | undefined> {
const { code, error, ok } = await isFeatureFlagSupportedForOrg(
featureFlag,
options.org,
options.org || '',
);

if (code === 401 || code === 403) {
Expand Down
40 changes: 39 additions & 1 deletion test/jest/unit/lib/feature-flags/feature-flags.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { hasFeatureFlag } from '../../../../../src/lib/feature-flags';
import {
hasFeatureFlag,
isFeatureFlagSupportedForOrg,
} from '../../../../../src/lib/feature-flags';
import * as request from '../../../../../src/lib/request';

beforeEach(() => {
jest.clearAllMocks();
});

describe('hasFeatureFlag fn', () => {
it.each`
hasFlag | expected
Expand Down Expand Up @@ -35,3 +42,34 @@ describe('hasFeatureFlag fn', () => {
).rejects.toThrowError('Forbidden');
});
});

describe('isFeatureFlagSupportedForOrg', () => {
it('should request the given feature flag', async () => {
const requestSpy = jest.spyOn(request, 'makeRequest').mockResolvedValue({
body: { code: 401, error: 'Unauthorized', ok: false },
} as any);

const { code } = await isFeatureFlagSupportedForOrg(
'cheeseburgers',
'org-id',
);

expect(code).toBe(401);
expect(requestSpy).toHaveBeenCalled();
});

it('should cache the result for subsequent calls', async () => {
const requestSpy = jest.spyOn(request, 'makeRequest').mockResolvedValue({
body: { code: 403, error: 'Forbidden', ok: false },
} as any);

await isFeatureFlagSupportedForOrg('cheeseburgers', 'org-id');
const { code } = await isFeatureFlagSupportedForOrg(
'cheeseburgers',
'org-id',
);

expect(code).toBe(403);
expect(requestSpy).toHaveBeenCalledTimes(1);
});
});
Loading