diff --git a/src/lib/openapi/spec/export-result-schema.ts b/src/lib/openapi/spec/export-result-schema.ts index cb8db1b3ff..d45e5a6165 100644 --- a/src/lib/openapi/spec/export-result-schema.ts +++ b/src/lib/openapi/spec/export-result-schema.ts @@ -4,6 +4,7 @@ import { featureStrategySchema } from './feature-strategy-schema'; import { featureEnvironmentSchema } from './feature-environment-schema'; import { contextFieldSchema } from './context-field-schema'; import { featureTagSchema } from './feature-tag-schema'; +import { segmentSchema } from './segment-schema'; export const exportResultSchema = { $id: '#/components/schemas/exportResultSchema', @@ -41,6 +42,12 @@ export const exportResultSchema = { $ref: '#/components/schemas/featureTagSchema', }, }, + segments: { + type: 'array', + items: { + $ref: '#/components/schemas/segmentSchema', + }, + }, }, components: { schemas: { @@ -49,6 +56,7 @@ export const exportResultSchema = { featureEnvironmentSchema, contextFieldSchema, featureTagSchema, + segmentSchema, }, }, } as const; diff --git a/src/lib/services/export-import-service.ts b/src/lib/services/export-import-service.ts index 48f02eb329..c595e485da 100644 --- a/src/lib/services/export-import-service.ts +++ b/src/lib/services/export-import-service.ts @@ -4,6 +4,7 @@ import { IFeatureEnvironment, IFeatureStrategy, IFeatureStrategySegment, + ISegment, ITag, } from '../types/model'; import { Logger } from '../logger'; @@ -37,6 +38,7 @@ export interface IExportData { contextFields: IContextFieldDto[]; featureStrategies: IFeatureStrategy[]; featureEnvironments: IFeatureEnvironment[]; + segments: ISegment[]; } export default class ExportImportService { @@ -108,6 +110,7 @@ export default class ExportImportService { strategySegments, contextFields, featureTags, + segments, ] = await Promise.all([ this.toggleStore.getAllByNames(query.features), await this.featureEnvironmentStore.getAllByFeatures( @@ -121,6 +124,7 @@ export default class ExportImportService { this.segmentStore.getAllFeatureStrategySegments(), this.contextFieldStore.getAll(), this.featureTagStore.getAll(), + this.segmentStore.getAll(), ]); this.addSegmentsToStrategies(featureStrategies, strategySegments); const filteredContextFields = contextFields.filter((field) => @@ -130,12 +134,18 @@ export default class ExportImportService { ), ), ); + const filteredSegments = segments.filter((segment) => + featureStrategies.some((strategy) => + strategy.segments.includes(segment.id), + ), + ); const result = { features, featureStrategies, featureEnvironments, contextFields: filteredContextFields, featureTags, + segments: filteredSegments, }; await this.eventStore.store({ type: FEATURES_EXPORTED, diff --git a/src/test/e2e/api/admin/export-import.e2e.test.ts b/src/test/e2e/api/admin/export-import.e2e.test.ts index de35364a2d..86b848587f 100644 --- a/src/test/e2e/api/admin/export-import.e2e.test.ts +++ b/src/test/e2e/api/admin/export-import.e2e.test.ts @@ -134,8 +134,9 @@ afterAll(async () => { }); test('exports features', async () => { + const segmentName = 'my-segment'; await createProject('default', 'default'); - const segment = await createSegment({ name: 'S3', constraints: [] }); + const segment = await createSegment({ name: segmentName, constraints: [] }); const strategy = { name: 'default', parameters: { rollout: '100', stickiness: 'default' }, @@ -187,6 +188,11 @@ test('exports features', async () => { variants: [], }, ], + segments: [ + { + name: segmentName, + }, + ], }); }); @@ -366,6 +372,7 @@ test('import features to existing project and environment', async () => { }, ], contextFields: [], + segments: [], }, project: project, environment: environment, diff --git a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap index eec7962586..c4a0e11aca 100644 --- a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap +++ b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap @@ -1090,6 +1090,12 @@ exports[`should serve the OpenAPI spec 1`] = ` }, "type": "array", }, + "segments": { + "items": { + "$ref": "#/components/schemas/segmentSchema", + }, + "type": "array", + }, }, "required": [ "features",