From 9c4950457ecee8801bcbd96e92c9985a2a1e9ae4 Mon Sep 17 00:00:00 2001 From: IlanTSnyk Date: Thu, 13 Oct 2022 15:01:56 +0300 Subject: [PATCH] feat: add function to get feature flags from a snyk org --- src/lib/get-feature-flag-for-snyk-org.ts | 40 ++++++++++++++++++++ test/lib/get-feature-flag.test.ts | 48 ++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/lib/get-feature-flag-for-snyk-org.ts create mode 100644 test/lib/get-feature-flag.test.ts diff --git a/src/lib/get-feature-flag-for-snyk-org.ts b/src/lib/get-feature-flag-for-snyk-org.ts new file mode 100644 index 00000000..6f787b82 --- /dev/null +++ b/src/lib/get-feature-flag-for-snyk-org.ts @@ -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 { + 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; + } +} diff --git a/test/lib/get-feature-flag.test.ts b/test/lib/get-feature-flag.test.ts new file mode 100644 index 00000000..c73c6d9c --- /dev/null +++ b/test/lib/get-feature-flag.test.ts @@ -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); +});