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

feat: updated default strategy event (#5462)

This commit is contained in:
Mateusz Kwasniewski 2023-11-28 14:59:20 +01:00 committed by GitHub
parent f0c28dd383
commit f6bc418bdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 24 deletions

View File

@ -102,7 +102,7 @@ export default class EnvironmentsController extends Controller {
this.route({ this.route({
method: 'post', method: 'post',
path: `${PREFIX}/:environment/default-strategy`, path: `${PREFIX}/:environment/default-strategy`,
handler: this.addDefaultStrategyToProjectEnvironment, handler: this.updateDefaultStrategyForProjectEnvironment,
permission: UPDATE_PROJECT, permission: UPDATE_PROJECT,
middleware: [ middleware: [
openApiService.validPath({ openApiService.validPath({
@ -110,7 +110,7 @@ export default class EnvironmentsController extends Controller {
operationId: 'addDefaultStrategyToProjectEnvironment', operationId: 'addDefaultStrategyToProjectEnvironment',
summary: 'Set environment-default strategy', summary: 'Set environment-default strategy',
description: description:
'Adds a default strategy for this environment. Unleash will use this strategy by default when enabling a toggle. Use the wild card "*" for `:environment` to add to all environments. ', 'Sets a default strategy for this environment. Unleash will use this strategy by default when enabling a toggle. Use the wild card "*" for `:environment` to add to all environments. ',
requestBody: createRequestSchema( requestBody: createRequestSchema(
'createFeatureStrategySchema', 'createFeatureStrategySchema',
), ),
@ -161,14 +161,14 @@ export default class EnvironmentsController extends Controller {
res.status(200).end(); res.status(200).end();
} }
async addDefaultStrategyToProjectEnvironment( async updateDefaultStrategyForProjectEnvironment(
req: Request<IProjectEnvironmentParams, CreateFeatureStrategySchema>, req: Request<IProjectEnvironmentParams, CreateFeatureStrategySchema>,
res: Response<CreateFeatureStrategySchema>, res: Response<CreateFeatureStrategySchema>,
): Promise<void> { ): Promise<void> {
const { projectId, environment } = req.params; const { projectId, environment } = req.params;
const strategy = req.body; const strategy = req.body;
const saved = await this.environmentService.addDefaultStrategy( const saved = await this.environmentService.updateDefaultStrategy(
environment, environment,
projectId, projectId,
strategy, strategy,

View File

@ -1,4 +1,5 @@
import { import {
DEFAULT_STRATEGY_UPDATED,
IEnvironment, IEnvironment,
IEnvironmentStore, IEnvironmentStore,
IFeatureEnvironmentStore, IFeatureEnvironmentStore,
@ -126,21 +127,34 @@ export default class EnvironmentService {
} }
} }
async addDefaultStrategy( async updateDefaultStrategy(
environment: string, environment: string,
projectId: string, projectId: string,
strategy: CreateFeatureStrategySchema, strategy: CreateFeatureStrategySchema,
username: string = 'unknown',
): Promise<CreateFeatureStrategySchema> { ): Promise<CreateFeatureStrategySchema> {
if (strategy.name !== 'flexibleRollout') { if (strategy.name !== 'flexibleRollout') {
throw new BadDataError( throw new BadDataError(
'Only "flexibleRollout" strategy can be used as a default strategy for an environment', 'Only "flexibleRollout" strategy can be used as a default strategy for an environment',
); );
} }
return this.projectStore.updateDefaultStrategy( const previousDefaultStrategy =
await this.projectStore.getDefaultStrategy(projectId, environment);
const defaultStrategy = await this.projectStore.updateDefaultStrategy(
projectId, projectId,
environment, environment,
strategy, strategy,
); );
await this.eventService.storeEvent({
type: DEFAULT_STRATEGY_UPDATED,
project: projectId,
environment,
createdBy: username,
preData: previousDefaultStrategy,
data: defaultStrategy,
});
return defaultStrategy;
} }
async overrideEnabledProjects( async overrideEnabledProjects(

View File

@ -111,6 +111,7 @@ export const SETTING_DELETED = 'setting-deleted' as const;
export const PROJECT_ENVIRONMENT_ADDED = 'project-environment-added' as const; export const PROJECT_ENVIRONMENT_ADDED = 'project-environment-added' as const;
export const PROJECT_ENVIRONMENT_REMOVED = export const PROJECT_ENVIRONMENT_REMOVED =
'project-environment-removed' as const; 'project-environment-removed' as const;
export const DEFAULT_STRATEGY_UPDATED = 'default-strategy-updated' as const;
export const CLIENT_METRICS = 'client-metrics' as const; export const CLIENT_METRICS = 'client-metrics' as const;
export const CLIENT_REGISTER = 'client-register' as const; export const CLIENT_REGISTER = 'client-register' as const;
@ -297,6 +298,7 @@ export const IEventTypes = [
BANNER_DELETED, BANNER_DELETED,
PROJECT_ENVIRONMENT_ADDED, PROJECT_ENVIRONMENT_ADDED,
PROJECT_ENVIRONMENT_REMOVED, PROJECT_ENVIRONMENT_REMOVED,
DEFAULT_STRATEGY_UPDATED,
] as const; ] as const;
export type IEventType = (typeof IEventTypes)[number]; export type IEventType = (typeof IEventTypes)[number];

View File

@ -113,19 +113,21 @@ test('Should not remove environment from project if project only has one environ
}); });
test('Should add default strategy to environment', async () => { test('Should add default strategy to environment', async () => {
const defaultStrategy = {
name: 'flexibleRollout',
constraints: [],
parameters: {
rollout: '50',
stickiness: 'customAppName',
groupId: 'stickytoggle',
},
};
await app.request await app.request
.post( .post(
`/api/admin/projects/default/environments/default/default-strategy`, `/api/admin/projects/default/environments/default/default-strategy`,
) )
.send({ .send(defaultStrategy)
name: 'flexibleRollout',
constraints: [],
parameters: {
rollout: '50',
stickiness: 'customAppName',
groupId: 'stickytoggle',
},
})
.expect(200); .expect(200);
const envs = const envs =
@ -134,15 +136,15 @@ test('Should add default strategy to environment', async () => {
expect(envs).toHaveLength(1); expect(envs).toHaveLength(1);
expect(envs[0]).toStrictEqual({ expect(envs[0]).toStrictEqual({
environment: 'default', environment: 'default',
defaultStrategy: { defaultStrategy,
name: 'flexibleRollout', });
constraints: [], const { body } = await app.getRecordedEvents();
parameters: { expect(body.events[0]).toMatchObject({
rollout: '50', type: 'default-strategy-updated',
stickiness: 'customAppName', project: 'default',
groupId: 'stickytoggle', environment: 'default',
}, data: defaultStrategy,
}, preData: null,
}); });
}); });