1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-26 13:48:33 +02: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, environment: string,
featureName: string, featureName: string,
enabled: boolean, enabled: boolean,
): Promise<boolean> { ): Promise<number> {
await this.db(T.featureEnvs) return this.db(T.featureEnvs).update({ enabled }).where({
.update({ enabled }) environment,
.where({ environment, feature_name: featureName }); feature_name: featureName,
return enabled; enabled: !enabled,
});
} }
async connectProject( async connectProject(

View File

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

View File

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

View File

@ -1087,6 +1087,13 @@ test('Disabling environment creates a FEATURE_ENVIRONMENT_DISABLED event', async
.send({ name: 'default', constraints: [], properties: {} }) .send({ name: 'default', constraints: [], properties: {} })
.expect(200); .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 await app.request
.post( .post(
`/api/admin/projects/default/features/${featureName}/environments/${environment}/off`, `/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, environment: string,
featureName: string, featureName: string,
enabled: boolean, enabled: boolean,
): Promise<boolean> { ): Promise<number> {
const fE = await this.get({ environment, featureName }); const fE = await this.get({ environment, featureName });
fE.enabled = enabled; if (fE.enabled !== enabled) {
return enabled; fE.enabled = enabled;
return 1;
} else {
return 0;
}
} }
async connectProject( async connectProject(