1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

fix: Only trigger environment enabled/disabled events if different f… (#1053)

Co-authored-by: Ivar Conradi Østhus <ivarconr@gmail.com>
This commit is contained in:
Christopher Kolstad 2021-10-21 22:33:50 +02:00 committed by GitHub
parent 9b3e62c56f
commit 6914bd7908
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 113 additions and 27 deletions

View File

@ -196,11 +196,12 @@ export class FeatureEnvironmentStore implements IFeatureEnvironmentStore {
environment: string,
featureName: string,
enabled: boolean,
): Promise<boolean> {
await this.db(T.featureEnvs)
.update({ enabled })
.where({ environment, feature_name: featureName });
return enabled;
): Promise<number> {
return this.db(T.featureEnvs).update({ enabled }).where({
environment,
feature_name: featureName,
enabled: !enabled,
});
}
async connectProject(

View File

@ -642,25 +642,29 @@ class FeatureToggleServiceV2 {
);
}
}
await this.featureEnvironmentStore.setEnvironmentEnabledStatus(
environment,
featureName,
enabled,
);
const updatedEnvironmentStatus =
await this.featureEnvironmentStore.setEnvironmentEnabledStatus(
environment,
featureName,
enabled,
);
const feature = await this.featureToggleStore.get(featureName);
const tags = await this.featureTagStore.getAllTagsForFeature(
featureName,
);
await this.eventStore.store({
type: enabled
? FEATURE_ENVIRONMENT_ENABLED
: FEATURE_ENVIRONMENT_DISABLED,
createdBy: userName,
data: { name: featureName },
tags,
project: projectId,
environment,
});
if (updatedEnvironmentStatus > 0) {
const tags = await this.featureTagStore.getAllTagsForFeature(
featureName,
);
await this.eventStore.store({
type: enabled
? FEATURE_ENVIRONMENT_ENABLED
: FEATURE_ENVIRONMENT_DISABLED,
createdBy: userName,
data: { name: featureName },
tags,
project: projectId,
environment,
});
}
return feature;
}
throw new NotFoundError(

View File

@ -20,7 +20,7 @@ export interface IFeatureEnvironmentStore
environment: string,
featureName: string,
enabled: boolean,
): Promise<boolean>;
): Promise<number>;
getEnvironmentMetaData(
environment: string,
featureName: string,

View File

@ -1087,6 +1087,13 @@ test('Disabling environment creates a FEATURE_ENVIRONMENT_DISABLED event', async
.send({ name: 'default', constraints: [], properties: {} })
.expect(200);
await app.request
.post(
`/api/admin/projects/default/features/${featureName}/environments/${environment}/on`,
)
.set('Content-Type', 'application/json')
.expect(200);
await app.request
.post(
`/api/admin/projects/default/features/${featureName}/environments/${environment}/off`,

View File

@ -0,0 +1,70 @@
import { IUnleashStores } from '../../../lib/types';
import dbInit from '../helpers/database-init';
import getLogger from '../../fixtures/no-logger';
import { IFeatureEnvironmentStore } from '../../../lib/types/stores/feature-environment-store';
import { IFeatureToggleStore } from '../../../lib/types/stores/feature-toggle-store';
import { IEnvironmentStore } from '../../../lib/types/stores/environment-store';
let db;
let stores: IUnleashStores;
let featureEnvironmentStore: IFeatureEnvironmentStore;
let featureStore: IFeatureToggleStore;
let environmentStore: IEnvironmentStore;
beforeEach(async () => {
db = await dbInit('feature_environment_store_serial', getLogger);
stores = db.stores;
featureEnvironmentStore = stores.featureEnvironmentStore;
environmentStore = stores.environmentStore;
featureStore = stores.featureToggleStore;
});
afterEach(async () => {
await db.destroy();
});
test('Setting enabled to same as existing value returns 0', async () => {
let envName = 'enabled-to-true';
let featureName = 'enabled-to-true-feature';
await environmentStore.create({
name: envName,
enabled: true,
type: 'test',
});
await featureStore.create('default', { name: featureName });
await featureEnvironmentStore.connectProject(envName, 'default');
await featureEnvironmentStore.connectFeatures(envName, 'default');
const enabledStatus = await featureEnvironmentStore.isEnvironmentEnabled(
featureName,
envName,
);
const changed = await featureEnvironmentStore.setEnvironmentEnabledStatus(
envName,
featureName,
enabledStatus,
);
expect(changed).toBe(0);
});
test('Setting enabled to not existing value returns 1', async () => {
let envName = 'enabled-toggle';
let featureName = 'enabled-toggle-feature';
await environmentStore.create({
name: envName,
enabled: true,
type: 'test',
});
await featureStore.create('default', { name: featureName });
await featureEnvironmentStore.connectProject(envName, 'default');
await featureEnvironmentStore.connectFeatures(envName, 'default');
const enabledStatus = await featureEnvironmentStore.isEnvironmentEnabled(
featureName,
envName,
);
const changed = await featureEnvironmentStore.setEnvironmentEnabledStatus(
envName,
featureName,
!enabledStatus,
);
expect(changed).toBe(1);
});

View File

@ -107,10 +107,14 @@ export default class FakeFeatureEnvironmentStore
environment: string,
featureName: string,
enabled: boolean,
): Promise<boolean> {
): Promise<number> {
const fE = await this.get({ environment, featureName });
fE.enabled = enabled;
return enabled;
if (fE.enabled !== enabled) {
fE.enabled = enabled;
return 1;
} else {
return 0;
}
}
async connectProject(