Skip to content

Commit

Permalink
fix: import tags (#3709)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed May 8, 2023
1 parent c2a3243 commit aa4ecdf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/lib/features/export-import-toggles/export-import-service.ts
Expand Up @@ -168,7 +168,7 @@ export default class ExportImportService {
const errors = ImportValidationMessages.compileErrors(
dto.project,
unsupportedStrategies,
unsupportedContextFields,
unsupportedContextFields || [],
[],
otherProjectFeatures,
false,
Expand Down Expand Up @@ -225,7 +225,7 @@ export default class ExportImportService {

private async importToggleStatuses(dto: ImportTogglesSchema, user: User) {
await Promise.all(
dto.data.featureEnvironments?.map((featureEnvironment) =>
(dto.data.featureEnvironments || []).map((featureEnvironment) =>
this.featureToggleService.updateEnabled(
dto.project,
featureEnvironment.name,
Expand Down Expand Up @@ -279,15 +279,17 @@ export default class ExportImportService {
await this.importTogglesStore.deleteTagsForFeatures(
dto.data.features.map((feature) => feature.name),
);
return Promise.all(
dto.data.featureTags?.map((tag) =>
this.featureTagService.addTag(

const featureTags = dto.data.featureTags || [];
for (const tag of featureTags) {
if (tag.tagType) {
await this.featureTagService.addTag(
tag.featureName,
{ type: tag.tagType, value: tag.tagValue },
extractUsernameFromUser(user),
),
),
);
);
}
}
}

private async importContextFields(dto: ImportTogglesSchema, user: User) {
Expand Down
51 changes: 51 additions & 0 deletions src/lib/features/export-import-toggles/export-import.e2e.test.ts
Expand Up @@ -14,6 +14,7 @@ import {
IProjectStore,
ISegment,
IStrategyConfig,
ITagStore,
IVariant,
} from '../../types';
import { DEFAULT_ENV } from '../../util';
Expand All @@ -33,6 +34,7 @@ let environmentStore: IEnvironmentStore;
let contextFieldStore: IContextFieldStore;
let projectStore: IProjectStore;
let toggleStore: IFeatureToggleStore;
let tagStore: ITagStore;

const defaultStrategy: IStrategyConfig = {
name: 'default',
Expand Down Expand Up @@ -150,13 +152,15 @@ beforeAll(async () => {
projectStore = db.stores.projectStore;
contextFieldStore = db.stores.contextFieldStore;
toggleStore = db.stores.featureToggleStore;
tagStore = db.stores.tagStore;
});

beforeEach(async () => {
await eventStore.deleteAll();
await toggleStore.deleteAll();
await projectStore.deleteAll();
await environmentStore.deleteAll();
await tagStore.deleteAll();

await contextFieldStore.deleteAll();
await app.createContextField({ name: 'appName' });
Expand Down Expand Up @@ -488,6 +492,10 @@ const exportedFeature: ImportTogglesSchema['data']['features'][0] = {
project: 'old_project',
name: 'first_feature',
};
const anotherExportedFeature: ImportTogglesSchema['data']['features'][0] = {
project: 'old_project',
name: 'second_feature',
};
const constraints: ImportTogglesSchema['data']['featureStrategies'][0]['constraints'] =
[
{
Expand Down Expand Up @@ -558,6 +566,31 @@ const defaultImportPayload: ImportTogglesSchema = {
environment: DEFAULT_ENV,
};

const importWithMultipleFeatures: ImportTogglesSchema = {
data: {
features: [exportedFeature, anotherExportedFeature],
featureStrategies: [],
featureEnvironments: [],
featureTags: [
{
featureName: exportedFeature.name,
tagType: 'simple',
tagValue: 'tag1',
},
{
featureName: anotherExportedFeature.name,
tagType: 'simple',
tagValue: 'tag1',
},
],
tagTypes,
contextFields: [],
segments: [],
},
project: DEFAULT_PROJECT,
environment: DEFAULT_ENV,
};

const getFeature = async (feature: string) =>
app.request.get(`/api/admin/features/${feature}`).expect(200);

Expand Down Expand Up @@ -614,6 +647,24 @@ test('import features to existing project and environment', async () => {
});
});

test('import multiple features with same tag', async () => {
await createProjects();

await app.importToggles(importWithMultipleFeatures);

const { body: tags1 } = await getTags(exportedFeature.name);
const { body: tags2 } = await getTags(anotherExportedFeature.name);

expect(tags1).toMatchObject({
version: 1,
tags: [{ value: 'tag1', type: 'simple' }],
});
expect(tags2).toMatchObject({
version: 1,
tags: [{ value: 'tag1', type: 'simple' }],
});
});

test('importing same JSON should work multiple times in a row', async () => {
await createProjects();
await app.importToggles(defaultImportPayload);
Expand Down

0 comments on commit aa4ecdf

Please sign in to comment.