Skip to content

Commit

Permalink
chore: register integration events in Teams integration (#7634)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nunogois authored Jul 22, 2024
1 parent 1033276 commit e07ded9
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 147 deletions.
12 changes: 6 additions & 6 deletions src/lib/addons/__snapshots__/teams.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should call teams webhook 1`] = `"{"themeColor":"0076D7","summary":"Message","sections":[{"activityTitle":"*[email protected]* created *[some-toggle](http://some-url.com/projects//features/some-toggle)* in project **","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"[email protected]"},{"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":"*[email protected]* created *[some-toggle](http://some-url.com/projects//features/some-toggle)* in project **","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"[email protected]"},{"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":"*[email protected]* archived *some-toggle* in project **","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"[email protected]"},{"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":"*[email protected]* archived *some-toggle* in project **","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"[email protected]"},{"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":"*[email protected]* archived *some-toggle* in project *[some-project](http://some-url.com/projects/some-project)*","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"[email protected]"},{"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":"*[email protected]* archived *some-toggle* in project *[some-project](http://some-url.com/projects/some-project)*","activitySubtitle":"Unleash notification update","facts":[{"name":"User","value":"[email protected]"},{"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":"*[email protected]* 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":"[email protected]"},{"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":"*[email protected]* 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":"[email protected]"},{"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":"*[email protected]* 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":"[email protected]"},{"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":"*[email protected]* 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":"[email protected]"},{"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",
"MY_CUSTOM_HEADER": "MY_CUSTOM_VALUE",
Expand Down
316 changes: 196 additions & 120 deletions src/lib/addons/teams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,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 = {
Expand Down Expand Up @@ -45,142 +47,216 @@ 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 teams webhook', async () => {
const addon = new TeamsAddon(ARGS);
const event: IEvent = {
id: 1,
createdAt: new Date(),
type: FEATURE_CREATED,
createdByUserId: SYSTEM_USER_ID,
createdBy: '[email protected]',
featureName: 'some-toggle',
data: {
name: 'some-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
};
describe('Teams integration', () => {
beforeEach(() => {
registerEventMock.mockClear();
});

const parameters = {
url: 'http://hooks.office.com',
};
test('Should call teams webhook', async () => {
const addon = new TeamsAddon(ARGS);
const event: IEvent = {
id: 1,
createdAt: new Date(),
type: FEATURE_CREATED,
createdByUserId: SYSTEM_USER_ID,
createdBy: '[email protected]',
featureName: 'some-toggle',
data: {
name: 'some-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
};

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 parameters = {
url: 'http://hooks.office.com',
};

test('Should call teams webhook for archived toggle', async () => {
const addon = new TeamsAddon(ARGS);
const event: IEvent = {
id: 1,
createdAt: new Date(),
createdByUserId: SYSTEM_USER_ID,
type: FEATURE_ARCHIVED,
createdBy: '[email protected]',
featureName: 'some-toggle',
data: {
name: 'some-toggle',
},
};
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 parameters = {
url: 'http://hooks.office.com',
};
test('Should call teams webhook for archived toggle', async () => {
const addon = new TeamsAddon(ARGS);
const event: IEvent = {
id: 1,
createdAt: new Date(),
createdByUserId: SYSTEM_USER_ID,
type: FEATURE_ARCHIVED,
createdBy: '[email protected]',
featureName: 'some-toggle',
data: {
name: 'some-toggle',
},
};

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 parameters = {
url: 'http://hooks.office.com',
};

test('Should call teams webhook for archived toggle with project info', async () => {
const addon = new TeamsAddon(ARGS);
const event: IEvent = {
id: 1,
createdAt: new Date(),
createdByUserId: SYSTEM_USER_ID,
type: FEATURE_ARCHIVED,
createdBy: '[email protected]',
featureName: 'some-toggle',
project: 'some-project',
data: {
name: 'some-toggle',
},
};
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 parameters = {
url: 'http://hooks.office.com',
};
test('Should call teams webhook for archived toggle with project info', async () => {
const addon = new TeamsAddon(ARGS);
const event: IEvent = {
id: 1,
createdAt: new Date(),
createdByUserId: SYSTEM_USER_ID,
type: FEATURE_ARCHIVED,
createdBy: '[email protected]',
featureName: 'some-toggle',
project: 'some-project',
data: {
name: 'some-toggle',
},
};

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 parameters = {
url: 'http://hooks.office.com',
};

test(`Should call teams webhook for toggled environment`, async () => {
const addon = new TeamsAddon(ARGS);
const event: IEvent = {
id: 2,
createdAt: new Date(),
createdByUserId: SYSTEM_USER_ID,
type: FEATURE_ENVIRONMENT_DISABLED,
createdBy: '[email protected]',
environment: 'development',
project: 'default',
featureName: 'some-toggle',
data: {
name: 'some-toggle',
},
};
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 parameters = {
url: 'http://hooks.slack.com',
};
test(`Should call teams webhook for toggled environment`, async () => {
const addon = new TeamsAddon(ARGS);
const event: IEvent = {
id: 2,
createdAt: new Date(),
createdByUserId: SYSTEM_USER_ID,
type: FEATURE_ENVIRONMENT_DISABLED,
createdBy: '[email protected]',
environment: 'development',
project: 'default',
featureName: 'some-toggle',
data: {
name: 'some-toggle',
},
};

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();
});
const parameters = {
url: 'http://hooks.slack.com',
};

test('Should include custom headers in call to teams', async () => {
const addon = new TeamsAddon(ARGS);
const event: IEvent = {
id: 2,
createdAt: new Date(),
createdByUserId: SYSTEM_USER_ID,
type: FEATURE_ENVIRONMENT_DISABLED,
createdBy: '[email protected]',
environment: 'development',
project: 'default',
featureName: 'some-toggle',
data: {
name: 'some-toggle',
},
};

const parameters = {
url: 'http://hooks.slack.com',
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();
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 include custom headers in call to teams', async () => {
const addon = new TeamsAddon(ARGS);
const event: IEvent = {
id: 2,
createdAt: new Date(),
createdByUserId: SYSTEM_USER_ID,
type: FEATURE_ENVIRONMENT_DISABLED,
createdBy: '[email protected]',
environment: 'development',
project: 'default',
featureName: 'some-toggle',
data: {
name: 'some-toggle',
},
};

const parameters = {
url: 'http://hooks.slack.com',
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 TeamsAddon(ARGS);
const event: IEvent = {
id: 2,
createdAt: new Date(),
createdByUserId: SYSTEM_USER_ID,
type: FEATURE_ENVIRONMENT_DISABLED,
createdBy: '[email protected]',
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}`,
},
],
},
],
},
},
});
});
});
Loading

0 comments on commit e07ded9

Please sign in to comment.