mirror of
https://github.com/Unleash/unleash.git
synced 2025-06-23 01:16:27 +02: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
|
// 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",
|
"Content-Type": "application/json",
|
||||||
"MY_CUSTOM_HEADER": "MY_CUSTOM_VALUE",
|
"MY_CUSTOM_HEADER": "MY_CUSTOM_VALUE",
|
||||||
|
@ -12,11 +12,13 @@ import noLogger from '../../test/fixtures/no-logger';
|
|||||||
import {
|
import {
|
||||||
type IAddonConfig,
|
type IAddonConfig,
|
||||||
type IFlagResolver,
|
type IFlagResolver,
|
||||||
|
serializeDates,
|
||||||
SYSTEM_USER_ID,
|
SYSTEM_USER_ID,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
import type { IntegrationEventsService } from '../services';
|
import type { IntegrationEventsService } from '../services';
|
||||||
|
|
||||||
let fetchRetryCalls: any[] = [];
|
let fetchRetryCalls: any[] = [];
|
||||||
|
const registerEventMock = jest.fn();
|
||||||
|
|
||||||
const INTEGRATION_ID = 1337;
|
const INTEGRATION_ID = 1337;
|
||||||
const ARGS: IAddonConfig = {
|
const ARGS: IAddonConfig = {
|
||||||
@ -44,15 +46,20 @@ jest.mock(
|
|||||||
retries,
|
retries,
|
||||||
backoff,
|
backoff,
|
||||||
});
|
});
|
||||||
return Promise.resolve({ status: 200 });
|
return Promise.resolve({ ok: true, status: 200 });
|
||||||
}
|
}
|
||||||
|
|
||||||
async registerEvent(_) {
|
async registerEvent(event) {
|
||||||
return Promise.resolve();
|
return registerEventMock(event);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
describe('Slack integration', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
registerEventMock.mockClear();
|
||||||
|
});
|
||||||
|
|
||||||
test('Should call slack webhook', async () => {
|
test('Should call slack webhook', async () => {
|
||||||
const addon = new SlackAddon(ARGS);
|
const addon = new SlackAddon(ARGS);
|
||||||
const event: IEvent = {
|
const event: IEvent = {
|
||||||
@ -293,3 +300,44 @@ test('Should include custom headers from parameters in call to service', async (
|
|||||||
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
||||||
expect(fetchRetryCalls[0].options.headers).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 Addon from './addon';
|
||||||
|
|
||||||
import slackDefinition from './slack-definition';
|
import slackDefinition from './slack-definition';
|
||||||
import type { IAddonConfig } from '../types/model';
|
import { type IAddonConfig, serializeDates } from '../types';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
type FeatureEventFormatter,
|
type FeatureEventFormatter,
|
||||||
@ -9,6 +9,7 @@ import {
|
|||||||
LinkStyle,
|
LinkStyle,
|
||||||
} from './feature-event-formatter-md';
|
} from './feature-event-formatter-md';
|
||||||
import type { IEvent } from '../types/events';
|
import type { IEvent } from '../types/events';
|
||||||
|
import type { IntegrationEventState } from '../features/integration-events/integration-events-store';
|
||||||
|
|
||||||
interface ISlackAddonParameters {
|
interface ISlackAddonParameters {
|
||||||
url: string;
|
url: string;
|
||||||
@ -34,6 +35,9 @@ export default class SlackAddon extends Addon {
|
|||||||
parameters: ISlackAddonParameters,
|
parameters: ISlackAddonParameters,
|
||||||
integrationId: number,
|
integrationId: number,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
let state: IntegrationEventState = 'success';
|
||||||
|
const stateDetails: string[] = [];
|
||||||
|
|
||||||
const {
|
const {
|
||||||
url,
|
url,
|
||||||
defaultChannel,
|
defaultChannel,
|
||||||
@ -48,6 +52,19 @@ export default class SlackAddon extends Addon {
|
|||||||
slackChannels.push(defaultChannel);
|
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 { text, url: featureLink } = this.msgFormatter.format(event);
|
||||||
|
|
||||||
const requests = slackChannels.map((channel) => {
|
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 = {
|
const requestOpts = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@ -94,8 +101,45 @@ export default class SlackAddon extends Addon {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const results = await Promise.all(requests);
|
const results = await Promise.all(requests);
|
||||||
const codes = results.map((res) => res.status).join(', ');
|
const failedRequests = results.filter((res) => !res.ok);
|
||||||
this.logger.info(`Handled event ${event.type}. Status codes=${codes}`);
|
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[] {
|
findSlackChannels({ tags }: Pick<IEvent, 'tags'>): string[] {
|
||||||
|
@ -211,7 +211,7 @@ describe('Webhook integration', () => {
|
|||||||
integrationId: INTEGRATION_ID,
|
integrationId: INTEGRATION_ID,
|
||||||
state: 'success',
|
state: 'success',
|
||||||
stateDetails:
|
stateDetails:
|
||||||
'Webhook request was successful with status code: 200',
|
'Webhook request was successful with status code: 200.',
|
||||||
event: serializeDates(event),
|
event: serializeDates(event),
|
||||||
details: {
|
details: {
|
||||||
url: parameters.url,
|
url: parameters.url,
|
||||||
|
@ -24,7 +24,7 @@ export default class Webhook extends Addon {
|
|||||||
integrationId: number,
|
integrationId: number,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
let state: IntegrationEventState = 'success';
|
let state: IntegrationEventState = 'success';
|
||||||
let stateDetails = '';
|
const stateDetails: string[] = [];
|
||||||
|
|
||||||
const { url, bodyTemplate, contentType, authorization, customHeaders } =
|
const { url, bodyTemplate, contentType, authorization, customHeaders } =
|
||||||
parameters;
|
parameters;
|
||||||
@ -47,10 +47,11 @@ export default class Webhook extends Addon {
|
|||||||
try {
|
try {
|
||||||
extraHeaders = JSON.parse(customHeaders);
|
extraHeaders = JSON.parse(customHeaders);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
state = 'successWithErrors';
|
const detailMessage =
|
||||||
stateDetails =
|
|
||||||
'Could not parse the JSON in the customHeaders parameter.';
|
'Could not parse the JSON in the customHeaders parameter.';
|
||||||
this.logger.warn(stateDetails);
|
state = 'successWithErrors';
|
||||||
|
stateDetails.push(detailMessage);
|
||||||
|
this.logger.warn(detailMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const requestOpts = {
|
const requestOpts = {
|
||||||
@ -64,21 +65,23 @@ export default class Webhook extends Addon {
|
|||||||
};
|
};
|
||||||
const res = await this.fetchRetry(url, requestOpts);
|
const res = await this.fetchRetry(url, requestOpts);
|
||||||
|
|
||||||
this.logger.info(
|
this.logger.info(`Handled event "${event.type}".`);
|
||||||
`Handled event "${event.type}". Status code: ${res.status}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (res.ok) {
|
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 {
|
} else {
|
||||||
|
const detailMessage = `Webhook request failed with status code: ${res.status}.`;
|
||||||
state = 'failed';
|
state = 'failed';
|
||||||
stateDetails = `Webhook request failed with status code: ${res.status}`;
|
stateDetails.push(detailMessage);
|
||||||
|
this.logger.warn(detailMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.registerEvent({
|
this.registerEvent({
|
||||||
integrationId,
|
integrationId,
|
||||||
state,
|
state,
|
||||||
stateDetails,
|
stateDetails: stateDetails.join('\n'),
|
||||||
event: serializeDates(event),
|
event: serializeDates(event),
|
||||||
details: {
|
details: {
|
||||||
url,
|
url,
|
||||||
|
Loading…
Reference in New Issue
Block a user