Refactor handling of feature flags

This commit centralizes how feature flags are handled. All feature flags
must now add an entry in the `featureFlagConfig` dictionary. This
dictionary associates the flag with an environment variable name and
optionally a minimum version for CodeQL.

The new logic is:

- if the environment variable is set to false: disabled
- if the minimum version requirement specified and met: disabled
- if the environment variable is set to true: enable
- Otherwise check feature flag enablement from the server
This commit is contained in:
Andrew Eisenberg 2022-10-05 15:54:07 -07:00
parent 24c8de16fa
commit e5c3375225
27 changed files with 400 additions and 368 deletions

View file

@ -29,9 +29,8 @@ const ALL_FEATURE_FLAGS_DISABLED_VARIANTS = [
for (const variant of ALL_FEATURE_FLAGS_DISABLED_VARIANTS) {
(0, ava_1.default)(`All feature flags are disabled if running against ${variant.description}`, async (t) => {
await (0, util_1.withTmpDir)(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const loggedMessages = [];
const featureFlags = new feature_flags_1.GitHubFeatureFlags(variant.gitHubVersion, testApiDetails, testRepositoryNwo, (0, testing_utils_1.getRecordingLogger)(loggedMessages));
const featureFlags = setUpTmpDir(tmpDir, (0, testing_utils_1.getRecordingLogger)(loggedMessages), variant.gitHubVersion);
for (const flag of Object.values(feature_flags_1.FeatureFlag)) {
t.assert((await featureFlags.getValue(flag)) === false);
}
@ -43,14 +42,13 @@ for (const variant of ALL_FEATURE_FLAGS_DISABLED_VARIANTS) {
}
(0, ava_1.default)("API response missing", async (t) => {
await (0, util_1.withTmpDir)(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const loggedMessages = [];
const featureFlags = new feature_flags_1.GitHubFeatureFlags({ type: util_1.GitHubVariant.DOTCOM }, testApiDetails, testRepositoryNwo, (0, testing_utils_1.getRecordingLogger)(loggedMessages));
const featureFlags = setUpTmpDir(tmpDir, (0, testing_utils_1.getRecordingLogger)(loggedMessages));
(0, testing_utils_1.mockFeatureFlagApiEndpoint)(403, {});
for (const flag of Object.values(feature_flags_1.FeatureFlag)) {
t.assert((await featureFlags.getValue(flag)) === false);
}
for (const featureFlag of ["ml_powered_queries_enabled"]) {
for (const featureFlag of Object.keys(feature_flags_1.featureFlagConfig)) {
t.assert(loggedMessages.find((v) => v.type === "debug" &&
v.message ===
`No feature flags API response for ${featureFlag}, considering it disabled.`) !== undefined);
@ -59,14 +57,13 @@ for (const variant of ALL_FEATURE_FLAGS_DISABLED_VARIANTS) {
});
(0, ava_1.default)("Feature flags are disabled if they're not returned in API response", async (t) => {
await (0, util_1.withTmpDir)(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const loggedMessages = [];
const featureFlags = new feature_flags_1.GitHubFeatureFlags({ type: util_1.GitHubVariant.DOTCOM }, testApiDetails, testRepositoryNwo, (0, testing_utils_1.getRecordingLogger)(loggedMessages));
const featureFlags = setUpTmpDir(tmpDir, (0, testing_utils_1.getRecordingLogger)(loggedMessages));
(0, testing_utils_1.mockFeatureFlagApiEndpoint)(200, {});
for (const flag of Object.values(feature_flags_1.FeatureFlag)) {
t.assert((await featureFlags.getValue(flag)) === false);
}
for (const featureFlag of ["ml_powered_queries_enabled"]) {
for (const featureFlag of Object.keys(feature_flags_1.featureFlagConfig)) {
t.assert(loggedMessages.find((v) => v.type === "debug" &&
v.message ===
`Feature flag '${featureFlag}' undefined in API response, considering it disabled.`) !== undefined);
@ -75,31 +72,97 @@ for (const variant of ALL_FEATURE_FLAGS_DISABLED_VARIANTS) {
});
(0, ava_1.default)("Feature flags exception is propagated if the API request errors", async (t) => {
await (0, util_1.withTmpDir)(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const featureFlags = new feature_flags_1.GitHubFeatureFlags({ type: util_1.GitHubVariant.DOTCOM }, testApiDetails, testRepositoryNwo, (0, logging_1.getRunnerLogger)(true));
const featureFlags = setUpTmpDir(tmpDir);
(0, testing_utils_1.mockFeatureFlagApiEndpoint)(500, {});
await t.throwsAsync(async () => featureFlags.getValue(feature_flags_1.FeatureFlag.MlPoweredQueriesEnabled), {
message: "Encountered an error while trying to load feature flags: Error: some error message",
});
});
});
const FEATURE_FLAGS = ["ml_powered_queries_enabled"];
for (const featureFlag of FEATURE_FLAGS) {
for (const featureFlag of Object.keys(feature_flags_1.featureFlagConfig)) {
(0, ava_1.default)(`Feature flag '${featureFlag}' is enabled if enabled in the API response`, async (t) => {
await (0, util_1.withTmpDir)(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const featureFlags = new feature_flags_1.GitHubFeatureFlags({ type: util_1.GitHubVariant.DOTCOM }, testApiDetails, testRepositoryNwo, (0, logging_1.getRunnerLogger)(true));
const featureFlags = setUpTmpDir(tmpDir);
// set all feature flags to false except the one we're testing
const expectedFeatureFlags = {};
for (const f of FEATURE_FLAGS) {
expectedFeatureFlags[f] = false;
for (const f of Object.keys(feature_flags_1.featureFlagConfig)) {
expectedFeatureFlags[f] = f === featureFlag;
}
expectedFeatureFlags[featureFlag] = true;
(0, testing_utils_1.mockFeatureFlagApiEndpoint)(200, expectedFeatureFlags);
const actualFeatureFlags = {
ml_powered_queries_enabled: await featureFlags.getValue(feature_flags_1.FeatureFlag.MlPoweredQueriesEnabled),
};
// retrieve the values of the actual feature flags
const actualFeatureFlags = {};
for (const f of Object.keys(feature_flags_1.featureFlagConfig)) {
actualFeatureFlags[f] = await featureFlags.getValue(f);
}
// Alls flags should be false except the one we're testing
t.deepEqual(actualFeatureFlags, expectedFeatureFlags);
});
});
(0, ava_1.default)(`Feature flag '${featureFlag}' is enabled if the associated environment variable is true`, async (t) => {
await (0, util_1.withTmpDir)(async (tmpDir) => {
const featureFlags = setUpTmpDir(tmpDir);
// set all feature flags to false
const expectedFeatureFlags = {};
for (const f of Object.keys(feature_flags_1.featureFlagConfig)) {
expectedFeatureFlags[f] = false;
}
(0, testing_utils_1.mockFeatureFlagApiEndpoint)(200, expectedFeatureFlags);
// feature flag should be disabled initially
t.assert(!(await featureFlags.getValue(featureFlag)));
// set env var to true and check that the feature flag is now enabled
process.env[feature_flags_1.featureFlagConfig[featureFlag].envVar] = "true";
t.assert(await featureFlags.getValue(featureFlag));
});
});
(0, ava_1.default)(`Feature flag '${featureFlag}' is disabled if the associated environment variable is false`, async (t) => {
await (0, util_1.withTmpDir)(async (tmpDir) => {
const featureFlags = setUpTmpDir(tmpDir);
// set all feature flags to true
const expectedFeatureFlags = {};
for (const f of Object.keys(feature_flags_1.featureFlagConfig)) {
expectedFeatureFlags[f] = true;
}
(0, testing_utils_1.mockFeatureFlagApiEndpoint)(200, expectedFeatureFlags);
// feature flag should be enabled initially
t.assert(await featureFlags.getValue(featureFlag));
// set env var to false and check that the feature flag is now disabled
process.env[feature_flags_1.featureFlagConfig[featureFlag].envVar] = "false";
t.assert(!(await featureFlags.getValue(featureFlag)));
});
});
if (feature_flags_1.featureFlagConfig[featureFlag].minimumVersion !== undefined) {
(0, ava_1.default)(`Feature flag '${featureFlag}' is disabled if the minimum CLI version is below ${feature_flags_1.featureFlagConfig[featureFlag].minimumVersion}`, async (t) => {
await (0, util_1.withTmpDir)(async (tmpDir) => {
const featureFlags = setUpTmpDir(tmpDir);
// set all feature flags to true
const expectedFeatureFlags = {};
for (const f of Object.keys(feature_flags_1.featureFlagConfig)) {
expectedFeatureFlags[f] = true;
}
(0, testing_utils_1.mockFeatureFlagApiEndpoint)(200, expectedFeatureFlags);
// feature flag should be enabled initially (ignoring the minimum CLI version)
t.assert(await featureFlags.getValue(featureFlag));
// feature flag should be disabled when an old CLI version is set
let codeql = (0, testing_utils_1.mockCodeQLVersion)("2.0.0");
t.assert(!(await featureFlags.getValue(featureFlag, codeql)));
// even setting the env var to true should not enable the feature flag if
// the minimum CLI version is not met
process.env[feature_flags_1.featureFlagConfig[featureFlag].envVar] = "true";
t.assert(!(await featureFlags.getValue(featureFlag, codeql)));
// feature flag should be enabled when a new CLI version is set
// and env var is not set
process.env[feature_flags_1.featureFlagConfig[featureFlag].envVar] = "";
codeql = (0, testing_utils_1.mockCodeQLVersion)(feature_flags_1.featureFlagConfig[featureFlag].minimumVersion);
t.assert(await featureFlags.getValue(featureFlag, codeql));
// set env var to false and check that the feature flag is now disabled
process.env[feature_flags_1.featureFlagConfig[featureFlag].envVar] = "false";
t.assert(!(await featureFlags.getValue(featureFlag, codeql)));
});
});
}
}
function setUpTmpDir(tmpDir, logger = (0, logging_1.getRunnerLogger)(true), gitHubVersion = { type: util_1.GitHubVariant.DOTCOM }) {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
return new feature_flags_1.GitHubFeatureFlags(gitHubVersion, testApiDetails, testRepositoryNwo, logger);
}
//# sourceMappingURL=feature-flags.test.js.map