refactor: Use `isEnabled` to check feature flag state
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:
- Adds test coverage to the
feature_flags
API. - Creates a
gitlab.featureFlags
context that can be used inpackage.json
'swhen
clauses. - Refactors the modules that were using
getExtensionConfiguration
to check a feature flag's state. It replacesgetExtensionConfiguration
withisEnabled(FeatureFlag.featureFlag)
andgitlab.featureFlags
inpackage.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