1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-04 00:18:01 +01:00

fix: force clone feature to correctly copy variant environments (#2498)

patches an issue where cloning a feature would clone variants for
it's last found environment in the db. This now will clone the feature
environments correctly
This commit is contained in:
Simon Hornby 2022-11-22 17:00:44 +02:00 committed by GitHub
parent 27cb6b742a
commit 801df6953c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 4 deletions

View File

@ -768,18 +768,30 @@ class FeatureToggleService {
await this.validateName(newFeatureName);
const cToggle =
await this.featureStrategiesStore.getFeatureToggleWithEnvs(
await this.featureStrategiesStore.getFeatureToggleWithVariantEnvs(
featureName,
);
const newToggle = { ...cToggle, name: newFeatureName };
const newToggle = {
...cToggle,
name: newFeatureName,
variants: undefined,
};
const created = await this.createFeatureToggle(
projectId,
newToggle,
userName,
);
const tasks = newToggle.environments.flatMap((e) =>
const variantTasks = newToggle.environments.map((e) => {
return this.featureEnvironmentStore.addVariantsToFeatureEnvironment(
newToggle.name,
e.name,
e.variants,
);
});
const strategyTasks = newToggle.environments.flatMap((e) =>
e.strategies.map((s) => {
if (replaceGroupId && s.parameters.hasOwnProperty('groupId')) {
s.parameters.groupId = newFeatureName;
@ -795,7 +807,7 @@ class FeatureToggleService {
}),
);
await Promise.all(tasks);
await Promise.all([...strategyTasks, ...variantTasks]);
return created;
}

View File

@ -253,3 +253,61 @@ test('adding and removing an environment preserves variants when variants per en
const toggle = await service.getFeature(featureName, false, null, false);
expect(toggle.variants).toHaveLength(1);
});
test('cloning a feature toggle copies variant environments correctly', async () => {
const newToggleName = 'Molly';
const clonedToggleName = 'Dolly';
const targetEnv = 'gene-lab';
await service.createFeatureToggle(
'default',
{
name: newToggleName,
},
'test',
);
await stores.environmentStore.create({
name: 'gene-lab',
type: 'production',
});
await stores.featureEnvironmentStore.connectFeatureToEnvironmentsForProject(
newToggleName,
'default',
);
await stores.featureEnvironmentStore.addVariantsToFeatureEnvironment(
newToggleName,
targetEnv,
[
{
name: 'variant1',
weight: 100,
weightType: 'fix',
stickiness: 'default',
},
],
);
await service.cloneFeatureToggle(
newToggleName,
'default',
clonedToggleName,
true,
'test-user',
);
const clonedToggle =
await stores.featureStrategiesStore.getFeatureToggleWithVariantEnvs(
clonedToggleName,
);
const defaultEnv = clonedToggle.environments.find(
(x) => x.name === 'default',
);
const newEnv = clonedToggle.environments.find((x) => x.name === targetEnv);
expect(defaultEnv.variants).toHaveLength(0);
expect(newEnv.variants).toHaveLength(1);
});