-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[ResponseOps][Slack] Simplify channel configuration #245423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
de5cfc5
663a4db
26bde28
dfac6b9
105ea12
e563094
2248a90
a5d6501
28901a0
5b3b943
86157d1
8af968e
6492d51
a2f937c
a198fe3
ee6838e
1709280
c21642e
233d53e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { TypeRegistry } from '@kbn/triggers-actions-ui-plugin/public/application/type_registry'; | ||
| import type { ActionTypeModel as ConnectorTypeModel } from '@kbn/triggers-actions-ui-plugin/public/types'; | ||
| import { registerConnectorTypes } from '..'; | ||
| import { experimentalFeaturesMock, registrationServicesMock } from '../../mocks'; | ||
| import { CONNECTOR_ID } from '@kbn/connector-schemas/slack_api/constants'; | ||
| import { ExperimentalFeaturesService } from '../../common/experimental_features_service'; | ||
|
|
||
| let connectorTypeModel: ConnectorTypeModel; | ||
|
|
||
| beforeAll(async () => { | ||
| const connectorTypeRegistry = new TypeRegistry<ConnectorTypeModel>(); | ||
| ExperimentalFeaturesService.init({ experimentalFeatures: experimentalFeaturesMock }); | ||
| registerConnectorTypes({ connectorTypeRegistry, services: registrationServicesMock }); | ||
|
|
||
| const getResult = connectorTypeRegistry.get(CONNECTOR_ID); | ||
|
|
||
| if (getResult !== null) { | ||
| connectorTypeModel = getResult; | ||
| } | ||
| }); | ||
|
|
||
| describe('deserializer', () => { | ||
| it('should deserialize the config as expected', () => { | ||
| const data = { | ||
| id: '1', | ||
| name: 'slack api', | ||
| isPreconfigured: false, | ||
| isDeprecated: false, | ||
| isSystemAction: false, | ||
| actionTypeId: CONNECTOR_ID, | ||
| config: { allowedChannels: [{ name: 'general' }, { name: '#random' }] }, | ||
| secrets: {}, | ||
| }; | ||
|
|
||
| const deserializer = connectorTypeModel.connectorForm?.deserializer!; | ||
| const result = deserializer(data); | ||
|
|
||
| expect(result).toEqual({ | ||
| ...data, | ||
| config: { | ||
| ...data.config, | ||
| allowedChannels: ['#general', '#random'], | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import type { ConnectorFormSchema, InternalConnectorForm } from '@kbn/alerts-ui-shared'; | ||
| import type { SlackApiConfig } from '@kbn/connector-schemas/slack_api'; | ||
|
|
||
| export const deserializer = (data: ConnectorFormSchema): InternalConnectorForm => { | ||
| const allowedChannels = (data.config?.allowedChannels as SlackApiConfig['allowedChannels']) ?? []; | ||
|
|
||
| const formattedChannels = | ||
| allowedChannels.map((channel) => { | ||
| if (channel.name.startsWith('#')) { | ||
| return channel.name; | ||
| } | ||
|
|
||
| return `#${channel.name}`; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how can this happen? Did we miss a schema validation to check that channel.name starts with # ?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old code always put names without |
||
| }) ?? []; | ||
|
|
||
| return { | ||
| ...data, | ||
| config: { ...data.config, allowedChannels: formattedChannels }, | ||
| }; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { TypeRegistry } from '@kbn/triggers-actions-ui-plugin/public/application/type_registry'; | ||
| import type { ActionTypeModel as ConnectorTypeModel } from '@kbn/triggers-actions-ui-plugin/public/types'; | ||
| import { registerConnectorTypes } from '..'; | ||
| import { experimentalFeaturesMock, registrationServicesMock } from '../../mocks'; | ||
| import { CONNECTOR_ID } from '@kbn/connector-schemas/slack_api/constants'; | ||
| import { ExperimentalFeaturesService } from '../../common/experimental_features_service'; | ||
|
|
||
| let connectorTypeModel: ConnectorTypeModel; | ||
|
|
||
| beforeAll(async () => { | ||
| const connectorTypeRegistry = new TypeRegistry<ConnectorTypeModel>(); | ||
| ExperimentalFeaturesService.init({ experimentalFeatures: experimentalFeaturesMock }); | ||
| registerConnectorTypes({ connectorTypeRegistry, services: registrationServicesMock }); | ||
|
|
||
| const getResult = connectorTypeRegistry.get(CONNECTOR_ID); | ||
|
|
||
| if (getResult !== null) { | ||
| connectorTypeModel = getResult; | ||
| } | ||
| }); | ||
|
|
||
| describe('serializer', () => { | ||
| it('should serialize the config as expected', () => { | ||
| const data = { | ||
| id: '1', | ||
| name: 'slack api', | ||
| isPreconfigured: false, | ||
| isDeprecated: false, | ||
| isSystemAction: false, | ||
| actionTypeId: CONNECTOR_ID, | ||
| config: { allowedChannels: ['#general', '#random'] }, | ||
| secrets: {}, | ||
| }; | ||
|
|
||
| const serializer = connectorTypeModel.connectorForm?.serializer!; | ||
|
|
||
| const result = serializer(data); | ||
|
|
||
| expect(result).toEqual({ | ||
| ...data, | ||
| config: { | ||
| ...data.config, | ||
| allowedChannels: [{ name: '#general' }, { name: '#random' }], | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import type { ConnectorFormSchema, InternalConnectorForm } from '@kbn/alerts-ui-shared'; | ||
|
|
||
| export const serializer = (data: InternalConnectorForm): ConnectorFormSchema => { | ||
| const formAllowedChannels = (data.config?.allowedChannels as string[]) ?? []; | ||
| const allowedChannels = formAllowedChannels.map((option) => ({ name: option })) ?? []; | ||
|
|
||
| return { | ||
| ...data, | ||
| config: { ...data.config, allowedChannels }, | ||
| }; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The casting is needed because the TS types do not support templates, as they assume the
datais the same for all connectors. I opened this issue: #246390.