1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01: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({
method: 'post',
path: `${PREFIX}/:environment/default-strategy`,
handler: this.addDefaultStrategyToProjectEnvironment,
handler: this.updateDefaultStrategyForProjectEnvironment,
permission: UPDATE_PROJECT,
middleware: [
openApiService.validPath({
@ -110,7 +110,7 @@ export default class EnvironmentsController extends Controller {
operationId: 'addDefaultStrategyToProjectEnvironment',
summary: 'Set environment-default strategy',
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(
'createFeatureStrategySchema',
),
@ -161,14 +161,14 @@ export default class EnvironmentsController extends Controller {
res.status(200).end();
}
async addDefaultStrategyToProjectEnvironment(
async updateDefaultStrategyForProjectEnvironment(
req: Request<IProjectEnvironmentParams, CreateFeatureStrategySchema>,
res: Response<CreateFeatureStrategySchema>,
): Promise<void> {
const { projectId, environment } = req.params;
const strategy = req.body;
const saved = await this.environmentService.addDefaultStrategy(
const saved = await this.environmentService.updateDefaultStrategy(
environment,
projectId,
strategy,

View File

@ -1,4 +1,5 @@
import {
DEFAULT_STRATEGY_UPDATED,
IEnvironment,
IEnvironmentStore,
IFeatureEnvironmentStore,
@ -126,21 +127,34 @@ export default class EnvironmentService {
}
}
async addDefaultStrategy(
async updateDefaultStrategy(
environment: string,
projectId: string,
strategy: CreateFeatureStrategySchema,
username: string = 'unknown',
): Promise<CreateFeatureStrategySchema> {
if (strategy.name !== 'flexibleRollout') {
throw new BadDataError(
'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,
environment,
strategy,
);
await this.eventService.storeEvent({
type: DEFAULT_STRATEGY_UPDATED,
project: projectId,
environment,
createdBy: username,
preData: previousDefaultStrategy,
data: defaultStrategy,
});
return defaultStrategy;
}
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_REMOVED =
'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_REGISTER = 'client-register' as const;
@ -297,6 +298,7 @@ export const IEventTypes = [
BANNER_DELETED,
PROJECT_ENVIRONMENT_ADDED,
PROJECT_ENVIRONMENT_REMOVED,
DEFAULT_STRATEGY_UPDATED,
] as const;
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 () => {
const defaultStrategy = {
name: 'flexibleRollout',
constraints: [],
parameters: {
rollout: '50',
stickiness: 'customAppName',
groupId: 'stickytoggle',
},
};
await app.request
.post(
`/api/admin/projects/default/environments/default/default-strategy`,
)
.send({
name: 'flexibleRollout',
constraints: [],
parameters: {
rollout: '50',
stickiness: 'customAppName',
groupId: 'stickytoggle',
},
})
.send(defaultStrategy)
.expect(200);
const envs =
@ -134,15 +136,15 @@ test('Should add default strategy to environment', async () => {
expect(envs).toHaveLength(1);
expect(envs[0]).toStrictEqual({
environment: 'default',
defaultStrategy: {
name: 'flexibleRollout',
constraints: [],
parameters: {
rollout: '50',
stickiness: 'customAppName',
groupId: 'stickytoggle',
},
},
defaultStrategy,
});
const { body } = await app.getRecordedEvents();
expect(body.events[0]).toMatchObject({
type: 'default-strategy-updated',
project: 'default',
environment: 'default',
data: defaultStrategy,
preData: null,
});
});