Skip to content

refactor: Use `isEnabled` to check feature flag state

Enrique Alcántara requested to merge refactor-feature-flag-state into main

In !991 (merged), we introduced a feature_flags module that exposes an API to check if a feature flag is enabled. This module also provides an easy way of setting a default value for the feature flag.

This merge request introduces the following enhancements:

  1. Adds test coverage to the feature_flags API.
  2. Creates a gitlab.featureFlags context that can be used in package.json 's when clauses.
  3. Refactors the modules that were using getExtensionConfiguration to check a feature flag's state. It replaces getExtensionConfiguration with isEnabled(FeatureFlag.featureFlag) and gitlab.featureFlags in package.json .

Defining a feature flag

Add your feature flag to the common/feature_flags/constants.ts module. You can also set a default value for the feature flag which allows enabling the feature flag by default in future releases:

// Add your feature flag here
export enum FeatureFlag {
  Snippets = 'snippets',
}

// Set the feature flag default value here
export const FEATURE_FLAGS_DEFAULT_VALUES = {
  [FeatureFlag.Snippets]: true,
};

Checking if a feature flag is enabled

Example of checking feature flag state in the source code

import { isEnabled, FeatureFlag } from 'common/feature_flags';

if (isEnabled(FeatureFlag.Snippets)) {
  // snippets are enabled
}

Example of checking feature flag state in package.json

{
  "command": "gl.createSnippet",
  "when": "gitlab.featureFlags.snippets"
}
Edited by Enrique Alcántara

Merge request reports

Loading