mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-04 00:18:01 +01:00
chore: register integration events in Teams integration (#7634)
https://linear.app/unleash/issue/2-2460/register-integration-events-teams Registers integration events in the **Teams** integration. Also includes slight improvements to the **Webhooks** integration. Similar to: - #7631 - #7626 - #7621
This commit is contained in:
parent
1033276e97
commit
e07ded9455
@ -1,16 +1,16 @@
|
|||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
exports[`Should call teams webhook 1`] = `"{"themeColor":"0076D7","summary":"Message","sections":[{"activityTitle":"*some@user.com* created *[some-toggle](http://some-url.com/projects//features/some-toggle)* in project **","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"some@user.com"},{"name":"Action","value":"feature-created"}]}],"potentialAction":[{"@type":"OpenUri","name":"Go to feature","targets":[{"os":"default","uri":"http://some-url.com/projects//features/some-toggle"}]}]}"`;
|
exports[`Teams integration Should call teams webhook 1`] = `"{"themeColor":"0076D7","summary":"Message","sections":[{"activityTitle":"*some@user.com* created *[some-toggle](http://some-url.com/projects//features/some-toggle)* in project **","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"some@user.com"},{"name":"Action","value":"feature-created"}]}],"potentialAction":[{"@type":"OpenUri","name":"Go to feature","targets":[{"os":"default","uri":"http://some-url.com/projects//features/some-toggle"}]}]}"`;
|
||||||
|
|
||||||
exports[`Should call teams webhook for archived toggle 1`] = `"{"themeColor":"0076D7","summary":"Message","sections":[{"activityTitle":"*some@user.com* archived *some-toggle* in project **","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"some@user.com"},{"name":"Action","value":"feature-archived"}]}],"potentialAction":[{"@type":"OpenUri","name":"Go to feature","targets":[{"os":"default","uri":"http://some-url.com/projects//archive"}]}]}"`;
|
exports[`Teams integration Should call teams webhook for archived toggle 1`] = `"{"themeColor":"0076D7","summary":"Message","sections":[{"activityTitle":"*some@user.com* archived *some-toggle* in project **","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"some@user.com"},{"name":"Action","value":"feature-archived"}]}],"potentialAction":[{"@type":"OpenUri","name":"Go to feature","targets":[{"os":"default","uri":"http://some-url.com/projects//archive"}]}]}"`;
|
||||||
|
|
||||||
exports[`Should call teams webhook for archived toggle with project info 1`] = `"{"themeColor":"0076D7","summary":"Message","sections":[{"activityTitle":"*some@user.com* archived *some-toggle* in project *[some-project](http://some-url.com/projects/some-project)*","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"some@user.com"},{"name":"Action","value":"feature-archived"}]}],"potentialAction":[{"@type":"OpenUri","name":"Go to feature","targets":[{"os":"default","uri":"http://some-url.com/projects/some-project/archive"}]}]}"`;
|
exports[`Teams integration Should call teams webhook for archived toggle with project info 1`] = `"{"themeColor":"0076D7","summary":"Message","sections":[{"activityTitle":"*some@user.com* archived *some-toggle* in project *[some-project](http://some-url.com/projects/some-project)*","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"some@user.com"},{"name":"Action","value":"feature-archived"}]}],"potentialAction":[{"@type":"OpenUri","name":"Go to feature","targets":[{"os":"default","uri":"http://some-url.com/projects/some-project/archive"}]}]}"`;
|
||||||
|
|
||||||
exports[`Should call teams webhook for toggled environment 1`] = `"{"themeColor":"0076D7","summary":"Message","sections":[{"activityTitle":"*some@user.com* disabled *[some-toggle](http://some-url.com/projects/default/features/some-toggle)* for the *development* environment in project *[default](http://some-url.com/projects/default)*","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"some@user.com"},{"name":"Action","value":"feature-environment-disabled"}]}],"potentialAction":[{"@type":"OpenUri","name":"Go to feature","targets":[{"os":"default","uri":"http://some-url.com/projects/default/features/some-toggle"}]}]}"`;
|
exports[`Teams integration Should call teams webhook for toggled environment 1`] = `"{"themeColor":"0076D7","summary":"Message","sections":[{"activityTitle":"*some@user.com* disabled *[some-toggle](http://some-url.com/projects/default/features/some-toggle)* for the *development* environment in project *[default](http://some-url.com/projects/default)*","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"some@user.com"},{"name":"Action","value":"feature-environment-disabled"}]}],"potentialAction":[{"@type":"OpenUri","name":"Go to feature","targets":[{"os":"default","uri":"http://some-url.com/projects/default/features/some-toggle"}]}]}"`;
|
||||||
|
|
||||||
exports[`Should include custom headers in call to teams 1`] = `"{"themeColor":"0076D7","summary":"Message","sections":[{"activityTitle":"*some@user.com* disabled *[some-toggle](http://some-url.com/projects/default/features/some-toggle)* for the *development* environment in project *[default](http://some-url.com/projects/default)*","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"some@user.com"},{"name":"Action","value":"feature-environment-disabled"}]}],"potentialAction":[{"@type":"OpenUri","name":"Go to feature","targets":[{"os":"default","uri":"http://some-url.com/projects/default/features/some-toggle"}]}]}"`;
|
exports[`Teams integration Should include custom headers in call to teams 1`] = `"{"themeColor":"0076D7","summary":"Message","sections":[{"activityTitle":"*some@user.com* disabled *[some-toggle](http://some-url.com/projects/default/features/some-toggle)* for the *development* environment in project *[default](http://some-url.com/projects/default)*","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"some@user.com"},{"name":"Action","value":"feature-environment-disabled"}]}],"potentialAction":[{"@type":"OpenUri","name":"Go to feature","targets":[{"os":"default","uri":"http://some-url.com/projects/default/features/some-toggle"}]}]}"`;
|
||||||
|
|
||||||
exports[`Should include custom headers in call to teams 2`] = `
|
exports[`Teams integration Should include custom headers in call to teams 2`] = `
|
||||||
{
|
{
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"MY_CUSTOM_HEADER": "MY_CUSTOM_VALUE",
|
"MY_CUSTOM_HEADER": "MY_CUSTOM_VALUE",
|
||||||
|
@ -13,11 +13,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 = {
|
||||||
@ -45,142 +47,216 @@ 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);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
test('Should call teams webhook', async () => {
|
describe('Teams integration', () => {
|
||||||
const addon = new TeamsAddon(ARGS);
|
beforeEach(() => {
|
||||||
const event: IEvent = {
|
registerEventMock.mockClear();
|
||||||
id: 1,
|
});
|
||||||
createdAt: new Date(),
|
|
||||||
type: FEATURE_CREATED,
|
|
||||||
createdByUserId: SYSTEM_USER_ID,
|
|
||||||
createdBy: 'some@user.com',
|
|
||||||
featureName: 'some-toggle',
|
|
||||||
data: {
|
|
||||||
name: 'some-toggle',
|
|
||||||
enabled: false,
|
|
||||||
strategies: [{ name: 'default' }],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const parameters = {
|
test('Should call teams webhook', async () => {
|
||||||
url: 'http://hooks.office.com',
|
const addon = new TeamsAddon(ARGS);
|
||||||
};
|
const event: IEvent = {
|
||||||
|
id: 1,
|
||||||
|
createdAt: new Date(),
|
||||||
|
type: FEATURE_CREATED,
|
||||||
|
createdByUserId: SYSTEM_USER_ID,
|
||||||
|
createdBy: 'some@user.com',
|
||||||
|
featureName: 'some-toggle',
|
||||||
|
data: {
|
||||||
|
name: 'some-toggle',
|
||||||
|
enabled: false,
|
||||||
|
strategies: [{ name: 'default' }],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
await addon.handleEvent(event, parameters, INTEGRATION_ID);
|
const parameters = {
|
||||||
expect(fetchRetryCalls.length).toBe(1);
|
url: 'http://hooks.office.com',
|
||||||
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
};
|
||||||
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
|
||||||
});
|
await addon.handleEvent(event, parameters, INTEGRATION_ID);
|
||||||
|
expect(fetchRetryCalls.length).toBe(1);
|
||||||
test('Should call teams webhook for archived toggle', async () => {
|
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
||||||
const addon = new TeamsAddon(ARGS);
|
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
||||||
const event: IEvent = {
|
});
|
||||||
id: 1,
|
|
||||||
createdAt: new Date(),
|
test('Should call teams webhook for archived toggle', async () => {
|
||||||
createdByUserId: SYSTEM_USER_ID,
|
const addon = new TeamsAddon(ARGS);
|
||||||
type: FEATURE_ARCHIVED,
|
const event: IEvent = {
|
||||||
createdBy: 'some@user.com',
|
id: 1,
|
||||||
featureName: 'some-toggle',
|
createdAt: new Date(),
|
||||||
data: {
|
createdByUserId: SYSTEM_USER_ID,
|
||||||
name: 'some-toggle',
|
type: FEATURE_ARCHIVED,
|
||||||
},
|
createdBy: 'some@user.com',
|
||||||
};
|
featureName: 'some-toggle',
|
||||||
|
data: {
|
||||||
const parameters = {
|
name: 'some-toggle',
|
||||||
url: 'http://hooks.office.com',
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
await addon.handleEvent(event, parameters, INTEGRATION_ID);
|
const parameters = {
|
||||||
expect(fetchRetryCalls.length).toBe(1);
|
url: 'http://hooks.office.com',
|
||||||
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
};
|
||||||
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
|
||||||
});
|
await addon.handleEvent(event, parameters, INTEGRATION_ID);
|
||||||
|
expect(fetchRetryCalls.length).toBe(1);
|
||||||
test('Should call teams webhook for archived toggle with project info', async () => {
|
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
||||||
const addon = new TeamsAddon(ARGS);
|
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
||||||
const event: IEvent = {
|
});
|
||||||
id: 1,
|
|
||||||
createdAt: new Date(),
|
test('Should call teams webhook for archived toggle with project info', async () => {
|
||||||
createdByUserId: SYSTEM_USER_ID,
|
const addon = new TeamsAddon(ARGS);
|
||||||
type: FEATURE_ARCHIVED,
|
const event: IEvent = {
|
||||||
createdBy: 'some@user.com',
|
id: 1,
|
||||||
featureName: 'some-toggle',
|
createdAt: new Date(),
|
||||||
project: 'some-project',
|
createdByUserId: SYSTEM_USER_ID,
|
||||||
data: {
|
type: FEATURE_ARCHIVED,
|
||||||
name: 'some-toggle',
|
createdBy: 'some@user.com',
|
||||||
},
|
featureName: 'some-toggle',
|
||||||
};
|
project: 'some-project',
|
||||||
|
data: {
|
||||||
const parameters = {
|
name: 'some-toggle',
|
||||||
url: 'http://hooks.office.com',
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
await addon.handleEvent(event, parameters, INTEGRATION_ID);
|
const parameters = {
|
||||||
expect(fetchRetryCalls.length).toBe(1);
|
url: 'http://hooks.office.com',
|
||||||
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
};
|
||||||
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
|
||||||
});
|
await addon.handleEvent(event, parameters, INTEGRATION_ID);
|
||||||
|
expect(fetchRetryCalls.length).toBe(1);
|
||||||
test(`Should call teams webhook for toggled environment`, async () => {
|
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
||||||
const addon = new TeamsAddon(ARGS);
|
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
||||||
const event: IEvent = {
|
});
|
||||||
id: 2,
|
|
||||||
createdAt: new Date(),
|
test(`Should call teams webhook for toggled environment`, async () => {
|
||||||
createdByUserId: SYSTEM_USER_ID,
|
const addon = new TeamsAddon(ARGS);
|
||||||
type: FEATURE_ENVIRONMENT_DISABLED,
|
const event: IEvent = {
|
||||||
createdBy: 'some@user.com',
|
id: 2,
|
||||||
environment: 'development',
|
createdAt: new Date(),
|
||||||
project: 'default',
|
createdByUserId: SYSTEM_USER_ID,
|
||||||
featureName: 'some-toggle',
|
type: FEATURE_ENVIRONMENT_DISABLED,
|
||||||
data: {
|
createdBy: 'some@user.com',
|
||||||
name: 'some-toggle',
|
environment: 'development',
|
||||||
},
|
project: 'default',
|
||||||
};
|
featureName: 'some-toggle',
|
||||||
|
data: {
|
||||||
const parameters = {
|
name: 'some-toggle',
|
||||||
url: 'http://hooks.slack.com',
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
await addon.handleEvent(event, parameters, INTEGRATION_ID);
|
const parameters = {
|
||||||
expect(fetchRetryCalls).toHaveLength(1);
|
url: 'http://hooks.slack.com',
|
||||||
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
};
|
||||||
expect(fetchRetryCalls[0].options.body).toMatch(/disabled/);
|
|
||||||
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
await addon.handleEvent(event, parameters, INTEGRATION_ID);
|
||||||
});
|
expect(fetchRetryCalls).toHaveLength(1);
|
||||||
|
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
||||||
test('Should include custom headers in call to teams', async () => {
|
expect(fetchRetryCalls[0].options.body).toMatch(/disabled/);
|
||||||
const addon = new TeamsAddon(ARGS);
|
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
||||||
const event: IEvent = {
|
});
|
||||||
id: 2,
|
|
||||||
createdAt: new Date(),
|
test('Should include custom headers in call to teams', async () => {
|
||||||
createdByUserId: SYSTEM_USER_ID,
|
const addon = new TeamsAddon(ARGS);
|
||||||
type: FEATURE_ENVIRONMENT_DISABLED,
|
const event: IEvent = {
|
||||||
createdBy: 'some@user.com',
|
id: 2,
|
||||||
environment: 'development',
|
createdAt: new Date(),
|
||||||
project: 'default',
|
createdByUserId: SYSTEM_USER_ID,
|
||||||
featureName: 'some-toggle',
|
type: FEATURE_ENVIRONMENT_DISABLED,
|
||||||
data: {
|
createdBy: 'some@user.com',
|
||||||
name: 'some-toggle',
|
environment: 'development',
|
||||||
},
|
project: 'default',
|
||||||
};
|
featureName: 'some-toggle',
|
||||||
|
data: {
|
||||||
const parameters = {
|
name: 'some-toggle',
|
||||||
url: 'http://hooks.slack.com',
|
},
|
||||||
customHeaders: `{ "MY_CUSTOM_HEADER": "MY_CUSTOM_VALUE" }`,
|
};
|
||||||
};
|
|
||||||
|
const parameters = {
|
||||||
await addon.handleEvent(event, parameters, INTEGRATION_ID);
|
url: 'http://hooks.slack.com',
|
||||||
expect(fetchRetryCalls).toHaveLength(1);
|
customHeaders: `{ "MY_CUSTOM_HEADER": "MY_CUSTOM_VALUE" }`,
|
||||||
expect(fetchRetryCalls[0].url).toBe(parameters.url);
|
};
|
||||||
expect(fetchRetryCalls[0].options.body).toMatch(/disabled/);
|
|
||||||
expect(fetchRetryCalls[0].options.body).toMatchSnapshot();
|
await addon.handleEvent(event, parameters, INTEGRATION_ID);
|
||||||
expect(fetchRetryCalls[0].options.headers).toMatchSnapshot();
|
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 TeamsAddon(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.teams.com',
|
||||||
|
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:
|
||||||
|
'Teams webhook request was successful with status code: 200.',
|
||||||
|
event: serializeDates(event),
|
||||||
|
details: {
|
||||||
|
url: parameters.url,
|
||||||
|
body: {
|
||||||
|
themeColor: '0076D7',
|
||||||
|
summary: 'Message',
|
||||||
|
sections: [
|
||||||
|
{
|
||||||
|
activityTitle: `*${event.createdBy}* disabled *[${event.featureName}](${ARGS.unleashUrl}/projects/${event.project}/features/${event.featureName})* for the *${event.environment}* environment in project *[${event.project}](${ARGS.unleashUrl}/projects/${event.project})*`,
|
||||||
|
activitySubtitle: `Unleash notification update`,
|
||||||
|
facts: [
|
||||||
|
{
|
||||||
|
name: 'User',
|
||||||
|
value: event.createdBy,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Action',
|
||||||
|
value: event.type,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
potentialAction: [
|
||||||
|
{
|
||||||
|
'@type': 'OpenUri',
|
||||||
|
name: 'Go to feature',
|
||||||
|
targets: [
|
||||||
|
{
|
||||||
|
os: 'default',
|
||||||
|
uri: `${ARGS.unleashUrl}/projects/${event.project}/features/${event.featureName}`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
import Addon from './addon';
|
import Addon from './addon';
|
||||||
|
|
||||||
import teamsDefinition from './teams-definition';
|
import teamsDefinition from './teams-definition';
|
||||||
import type { IAddonConfig } from '../types/model';
|
import { type IAddonConfig, serializeDates } from '../types';
|
||||||
import {
|
import {
|
||||||
type FeatureEventFormatter,
|
type FeatureEventFormatter,
|
||||||
FeatureEventFormatterMd,
|
FeatureEventFormatterMd,
|
||||||
} 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 ITeamsParameters {
|
interface ITeamsParameters {
|
||||||
url: string;
|
url: string;
|
||||||
@ -26,6 +27,9 @@ export default class TeamsAddon extends Addon {
|
|||||||
parameters: ITeamsParameters,
|
parameters: ITeamsParameters,
|
||||||
integrationId: number,
|
integrationId: number,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
let state: IntegrationEventState = 'success';
|
||||||
|
const stateDetails: string[] = [];
|
||||||
|
|
||||||
const { url, customHeaders } = parameters;
|
const { url, customHeaders } = parameters;
|
||||||
const { createdBy } = event;
|
const { createdBy } = event;
|
||||||
const { text, url: featureLink } = this.msgFormatter.format(event);
|
const { text, url: featureLink } = this.msgFormatter.format(event);
|
||||||
@ -68,9 +72,11 @@ export default class TeamsAddon extends Addon {
|
|||||||
try {
|
try {
|
||||||
extraHeaders = JSON.parse(customHeaders);
|
extraHeaders = JSON.parse(customHeaders);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.warn(
|
state = 'successWithErrors';
|
||||||
`Could not parse the json in the customHeaders parameter. [${customHeaders}]`,
|
const badHeadersMessage =
|
||||||
);
|
'Could not parse the JSON in the customHeaders parameter.';
|
||||||
|
stateDetails.push(badHeadersMessage);
|
||||||
|
this.logger.warn(badHeadersMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,8 +86,29 @@ export default class TeamsAddon extends Addon {
|
|||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
};
|
};
|
||||||
const res = await this.fetchRetry(url, requestOpts);
|
const res = await this.fetchRetry(url, requestOpts);
|
||||||
this.logger.info(
|
|
||||||
`Handled event ${event.type}. Status codes=${res.status}`,
|
this.logger.info(`Handled event "${event.type}".`);
|
||||||
);
|
|
||||||
|
if (res.ok) {
|
||||||
|
const successMessage = `Teams webhook request was successful with status code: ${res.status}.`;
|
||||||
|
stateDetails.push(successMessage);
|
||||||
|
this.logger.info(successMessage);
|
||||||
|
} else {
|
||||||
|
state = 'failed';
|
||||||
|
const failedMessage = `Teams webhook request failed with status code: ${res.status}.`;
|
||||||
|
stateDetails.push(failedMessage);
|
||||||
|
this.logger.warn(failedMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.registerEvent({
|
||||||
|
integrationId,
|
||||||
|
state,
|
||||||
|
stateDetails: stateDetails.join('\n'),
|
||||||
|
event: serializeDates(event),
|
||||||
|
details: {
|
||||||
|
url,
|
||||||
|
body,
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,8 +26,13 @@ export default class Webhook extends Addon {
|
|||||||
let state: IntegrationEventState = 'success';
|
let state: IntegrationEventState = 'success';
|
||||||
const stateDetails: string[] = [];
|
const stateDetails: string[] = [];
|
||||||
|
|
||||||
const { url, bodyTemplate, contentType, authorization, customHeaders } =
|
const {
|
||||||
parameters;
|
url,
|
||||||
|
bodyTemplate,
|
||||||
|
contentType = 'application/json',
|
||||||
|
authorization,
|
||||||
|
customHeaders,
|
||||||
|
} = parameters;
|
||||||
const context = {
|
const context = {
|
||||||
event,
|
event,
|
||||||
// Stringify twice to avoid escaping in Mustache
|
// Stringify twice to avoid escaping in Mustache
|
||||||
@ -35,11 +40,13 @@ export default class Webhook extends Addon {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let body: string | undefined;
|
let body: string | undefined;
|
||||||
|
let sendingEvent = false;
|
||||||
|
|
||||||
if (typeof bodyTemplate === 'string' && bodyTemplate.length > 1) {
|
if (typeof bodyTemplate === 'string' && bodyTemplate.length > 1) {
|
||||||
body = Mustache.render(bodyTemplate, context);
|
body = Mustache.render(bodyTemplate, context);
|
||||||
} else {
|
} else {
|
||||||
body = JSON.stringify(event);
|
body = JSON.stringify(event);
|
||||||
|
sendingEvent = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
let extraHeaders = {};
|
let extraHeaders = {};
|
||||||
@ -47,17 +54,17 @@ export default class Webhook extends Addon {
|
|||||||
try {
|
try {
|
||||||
extraHeaders = JSON.parse(customHeaders);
|
extraHeaders = JSON.parse(customHeaders);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const detailMessage =
|
|
||||||
'Could not parse the JSON in the customHeaders parameter.';
|
|
||||||
state = 'successWithErrors';
|
state = 'successWithErrors';
|
||||||
stateDetails.push(detailMessage);
|
const badHeadersMessage =
|
||||||
this.logger.warn(detailMessage);
|
'Could not parse the JSON in the customHeaders parameter.';
|
||||||
|
stateDetails.push(badHeadersMessage);
|
||||||
|
this.logger.warn(badHeadersMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const requestOpts = {
|
const requestOpts = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': contentType || 'application/json',
|
'Content-Type': contentType,
|
||||||
Authorization: authorization || undefined,
|
Authorization: authorization || undefined,
|
||||||
...extraHeaders,
|
...extraHeaders,
|
||||||
},
|
},
|
||||||
@ -68,14 +75,14 @@ export default class Webhook extends Addon {
|
|||||||
this.logger.info(`Handled event "${event.type}".`);
|
this.logger.info(`Handled event "${event.type}".`);
|
||||||
|
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const detailMessage = `Webhook request was successful with status code: ${res.status}.`;
|
const successMessage = `Webhook request was successful with status code: ${res.status}.`;
|
||||||
stateDetails.push(detailMessage);
|
stateDetails.push(successMessage);
|
||||||
this.logger.info(detailMessage);
|
this.logger.info(successMessage);
|
||||||
} else {
|
} else {
|
||||||
const detailMessage = `Webhook request failed with status code: ${res.status}.`;
|
|
||||||
state = 'failed';
|
state = 'failed';
|
||||||
stateDetails.push(detailMessage);
|
const failedMessage = `Webhook request failed with status code: ${res.status}.`;
|
||||||
this.logger.warn(detailMessage);
|
stateDetails.push(failedMessage);
|
||||||
|
this.logger.warn(failedMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.registerEvent({
|
this.registerEvent({
|
||||||
@ -86,7 +93,7 @@ export default class Webhook extends Addon {
|
|||||||
details: {
|
details: {
|
||||||
url,
|
url,
|
||||||
contentType,
|
contentType,
|
||||||
body,
|
body: sendingEvent ? event : body,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user