mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	chore: register integration events in Slack integration (#7626)
https://linear.app/unleash/issue/2-2458/register-integration-events-slack Registers integration events in the **Slack** integration. Similar to: https://github.com/Unleash/unleash/pull/7621 Also slightly improves the previous work on webhooks.
This commit is contained in:
		
							parent
							
								
									87fa5a2414
								
							
						
					
					
						commit
						203b700e27
					
				| @ -1,16 +1,16 @@ | ||||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||||
| 
 | ||||
| exports[`Should call slack webhook 1`] = `"{"username":"Unleash","icon_emoji":":unleash:","text":"*some@user.com* created *<http://some-url.com/projects/default/features/some-toggle|some-toggle>* in project *<http://some-url.com/projects/default|default>*","channel":"#general","attachments":[{"actions":[{"name":"featureToggle","text":"Open in Unleash","type":"button","value":"featureToggle","style":"primary","url":"http://some-url.com/projects/default/features/some-toggle"}]}]}"`; | ||||
| exports[`Slack integration Should call slack webhook 1`] = `"{"username":"Unleash","icon_emoji":":unleash:","text":"*some@user.com* created *<http://some-url.com/projects/default/features/some-toggle|some-toggle>* in project *<http://some-url.com/projects/default|default>*","channel":"#general","attachments":[{"actions":[{"name":"featureToggle","text":"Open in Unleash","type":"button","value":"featureToggle","style":"primary","url":"http://some-url.com/projects/default/features/some-toggle"}]}]}"`; | ||||
| 
 | ||||
| exports[`Should call slack webhook for archived toggle 1`] = `"{"username":"Unleash","icon_emoji":":unleash:","text":"*some@user.com* archived *some-toggle* in project **","channel":"#general","attachments":[{"actions":[{"name":"featureToggle","text":"Open in Unleash","type":"button","value":"featureToggle","style":"primary","url":"http://some-url.com/projects//archive"}]}]}"`; | ||||
| exports[`Slack integration Should call slack webhook for archived toggle 1`] = `"{"username":"Unleash","icon_emoji":":unleash:","text":"*some@user.com* archived *some-toggle* in project **","channel":"#general","attachments":[{"actions":[{"name":"featureToggle","text":"Open in Unleash","type":"button","value":"featureToggle","style":"primary","url":"http://some-url.com/projects//archive"}]}]}"`; | ||||
| 
 | ||||
| exports[`Should call slack webhook for archived toggle with project info 1`] = `"{"username":"Unleash","icon_emoji":":unleash:","text":"*some@user.com* archived *some-toggle* in project *<http://some-url.com/projects/some-project|some-project>*","channel":"#general","attachments":[{"actions":[{"name":"featureToggle","text":"Open in Unleash","type":"button","value":"featureToggle","style":"primary","url":"http://some-url.com/projects/some-project/archive"}]}]}"`; | ||||
| exports[`Slack integration Should call slack webhook for archived toggle with project info 1`] = `"{"username":"Unleash","icon_emoji":":unleash:","text":"*some@user.com* archived *some-toggle* in project *<http://some-url.com/projects/some-project|some-project>*","channel":"#general","attachments":[{"actions":[{"name":"featureToggle","text":"Open in Unleash","type":"button","value":"featureToggle","style":"primary","url":"http://some-url.com/projects/some-project/archive"}]}]}"`; | ||||
| 
 | ||||
| exports[`Should call webhook for toggled environment 1`] = `"{"username":"Unleash","icon_emoji":":unleash:","text":"*some@user.com* disabled *<http://some-url.com/projects/default/features/some-toggle|some-toggle>* for the *development* environment in project *<http://some-url.com/projects/default|default>*","channel":"#general","attachments":[{"actions":[{"name":"featureToggle","text":"Open in Unleash","type":"button","value":"featureToggle","style":"primary","url":"http://some-url.com/projects/default/features/some-toggle"}]}]}"`; | ||||
| exports[`Slack integration Should call webhook for toggled environment 1`] = `"{"username":"Unleash","icon_emoji":":unleash:","text":"*some@user.com* disabled *<http://some-url.com/projects/default/features/some-toggle|some-toggle>* for the *development* environment in project *<http://some-url.com/projects/default|default>*","channel":"#general","attachments":[{"actions":[{"name":"featureToggle","text":"Open in Unleash","type":"button","value":"featureToggle","style":"primary","url":"http://some-url.com/projects/default/features/some-toggle"}]}]}"`; | ||||
| 
 | ||||
| exports[`Should include custom headers from parameters in call to service 1`] = `"{"username":"Unleash","icon_emoji":":unleash:","text":"*some@user.com* disabled *<http://some-url.com/projects/default/features/some-toggle|some-toggle>* for the *development* environment in project *<http://some-url.com/projects/default|default>*","channel":"#general","attachments":[{"actions":[{"name":"featureToggle","text":"Open in Unleash","type":"button","value":"featureToggle","style":"primary","url":"http://some-url.com/projects/default/features/some-toggle"}]}]}"`; | ||||
| exports[`Slack integration Should include custom headers from parameters in call to service 1`] = `"{"username":"Unleash","icon_emoji":":unleash:","text":"*some@user.com* disabled *<http://some-url.com/projects/default/features/some-toggle|some-toggle>* for the *development* environment in project *<http://some-url.com/projects/default|default>*","channel":"#general","attachments":[{"actions":[{"name":"featureToggle","text":"Open in Unleash","type":"button","value":"featureToggle","style":"primary","url":"http://some-url.com/projects/default/features/some-toggle"}]}]}"`; | ||||
| 
 | ||||
| exports[`Should include custom headers from parameters in call to service 2`] = ` | ||||
| exports[`Slack integration Should include custom headers from parameters in call to service 2`] = ` | ||||
| { | ||||
|   "Content-Type": "application/json", | ||||
|   "MY_CUSTOM_HEADER": "MY_CUSTOM_VALUE", | ||||
|  | ||||
| @ -12,11 +12,13 @@ import noLogger from '../../test/fixtures/no-logger'; | ||||
| import { | ||||
|     type IAddonConfig, | ||||
|     type IFlagResolver, | ||||
|     serializeDates, | ||||
|     SYSTEM_USER_ID, | ||||
| } from '../types'; | ||||
| import type { IntegrationEventsService } from '../services'; | ||||
| 
 | ||||
| let fetchRetryCalls: any[] = []; | ||||
| const registerEventMock = jest.fn(); | ||||
| 
 | ||||
| const INTEGRATION_ID = 1337; | ||||
| const ARGS: IAddonConfig = { | ||||
| @ -44,252 +46,298 @@ jest.mock( | ||||
|                     retries, | ||||
|                     backoff, | ||||
|                 }); | ||||
|                 return Promise.resolve({ status: 200 }); | ||||
|                 return Promise.resolve({ ok: true, status: 200 }); | ||||
|             } | ||||
| 
 | ||||
|             async registerEvent(_) { | ||||
|                 return Promise.resolve(); | ||||
|             async registerEvent(event) { | ||||
|                 return registerEventMock(event); | ||||
|             } | ||||
|         }, | ||||
| ); | ||||
| 
 | ||||
| test('Should call slack webhook', async () => { | ||||
|     const addon = new SlackAddon(ARGS); | ||||
|     const event: IEvent = { | ||||
|         id: 1, | ||||
|         createdAt: new Date(), | ||||
|         type: FEATURE_CREATED, | ||||
|         createdByUserId: SYSTEM_USER_ID, | ||||
|         createdBy: 'some@user.com', | ||||
|         project: 'default', | ||||
|         featureName: 'some-toggle', | ||||
|         data: { | ||||
|             name: 'some-toggle', | ||||
|             enabled: false, | ||||
|             type: 'release', | ||||
|             strategies: [{ name: 'default' }], | ||||
|         }, | ||||
|     }; | ||||
| describe('Slack integration', () => { | ||||
|     beforeEach(() => { | ||||
|         registerEventMock.mockClear(); | ||||
|     }); | ||||
| 
 | ||||
|     const parameters = { | ||||
|         url: 'http://hooks.slack.com', | ||||
|         defaultChannel: 'general', | ||||
|     }; | ||||
| 
 | ||||
|     await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
|     expect(fetchRetryCalls.length).toBe(1); | ||||
|     expect(fetchRetryCalls[0].url).toBe(parameters.url); | ||||
|     expect(fetchRetryCalls[0].options.body).toMatchSnapshot(); | ||||
| }); | ||||
| 
 | ||||
| test('Should call slack webhook for archived toggle', async () => { | ||||
|     const addon = new SlackAddon(ARGS); | ||||
|     const event: IEvent = { | ||||
|         id: 2, | ||||
|         createdAt: new Date(), | ||||
|         createdByUserId: SYSTEM_USER_ID, | ||||
|         type: FEATURE_ARCHIVED, | ||||
|         featureName: 'some-toggle', | ||||
|         createdBy: 'some@user.com', | ||||
|         data: { | ||||
|             name: 'some-toggle', | ||||
|         }, | ||||
|     }; | ||||
| 
 | ||||
|     const parameters = { | ||||
|         url: 'http://hooks.slack.com', | ||||
|         defaultChannel: 'general', | ||||
|     }; | ||||
| 
 | ||||
|     await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
|     expect(fetchRetryCalls.length).toBe(1); | ||||
|     expect(fetchRetryCalls[0].url).toBe(parameters.url); | ||||
|     expect(fetchRetryCalls[0].options.body).toMatchSnapshot(); | ||||
| }); | ||||
| 
 | ||||
| test('Should call slack webhook for archived toggle with project info', async () => { | ||||
|     const addon = new SlackAddon(ARGS); | ||||
|     const event: IEvent = { | ||||
|         id: 2, | ||||
|         createdAt: new Date(), | ||||
|         createdByUserId: SYSTEM_USER_ID, | ||||
|         type: FEATURE_ARCHIVED, | ||||
|         featureName: 'some-toggle', | ||||
|         project: 'some-project', | ||||
|         createdBy: 'some@user.com', | ||||
|         data: { | ||||
|             name: 'some-toggle', | ||||
|         }, | ||||
|     }; | ||||
| 
 | ||||
|     const parameters = { | ||||
|         url: 'http://hooks.slack.com', | ||||
|         defaultChannel: 'general', | ||||
|     }; | ||||
| 
 | ||||
|     await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
|     expect(fetchRetryCalls.length).toBe(1); | ||||
|     expect(fetchRetryCalls[0].url).toBe(parameters.url); | ||||
|     expect(fetchRetryCalls[0].options.body).toMatchSnapshot(); | ||||
| }); | ||||
| 
 | ||||
| test(`Should call webhook for toggled environment`, async () => { | ||||
|     const addon = new SlackAddon(ARGS); | ||||
|     const event: IEvent = { | ||||
|         id: 2, | ||||
|         createdAt: new Date(), | ||||
|         createdByUserId: SYSTEM_USER_ID, | ||||
|         type: FEATURE_ENVIRONMENT_DISABLED, | ||||
|         createdBy: 'some@user.com', | ||||
|         environment: 'development', | ||||
|         project: 'default', | ||||
|         featureName: 'some-toggle', | ||||
|         data: { | ||||
|             name: 'some-toggle', | ||||
|         }, | ||||
|     }; | ||||
| 
 | ||||
|     const parameters = { | ||||
|         url: 'http://hooks.slack.com', | ||||
|         defaultChannel: 'general', | ||||
|     }; | ||||
| 
 | ||||
|     await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
|     expect(fetchRetryCalls).toHaveLength(1); | ||||
|     expect(fetchRetryCalls[0].url).toBe(parameters.url); | ||||
|     expect(fetchRetryCalls[0].options.body).toMatch(/disabled/); | ||||
|     expect(fetchRetryCalls[0].options.body).toMatchSnapshot(); | ||||
| }); | ||||
| 
 | ||||
| test('Should use default channel', async () => { | ||||
|     const addon = new SlackAddon(ARGS); | ||||
|     const event: IEvent = { | ||||
|         id: 3, | ||||
|         createdAt: new Date(), | ||||
|         createdByUserId: SYSTEM_USER_ID, | ||||
|         type: FEATURE_CREATED, | ||||
|         createdBy: 'some@user.com', | ||||
|         featureName: 'some-toggle', | ||||
|         data: { | ||||
|             name: 'some-toggle', | ||||
|             enabled: false, | ||||
|             strategies: [{ name: 'default' }], | ||||
|         }, | ||||
|     }; | ||||
| 
 | ||||
|     const parameters = { | ||||
|         url: 'http://hooks.slack.com', | ||||
|         defaultChannel: 'some-channel', | ||||
|     }; | ||||
| 
 | ||||
|     await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
| 
 | ||||
|     const req = JSON.parse(fetchRetryCalls[0].options.body); | ||||
| 
 | ||||
|     expect(req.channel).toBe('#some-channel'); | ||||
| }); | ||||
| 
 | ||||
| test('Should override default channel with data from tag', async () => { | ||||
|     const addon = new SlackAddon(ARGS); | ||||
|     const event: IEvent = { | ||||
|         id: 4, | ||||
|         createdAt: new Date(), | ||||
|         createdByUserId: SYSTEM_USER_ID, | ||||
|         type: FEATURE_CREATED, | ||||
|         createdBy: 'some@user.com', | ||||
|         featureName: 'some-toggle', | ||||
|         data: { | ||||
|             name: 'some-toggle', | ||||
|             enabled: false, | ||||
|             strategies: [{ name: 'default' }], | ||||
|         }, | ||||
|         tags: [ | ||||
|             { | ||||
|                 type: 'slack', | ||||
|                 value: 'another-channel', | ||||
|     test('Should call slack webhook', async () => { | ||||
|         const addon = new SlackAddon(ARGS); | ||||
|         const event: IEvent = { | ||||
|             id: 1, | ||||
|             createdAt: new Date(), | ||||
|             type: FEATURE_CREATED, | ||||
|             createdByUserId: SYSTEM_USER_ID, | ||||
|             createdBy: 'some@user.com', | ||||
|             project: 'default', | ||||
|             featureName: 'some-toggle', | ||||
|             data: { | ||||
|                 name: 'some-toggle', | ||||
|                 enabled: false, | ||||
|                 type: 'release', | ||||
|                 strategies: [{ name: 'default' }], | ||||
|             }, | ||||
|         ], | ||||
|     }; | ||||
|         }; | ||||
| 
 | ||||
|     const parameters = { | ||||
|         url: 'http://hooks.slack.com', | ||||
|         defaultChannel: 'some-channel', | ||||
|     }; | ||||
|         const parameters = { | ||||
|             url: 'http://hooks.slack.com', | ||||
|             defaultChannel: 'general', | ||||
|         }; | ||||
| 
 | ||||
|     await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
|         await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
|         expect(fetchRetryCalls.length).toBe(1); | ||||
|         expect(fetchRetryCalls[0].url).toBe(parameters.url); | ||||
|         expect(fetchRetryCalls[0].options.body).toMatchSnapshot(); | ||||
|     }); | ||||
| 
 | ||||
|     const req = JSON.parse(fetchRetryCalls[0].options.body); | ||||
| 
 | ||||
|     expect(req.channel).toBe('#another-channel'); | ||||
| }); | ||||
| 
 | ||||
| test('Should post to all channels in tags', async () => { | ||||
|     const addon = new SlackAddon(ARGS); | ||||
|     const event: IEvent = { | ||||
|         id: 5, | ||||
|         createdAt: new Date(), | ||||
|         createdByUserId: SYSTEM_USER_ID, | ||||
|         type: FEATURE_CREATED, | ||||
|         createdBy: 'some@user.com', | ||||
|         featureName: 'some-toggle', | ||||
|         data: { | ||||
|             name: 'some-toggle', | ||||
|             enabled: false, | ||||
|             strategies: [{ name: 'default' }], | ||||
|         }, | ||||
|         tags: [ | ||||
|             { | ||||
|                 type: 'slack', | ||||
|                 value: 'another-channel-1', | ||||
|     test('Should call slack webhook for archived toggle', async () => { | ||||
|         const addon = new SlackAddon(ARGS); | ||||
|         const event: IEvent = { | ||||
|             id: 2, | ||||
|             createdAt: new Date(), | ||||
|             createdByUserId: SYSTEM_USER_ID, | ||||
|             type: FEATURE_ARCHIVED, | ||||
|             featureName: 'some-toggle', | ||||
|             createdBy: 'some@user.com', | ||||
|             data: { | ||||
|                 name: 'some-toggle', | ||||
|             }, | ||||
|             { | ||||
|                 type: 'slack', | ||||
|                 value: 'another-channel-2', | ||||
|         }; | ||||
| 
 | ||||
|         const parameters = { | ||||
|             url: 'http://hooks.slack.com', | ||||
|             defaultChannel: 'general', | ||||
|         }; | ||||
| 
 | ||||
|         await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
|         expect(fetchRetryCalls.length).toBe(1); | ||||
|         expect(fetchRetryCalls[0].url).toBe(parameters.url); | ||||
|         expect(fetchRetryCalls[0].options.body).toMatchSnapshot(); | ||||
|     }); | ||||
| 
 | ||||
|     test('Should call slack webhook for archived toggle with project info', async () => { | ||||
|         const addon = new SlackAddon(ARGS); | ||||
|         const event: IEvent = { | ||||
|             id: 2, | ||||
|             createdAt: new Date(), | ||||
|             createdByUserId: SYSTEM_USER_ID, | ||||
|             type: FEATURE_ARCHIVED, | ||||
|             featureName: 'some-toggle', | ||||
|             project: 'some-project', | ||||
|             createdBy: 'some@user.com', | ||||
|             data: { | ||||
|                 name: 'some-toggle', | ||||
|             }, | ||||
|         ], | ||||
|     }; | ||||
|         }; | ||||
| 
 | ||||
|     const parameters = { | ||||
|         url: 'http://hooks.slack.com', | ||||
|         defaultChannel: 'some-channel', | ||||
|     }; | ||||
|         const parameters = { | ||||
|             url: 'http://hooks.slack.com', | ||||
|             defaultChannel: 'general', | ||||
|         }; | ||||
| 
 | ||||
|     await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
|         await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
|         expect(fetchRetryCalls.length).toBe(1); | ||||
|         expect(fetchRetryCalls[0].url).toBe(parameters.url); | ||||
|         expect(fetchRetryCalls[0].options.body).toMatchSnapshot(); | ||||
|     }); | ||||
| 
 | ||||
|     const req1 = JSON.parse(fetchRetryCalls[0].options.body); | ||||
|     const req2 = JSON.parse(fetchRetryCalls[1].options.body); | ||||
|     test(`Should call webhook for toggled environment`, async () => { | ||||
|         const addon = new SlackAddon(ARGS); | ||||
|         const event: IEvent = { | ||||
|             id: 2, | ||||
|             createdAt: new Date(), | ||||
|             createdByUserId: SYSTEM_USER_ID, | ||||
|             type: FEATURE_ENVIRONMENT_DISABLED, | ||||
|             createdBy: 'some@user.com', | ||||
|             environment: 'development', | ||||
|             project: 'default', | ||||
|             featureName: 'some-toggle', | ||||
|             data: { | ||||
|                 name: 'some-toggle', | ||||
|             }, | ||||
|         }; | ||||
| 
 | ||||
|     expect(fetchRetryCalls).toHaveLength(2); | ||||
|     expect(req1.channel).toBe('#another-channel-1'); | ||||
|     expect(req2.channel).toBe('#another-channel-2'); | ||||
| }); | ||||
| 
 | ||||
| test('Should include custom headers from parameters in call to service', async () => { | ||||
|     const addon = new SlackAddon(ARGS); | ||||
|     const event: IEvent = { | ||||
|         id: 2, | ||||
|         createdAt: new Date(), | ||||
|         type: FEATURE_ENVIRONMENT_DISABLED, | ||||
|         createdByUserId: SYSTEM_USER_ID, | ||||
|         createdBy: 'some@user.com', | ||||
|         environment: 'development', | ||||
|         project: 'default', | ||||
|         featureName: 'some-toggle', | ||||
|         data: { | ||||
|             name: 'some-toggle', | ||||
|         }, | ||||
|     }; | ||||
| 
 | ||||
|     const parameters = { | ||||
|         url: 'http://hooks.slack.com', | ||||
|         defaultChannel: 'general', | ||||
|         customHeaders: `{ "MY_CUSTOM_HEADER": "MY_CUSTOM_VALUE" }`, | ||||
|     }; | ||||
| 
 | ||||
|     await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
|     expect(fetchRetryCalls).toHaveLength(1); | ||||
|     expect(fetchRetryCalls[0].url).toBe(parameters.url); | ||||
|     expect(fetchRetryCalls[0].options.body).toMatch(/disabled/); | ||||
|     expect(fetchRetryCalls[0].options.body).toMatchSnapshot(); | ||||
|     expect(fetchRetryCalls[0].options.headers).toMatchSnapshot(); | ||||
|         const parameters = { | ||||
|             url: 'http://hooks.slack.com', | ||||
|             defaultChannel: 'general', | ||||
|         }; | ||||
| 
 | ||||
|         await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
|         expect(fetchRetryCalls).toHaveLength(1); | ||||
|         expect(fetchRetryCalls[0].url).toBe(parameters.url); | ||||
|         expect(fetchRetryCalls[0].options.body).toMatch(/disabled/); | ||||
|         expect(fetchRetryCalls[0].options.body).toMatchSnapshot(); | ||||
|     }); | ||||
| 
 | ||||
|     test('Should use default channel', async () => { | ||||
|         const addon = new SlackAddon(ARGS); | ||||
|         const event: IEvent = { | ||||
|             id: 3, | ||||
|             createdAt: new Date(), | ||||
|             createdByUserId: SYSTEM_USER_ID, | ||||
|             type: FEATURE_CREATED, | ||||
|             createdBy: 'some@user.com', | ||||
|             featureName: 'some-toggle', | ||||
|             data: { | ||||
|                 name: 'some-toggle', | ||||
|                 enabled: false, | ||||
|                 strategies: [{ name: 'default' }], | ||||
|             }, | ||||
|         }; | ||||
| 
 | ||||
|         const parameters = { | ||||
|             url: 'http://hooks.slack.com', | ||||
|             defaultChannel: 'some-channel', | ||||
|         }; | ||||
| 
 | ||||
|         await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
| 
 | ||||
|         const req = JSON.parse(fetchRetryCalls[0].options.body); | ||||
| 
 | ||||
|         expect(req.channel).toBe('#some-channel'); | ||||
|     }); | ||||
| 
 | ||||
|     test('Should override default channel with data from tag', async () => { | ||||
|         const addon = new SlackAddon(ARGS); | ||||
|         const event: IEvent = { | ||||
|             id: 4, | ||||
|             createdAt: new Date(), | ||||
|             createdByUserId: SYSTEM_USER_ID, | ||||
|             type: FEATURE_CREATED, | ||||
|             createdBy: 'some@user.com', | ||||
|             featureName: 'some-toggle', | ||||
|             data: { | ||||
|                 name: 'some-toggle', | ||||
|                 enabled: false, | ||||
|                 strategies: [{ name: 'default' }], | ||||
|             }, | ||||
|             tags: [ | ||||
|                 { | ||||
|                     type: 'slack', | ||||
|                     value: 'another-channel', | ||||
|                 }, | ||||
|             ], | ||||
|         }; | ||||
| 
 | ||||
|         const parameters = { | ||||
|             url: 'http://hooks.slack.com', | ||||
|             defaultChannel: 'some-channel', | ||||
|         }; | ||||
| 
 | ||||
|         await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
| 
 | ||||
|         const req = JSON.parse(fetchRetryCalls[0].options.body); | ||||
| 
 | ||||
|         expect(req.channel).toBe('#another-channel'); | ||||
|     }); | ||||
| 
 | ||||
|     test('Should post to all channels in tags', async () => { | ||||
|         const addon = new SlackAddon(ARGS); | ||||
|         const event: IEvent = { | ||||
|             id: 5, | ||||
|             createdAt: new Date(), | ||||
|             createdByUserId: SYSTEM_USER_ID, | ||||
|             type: FEATURE_CREATED, | ||||
|             createdBy: 'some@user.com', | ||||
|             featureName: 'some-toggle', | ||||
|             data: { | ||||
|                 name: 'some-toggle', | ||||
|                 enabled: false, | ||||
|                 strategies: [{ name: 'default' }], | ||||
|             }, | ||||
|             tags: [ | ||||
|                 { | ||||
|                     type: 'slack', | ||||
|                     value: 'another-channel-1', | ||||
|                 }, | ||||
|                 { | ||||
|                     type: 'slack', | ||||
|                     value: 'another-channel-2', | ||||
|                 }, | ||||
|             ], | ||||
|         }; | ||||
| 
 | ||||
|         const parameters = { | ||||
|             url: 'http://hooks.slack.com', | ||||
|             defaultChannel: 'some-channel', | ||||
|         }; | ||||
| 
 | ||||
|         await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
| 
 | ||||
|         const req1 = JSON.parse(fetchRetryCalls[0].options.body); | ||||
|         const req2 = JSON.parse(fetchRetryCalls[1].options.body); | ||||
| 
 | ||||
|         expect(fetchRetryCalls).toHaveLength(2); | ||||
|         expect(req1.channel).toBe('#another-channel-1'); | ||||
|         expect(req2.channel).toBe('#another-channel-2'); | ||||
|     }); | ||||
| 
 | ||||
|     test('Should include custom headers from parameters in call to service', async () => { | ||||
|         const addon = new SlackAddon(ARGS); | ||||
|         const event: IEvent = { | ||||
|             id: 2, | ||||
|             createdAt: new Date(), | ||||
|             type: FEATURE_ENVIRONMENT_DISABLED, | ||||
|             createdByUserId: SYSTEM_USER_ID, | ||||
|             createdBy: 'some@user.com', | ||||
|             environment: 'development', | ||||
|             project: 'default', | ||||
|             featureName: 'some-toggle', | ||||
|             data: { | ||||
|                 name: 'some-toggle', | ||||
|             }, | ||||
|         }; | ||||
| 
 | ||||
|         const parameters = { | ||||
|             url: 'http://hooks.slack.com', | ||||
|             defaultChannel: 'general', | ||||
|             customHeaders: `{ "MY_CUSTOM_HEADER": "MY_CUSTOM_VALUE" }`, | ||||
|         }; | ||||
| 
 | ||||
|         await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
|         expect(fetchRetryCalls).toHaveLength(1); | ||||
|         expect(fetchRetryCalls[0].url).toBe(parameters.url); | ||||
|         expect(fetchRetryCalls[0].options.body).toMatch(/disabled/); | ||||
|         expect(fetchRetryCalls[0].options.body).toMatchSnapshot(); | ||||
|         expect(fetchRetryCalls[0].options.headers).toMatchSnapshot(); | ||||
|     }); | ||||
| 
 | ||||
|     test('Should call registerEvent', async () => { | ||||
|         const addon = new SlackAddon(ARGS); | ||||
|         const event: IEvent = { | ||||
|             id: 2, | ||||
|             createdAt: new Date(), | ||||
|             type: FEATURE_ENVIRONMENT_DISABLED, | ||||
|             createdByUserId: SYSTEM_USER_ID, | ||||
|             createdBy: 'some@user.com', | ||||
|             environment: 'development', | ||||
|             project: 'default', | ||||
|             featureName: 'some-toggle', | ||||
|             data: { | ||||
|                 name: 'some-toggle', | ||||
|             }, | ||||
|         }; | ||||
| 
 | ||||
|         const parameters = { | ||||
|             url: 'http://hooks.slack.com', | ||||
|             defaultChannel: 'general', | ||||
|             customHeaders: `{ "MY_CUSTOM_HEADER": "MY_CUSTOM_VALUE" }`, | ||||
|         }; | ||||
| 
 | ||||
|         await addon.handleEvent(event, parameters, INTEGRATION_ID); | ||||
| 
 | ||||
|         expect(registerEventMock).toHaveBeenCalledTimes(1); | ||||
|         expect(registerEventMock).toHaveBeenCalledWith({ | ||||
|             integrationId: INTEGRATION_ID, | ||||
|             state: 'success', | ||||
|             stateDetails: | ||||
|                 'All (1) Slack webhook requests were successful with status codes: 200.', | ||||
|             event: serializeDates(event), | ||||
|             details: { | ||||
|                 url: parameters.url, | ||||
|                 channels: ['general'], | ||||
|                 username: 'Unleash', | ||||
|                 text: '*some@user.com* disabled *<http://some-url.com/projects/default/features/some-toggle|some-toggle>* for the *development* environment in project *<http://some-url.com/projects/default|default>*', | ||||
|             }, | ||||
|         }); | ||||
|     }); | ||||
| }); | ||||
|  | ||||
| @ -1,7 +1,7 @@ | ||||
| import Addon from './addon'; | ||||
| 
 | ||||
| import slackDefinition from './slack-definition'; | ||||
| import type { IAddonConfig } from '../types/model'; | ||||
| import { type IAddonConfig, serializeDates } from '../types'; | ||||
| 
 | ||||
| import { | ||||
|     type FeatureEventFormatter, | ||||
| @ -9,6 +9,7 @@ import { | ||||
|     LinkStyle, | ||||
| } from './feature-event-formatter-md'; | ||||
| import type { IEvent } from '../types/events'; | ||||
| import type { IntegrationEventState } from '../features/integration-events/integration-events-store'; | ||||
| 
 | ||||
| interface ISlackAddonParameters { | ||||
|     url: string; | ||||
| @ -34,6 +35,9 @@ export default class SlackAddon extends Addon { | ||||
|         parameters: ISlackAddonParameters, | ||||
|         integrationId: number, | ||||
|     ): Promise<void> { | ||||
|         let state: IntegrationEventState = 'success'; | ||||
|         const stateDetails: string[] = []; | ||||
| 
 | ||||
|         const { | ||||
|             url, | ||||
|             defaultChannel, | ||||
| @ -48,6 +52,19 @@ export default class SlackAddon extends Addon { | ||||
|             slackChannels.push(defaultChannel); | ||||
|         } | ||||
| 
 | ||||
|         let extraHeaders = {}; | ||||
|         if (typeof customHeaders === 'string' && customHeaders.length > 1) { | ||||
|             try { | ||||
|                 extraHeaders = JSON.parse(customHeaders); | ||||
|             } catch (e) { | ||||
|                 const detailMessage = | ||||
|                     'Could not parse the JSON in the customHeaders parameter.'; | ||||
|                 state = 'successWithErrors'; | ||||
|                 stateDetails.push(detailMessage); | ||||
|                 this.logger.warn(detailMessage); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         const { text, url: featureLink } = this.msgFormatter.format(event); | ||||
| 
 | ||||
|         const requests = slackChannels.map((channel) => { | ||||
| @ -71,16 +88,6 @@ export default class SlackAddon extends Addon { | ||||
|                     }, | ||||
|                 ], | ||||
|             }; | ||||
|             let extraHeaders = {}; | ||||
|             if (typeof customHeaders === 'string' && customHeaders.length > 1) { | ||||
|                 try { | ||||
|                     extraHeaders = JSON.parse(customHeaders); | ||||
|                 } catch (e) { | ||||
|                     this.logger.warn( | ||||
|                         `Could not parse the json in the customHeaders parameter. [${customHeaders}]`, | ||||
|                     ); | ||||
|                 } | ||||
|             } | ||||
|             const requestOpts = { | ||||
|                 method: 'POST', | ||||
|                 headers: { | ||||
| @ -94,8 +101,45 @@ export default class SlackAddon extends Addon { | ||||
|         }); | ||||
| 
 | ||||
|         const results = await Promise.all(requests); | ||||
|         const codes = results.map((res) => res.status).join(', '); | ||||
|         this.logger.info(`Handled event ${event.type}. Status codes=${codes}`); | ||||
|         const failedRequests = results.filter((res) => !res.ok); | ||||
|         const codes = this.getUniqueArray( | ||||
|             results.map((res) => res.status), | ||||
|         ).join(', '); | ||||
| 
 | ||||
|         this.logger.info(`Handled event ${event.type}.`); | ||||
| 
 | ||||
|         if (failedRequests.length === 0) { | ||||
|             const detailMessage = `All (${results.length}) Slack webhook requests were successful with status codes: ${codes}.`; | ||||
|             stateDetails.push(detailMessage); | ||||
|             this.logger.info(detailMessage); | ||||
|         } else if (failedRequests.length === results.length) { | ||||
|             const detailMessage = `All (${results.length}) Slack webhook requests failed with status codes: ${codes}.`; | ||||
|             state = 'failed'; | ||||
|             stateDetails.push(detailMessage); | ||||
|             this.logger.warn(detailMessage); | ||||
|         } else { | ||||
|             const detailMessage = `Some (${failedRequests.length} of ${results.length}) Slack webhook requests failed. Status codes: ${codes}.`; | ||||
|             state = 'successWithErrors'; | ||||
|             stateDetails.push(detailMessage); | ||||
|             this.logger.warn(detailMessage); | ||||
|         } | ||||
| 
 | ||||
|         this.registerEvent({ | ||||
|             integrationId, | ||||
|             state, | ||||
|             stateDetails: stateDetails.join('\n'), | ||||
|             event: serializeDates(event), | ||||
|             details: { | ||||
|                 url, | ||||
|                 channels: slackChannels, | ||||
|                 username, | ||||
|                 text, | ||||
|             }, | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     getUniqueArray<T>(arr: T[]): T[] { | ||||
|         return [...new Set(arr)]; | ||||
|     } | ||||
| 
 | ||||
|     findSlackChannels({ tags }: Pick<IEvent, 'tags'>): string[] { | ||||
|  | ||||
| @ -211,7 +211,7 @@ describe('Webhook integration', () => { | ||||
|             integrationId: INTEGRATION_ID, | ||||
|             state: 'success', | ||||
|             stateDetails: | ||||
|                 'Webhook request was successful with status code: 200', | ||||
|                 'Webhook request was successful with status code: 200.', | ||||
|             event: serializeDates(event), | ||||
|             details: { | ||||
|                 url: parameters.url, | ||||
|  | ||||
| @ -24,7 +24,7 @@ export default class Webhook extends Addon { | ||||
|         integrationId: number, | ||||
|     ): Promise<void> { | ||||
|         let state: IntegrationEventState = 'success'; | ||||
|         let stateDetails = ''; | ||||
|         const stateDetails: string[] = []; | ||||
| 
 | ||||
|         const { url, bodyTemplate, contentType, authorization, customHeaders } = | ||||
|             parameters; | ||||
| @ -47,10 +47,11 @@ export default class Webhook extends Addon { | ||||
|             try { | ||||
|                 extraHeaders = JSON.parse(customHeaders); | ||||
|             } catch (e) { | ||||
|                 state = 'successWithErrors'; | ||||
|                 stateDetails = | ||||
|                 const detailMessage = | ||||
|                     'Could not parse the JSON in the customHeaders parameter.'; | ||||
|                 this.logger.warn(stateDetails); | ||||
|                 state = 'successWithErrors'; | ||||
|                 stateDetails.push(detailMessage); | ||||
|                 this.logger.warn(detailMessage); | ||||
|             } | ||||
|         } | ||||
|         const requestOpts = { | ||||
| @ -64,21 +65,23 @@ export default class Webhook extends Addon { | ||||
|         }; | ||||
|         const res = await this.fetchRetry(url, requestOpts); | ||||
| 
 | ||||
|         this.logger.info( | ||||
|             `Handled event "${event.type}". Status code: ${res.status}`, | ||||
|         ); | ||||
|         this.logger.info(`Handled event "${event.type}".`); | ||||
| 
 | ||||
|         if (res.ok) { | ||||
|             stateDetails = `Webhook request was successful with status code: ${res.status}`; | ||||
|             const detailMessage = `Webhook request was successful with status code: ${res.status}.`; | ||||
|             stateDetails.push(detailMessage); | ||||
|             this.logger.info(detailMessage); | ||||
|         } else { | ||||
|             const detailMessage = `Webhook request failed with status code: ${res.status}.`; | ||||
|             state = 'failed'; | ||||
|             stateDetails = `Webhook request failed with status code: ${res.status}`; | ||||
|             stateDetails.push(detailMessage); | ||||
|             this.logger.warn(detailMessage); | ||||
|         } | ||||
| 
 | ||||
|         this.registerEvent({ | ||||
|             integrationId, | ||||
|             state, | ||||
|             stateDetails, | ||||
|             stateDetails: stateDetails.join('\n'), | ||||
|             event: serializeDates(event), | ||||
|             details: { | ||||
|                 url, | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user