Skip to content

Commit 9c97aaa

Browse files
committed
Move chat flag evaluations to the route handler
1 parent 25afa70 commit 9c97aaa

3 files changed

Lines changed: 14 additions & 41 deletions

File tree

x-pack/plugins/cloud_integrations/cloud_chat/server/plugin.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { PluginInitializerContext, CoreSetup, Plugin } from '@kbn/core/server';
1010
import type { CloudSetup } from '@kbn/cloud-plugin/server';
1111
import { registerChatRoute } from './routes';
1212
import type { CloudChatConfigType } from './config';
13-
import type { ChatVariant } from '../common/types';
1413

1514
interface CloudChatSetupDeps {
1615
cloud: CloudSetup;
@@ -36,16 +35,6 @@ export class CloudChatPlugin implements Plugin<void, void, CloudChatSetupDeps> {
3635
trialEndDate,
3736
trialBuffer,
3837
isDev: this.isDev,
39-
getChatVariant: () =>
40-
core
41-
.getStartServices()
42-
.then(([{ featureFlags }]) =>
43-
featureFlags.getStringValue<ChatVariant>('cloud-chat.chat-variant', 'header')
44-
),
45-
getChatDisabledThroughExperiments: () =>
46-
core
47-
.getStartServices()
48-
.then(([{ featureFlags }]) => featureFlags.getBooleanValue('cloud-chat.enabled', true)),
4938
});
5039
}
5140
}

x-pack/plugins/cloud_integrations/cloud_chat/server/routes/chat.test.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@ import {
1616
httpServerMock,
1717
coreMock,
1818
securityServiceMock,
19+
coreFeatureFlagsMock,
1920
} from '@kbn/core/server/mocks';
2021
import { kibanaResponseFactory } from '@kbn/core/server';
2122
import { type MetaWithSaml, registerChatRoute } from './chat';
22-
import { ChatVariant } from '../../common/types';
2323

2424
describe('chat route', () => {
25-
const getChatVariant = async (): Promise<ChatVariant> => 'header';
26-
const getChatDisabledThroughExperiments = async (): Promise<boolean> => false;
2725
let security: ReturnType<typeof securityServiceMock.createRequestHandlerContext>;
2826
let requestHandlerContextMock: ReturnType<typeof coreMock.createCustomRequestHandlerContext>;
27+
let featureFlags: ReturnType<typeof coreFeatureFlagsMock.createRequestHandlerContext>;
2928

3029
beforeEach(() => {
3130
const core = coreMock.createRequestHandlerContext();
3231
security = core.security;
32+
featureFlags = core.featureFlags;
33+
featureFlags.getStringValue.mockResolvedValue('header');
34+
featureFlags.getBooleanValue.mockResolvedValue(true);
3335
requestHandlerContextMock = coreMock.createCustomRequestHandlerContext({ core });
3436
});
3537

@@ -43,8 +45,6 @@ describe('chat route', () => {
4345
chatIdentitySecret: 'secret',
4446
trialBuffer: 60,
4547
trialEndDate: new Date(),
46-
getChatVariant,
47-
getChatDisabledThroughExperiments,
4848
});
4949

5050
const [_config, handler] = router.get.mock.calls[0];
@@ -78,8 +78,6 @@ describe('chat route', () => {
7878
chatIdentitySecret: 'secret',
7979
trialBuffer: 60,
8080
trialEndDate: new Date(),
81-
getChatVariant,
82-
getChatDisabledThroughExperiments,
8381
});
8482

8583
const [_config, handler] = router.get.mock.calls[0];
@@ -120,8 +118,6 @@ describe('chat route', () => {
120118
isDev: false,
121119
chatIdentitySecret: 'secret',
122120
trialBuffer: 2,
123-
getChatVariant,
124-
getChatDisabledThroughExperiments,
125121
});
126122

127123
const [_config, handler] = router.get.mock.calls[0];
@@ -165,8 +161,6 @@ describe('chat route', () => {
165161
chatIdentitySecret: 'secret',
166162
trialBuffer: 2,
167163
trialEndDate,
168-
getChatVariant,
169-
getChatDisabledThroughExperiments,
170164
});
171165

172166
const [_config, handler] = router.get.mock.calls[0];
@@ -202,14 +196,13 @@ describe('chat route', () => {
202196
);
203197

204198
const router = httpServiceMock.createRouter();
199+
featureFlags.getBooleanValue.mockResolvedValueOnce(false);
205200
registerChatRoute({
206201
router,
207202
isDev: false,
208203
chatIdentitySecret: 'secret',
209204
trialBuffer: 60,
210205
trialEndDate: new Date(),
211-
getChatVariant,
212-
getChatDisabledThroughExperiments: async () => true,
213206
});
214207
const [_config, handler] = router.get.mock.calls[0];
215208
await expect(
@@ -249,8 +242,6 @@ describe('chat route', () => {
249242
chatIdentitySecret: 'secret',
250243
trialBuffer: 60,
251244
trialEndDate: new Date(),
252-
getChatVariant,
253-
getChatDisabledThroughExperiments,
254245
});
255246
const [_config, handler] = router.get.mock.calls[0];
256247
await expect(
@@ -297,8 +288,6 @@ describe('chat route', () => {
297288
chatIdentitySecret: 'secret',
298289
trialBuffer: 60,
299290
trialEndDate: new Date(),
300-
getChatVariant,
301-
getChatDisabledThroughExperiments,
302291
});
303292
const [_config, handler] = router.get.mock.calls[0];
304293
await expect(
@@ -342,14 +331,13 @@ describe('chat route', () => {
342331
);
343332

344333
const router = httpServiceMock.createRouter();
334+
featureFlags.getStringValue.mockResolvedValueOnce('bubble');
345335
registerChatRoute({
346336
router,
347337
isDev: false,
348338
chatIdentitySecret: 'secret',
349339
trialBuffer: 60,
350340
trialEndDate: new Date(),
351-
getChatVariant: async () => 'bubble',
352-
getChatDisabledThroughExperiments,
353341
});
354342
const [_config, handler] = router.get.mock.calls[0];
355343
await expect(

x-pack/plugins/cloud_integrations/cloud_chat/server/routes/chat.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,20 @@ export const registerChatRoute = ({
2424
trialEndDate,
2525
trialBuffer,
2626
isDev,
27-
getChatVariant,
28-
getChatDisabledThroughExperiments,
2927
}: {
3028
router: IRouter;
3129
chatIdentitySecret: string;
3230
trialEndDate?: Date;
3331
trialBuffer: number;
3432
isDev: boolean;
35-
getChatVariant: () => Promise<ChatVariant>;
36-
/**
37-
* Returns true if chat is disabled in LaunchDarkly
38-
* Meant to be used as a runtime kill switch
39-
*/
40-
getChatDisabledThroughExperiments: () => Promise<boolean>;
4133
}) => {
4234
router.get(
4335
{
4436
path: GET_CHAT_USER_DATA_ROUTE_PATH,
4537
validate: {},
4638
},
4739
async (context, request, response) => {
48-
const { security } = await context.core;
40+
const { security, featureFlags } = await context.core;
4941
const user = security.authc.getCurrentUser();
5042

5143
if (!user) {
@@ -85,7 +77,8 @@ export const registerChatRoute = ({
8577
});
8678
}
8779

88-
if (await getChatDisabledThroughExperiments()) {
80+
// Meant to be used as a runtime kill switch via LaunchDarkly
81+
if (!(await featureFlags.getBooleanValue('cloud-chat.enabled', true).catch(() => false))) {
8982
return response.badRequest({
9083
body: 'Chat is disabled through experiments',
9184
});
@@ -96,7 +89,10 @@ export const registerChatRoute = ({
9689
token,
9790
email: userEmail,
9891
id: userId,
99-
chatVariant: await getChatVariant(),
92+
chatVariant: await featureFlags.getStringValue<ChatVariant>(
93+
'cloud-chat.chat-variant',
94+
'header'
95+
),
10096
};
10197
return response.ok({ body });
10298
}

0 commit comments

Comments
 (0)