Skip to content

Commit

Permalink
Add support for feature flagging using cloudIds when available
Browse files Browse the repository at this point in the history
  • Loading branch information
subbuvenk-atlas committed Dec 18, 2024
1 parent 65b6fca commit 21f8a75
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/resolvers/admin-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ resolver.define('project/lastSyncTime', async (): Promise<ResolverResponse<strin
}
});

resolver.define('features', (): ResolverResponse<FeaturesList> => {
return getFeatures();
resolver.define('features', (req): ResolverResponse<FeaturesList> => {
const {
context: { cloudId },
} = req;

return getFeatures(cloudId);
});

resolver.define('appId', (): ResolverResponse<string> => {
Expand Down
8 changes: 6 additions & 2 deletions src/resolvers/import-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ resolver.define('project/import/clear', async (): Promise<ResolverResponse> => {
}
});

resolver.define('features', (): ResolverResponse<FeaturesList> => {
return getFeatures();
resolver.define('features', (req): ResolverResponse<FeaturesList> => {
const {
context: { cloudId },
} = req;

return getFeatures(cloudId);
});

resolver.define('appId', (): ResolverResponse<string> => {
Expand Down
4 changes: 2 additions & 2 deletions src/resolvers/shared-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { getWebhookSetupConfig, setupAndValidateWebhook } from '../services/webh
import { getForgeAppId } from '../utils/get-forge-app-id';
import { WebhookSetupConfig } from '../types';

export const getFeatures = (): ResolverResponse<FeaturesList> => {
export const getFeatures = (cloudId: string): ResolverResponse<FeaturesList> => {
try {
const features = listFeatures();
const features = listFeatures(cloudId);
return {
success: true,
data: features,
Expand Down
23 changes: 21 additions & 2 deletions src/services/feature-flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,36 @@ describe('listFeatures', () => {
process.env.FF_SEND_STAGING_EVENTS = 'false';
});

test('gets feature flags from their variables', async () => {
it('gets feature flags from their variables', async () => {
process.env.FF_SEND_STAGING_EVENTS = 'true';

const featureFlags = listFeatures();

expect(featureFlags.isSendStagingEventsEnabled).toEqual(true);
});

test('gets feature flags in their default state', async () => {
it('gets feature flags in their default state', async () => {
const featureFlags = listFeatures();

expect(featureFlags.isSendStagingEventsEnabled).toEqual(false);
});

it('uses cloudId to determine if gitlab maintainer token is enabled', async () => {
process.env.ENABLE_GITLAB_MAINTAINER_TOKEN_CLOUD_IDS = 'cloudId1,cloudId2';
process.env.ENABLE_GITLAB_MAINTAINER_TOKEN = 'true';

let featureFlags = listFeatures('cloudId1');
expect(featureFlags.isGitlabMaintainerTokenEnabled).toEqual(true);

featureFlags = listFeatures();
expect(featureFlags.isGitlabMaintainerTokenEnabled).toEqual(true);

featureFlags = listFeatures('cloudId3');
expect(featureFlags.isGitlabMaintainerTokenEnabled).toEqual(false);

process.env.ENABLE_GITLAB_MAINTAINER_TOKEN_CLOUD_IDS = '';

featureFlags = listFeatures('cloudId1');
expect(featureFlags.isGitlabMaintainerTokenEnabled).toEqual(true);
});
});
15 changes: 11 additions & 4 deletions src/services/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@ const isDocumentComponentLinksDisabled = (defaultValue = false): boolean => {
return process.env.DISABLE_DOCUMENT_COMPONENT_LINKS === 'true' || defaultValue;
};

export const isGitlabMaintainerTokenEnabled = (defaultValue = false): boolean => {
return process.env.ENABLE_GITLAB_MAINTAINER_TOKEN === 'true' || defaultValue;
export const isGitlabMaintainerTokenEnabled = (cloudId?: string, defaultValue = false): boolean => {
// cloudId is available in frontend context when fetching features for AppContext.
// It is not available in all backend contexts, so we will use the default value if it is not provided.
const isEnabledForCloudId =
!!cloudId && !!process.env.ENABLE_GITLAB_MAINTAINER_TOKEN_CLOUD_IDS
? process.env.ENABLE_GITLAB_MAINTAINER_TOKEN_CLOUD_IDS.split(',').includes(cloudId)
: true;

return (process.env.ENABLE_GITLAB_MAINTAINER_TOKEN === 'true' && isEnabledForCloudId) || defaultValue;
};

export const listFeatures = (): FeaturesList => {
export const listFeatures = (cloudId?: string): FeaturesList => {
return {
[GitlabFeaturesEnum.SEND_STAGING_EVENTS]: isSendStagingEventsEnabled(),
[GitlabFeaturesEnum.DATA_COMPONENT_TYPES]: isDataComponentTypesEnabled(),
[GitlabFeaturesEnum.DISABLE_DOCUMENT_COMPONENT_LINKS]: isDocumentComponentLinksDisabled(),
[GitlabFeaturesEnum.ENABLE_GITLAB_MAINTAINER_TOKEN]: isGitlabMaintainerTokenEnabled(),
[GitlabFeaturesEnum.ENABLE_GITLAB_MAINTAINER_TOKEN]: isGitlabMaintainerTokenEnabled(cloudId),
};
};

0 comments on commit 21f8a75

Please sign in to comment.