2021-08-12 15:04:37 +02:00
|
|
|
import Mustache from 'mustache';
|
|
|
|
import Addon from './addon';
|
|
|
|
import definition from './webhook-definition';
|
2024-03-18 13:58:05 +01:00
|
|
|
import type { IEvent } from '../types/events';
|
2024-08-20 09:00:28 +02:00
|
|
|
import {
|
|
|
|
type IAddonConfig,
|
|
|
|
type IFlagResolver,
|
|
|
|
serializeDates,
|
|
|
|
} from '../types';
|
chore: register integration events in webhooks (#7621)
https://linear.app/unleash/issue/2-2450/register-integration-events-webhook
Registers integration events in the **Webhook** integration.
Even though this touches a lot of files, most of it is preparation for
the next steps. The only actual implementation of registering
integration events is in the **Webhook** integration. The rest will
follow on separate PRs.
Here's an example of how this looks like in the database table:
```json
{
"id": 7,
"integration_id": 2,
"created_at": "2024-07-18T18:11:11.376348+01:00",
"state": "failed",
"state_details": "Webhook request failed with status code: ECONNREFUSED",
"event": {
"id": 130,
"data": null,
"tags": [],
"type": "feature-environment-enabled",
"preData": null,
"project": "default",
"createdAt": "2024-07-18T17:11:10.821Z",
"createdBy": "admin",
"environment": "development",
"featureName": "test",
"createdByUserId": 1
},
"details": {
"url": "http://localhost:1337",
"body": "{ \"id\": 130, \"type\": \"feature-environment-enabled\", \"createdBy\": \"admin\", \"createdAt\": \"2024-07-18T17: 11: 10.821Z\", \"createdByUserId\": 1, \"data\": null, \"preData\": null, \"tags\": [], \"featureName\": \"test\", \"project\": \"default\", \"environment\": \"development\" }"
}
}
```
2024-07-19 11:07:52 +02:00
|
|
|
import type { IntegrationEventState } from '../features/integration-events/integration-events-store';
|
2024-07-25 11:45:20 +02:00
|
|
|
import {
|
|
|
|
type FeatureEventFormatter,
|
|
|
|
FeatureEventFormatterMd,
|
|
|
|
} from './feature-event-formatter-md';
|
2021-11-12 13:15:51 +01:00
|
|
|
|
|
|
|
interface IParameters {
|
|
|
|
url: string;
|
2024-08-20 09:00:28 +02:00
|
|
|
serviceName?: string;
|
2021-11-12 13:15:51 +01:00
|
|
|
bodyTemplate?: string;
|
|
|
|
contentType?: string;
|
2022-11-09 11:45:30 +01:00
|
|
|
authorization?: string;
|
2023-07-05 09:42:17 +02:00
|
|
|
customHeaders?: string;
|
2021-11-12 13:15:51 +01:00
|
|
|
}
|
2021-08-12 15:04:37 +02:00
|
|
|
|
|
|
|
export default class Webhook extends Addon {
|
2024-07-25 11:45:20 +02:00
|
|
|
private msgFormatter: FeatureEventFormatter;
|
|
|
|
|
2024-08-20 09:00:28 +02:00
|
|
|
flagResolver: IFlagResolver;
|
|
|
|
|
chore: register integration events in webhooks (#7621)
https://linear.app/unleash/issue/2-2450/register-integration-events-webhook
Registers integration events in the **Webhook** integration.
Even though this touches a lot of files, most of it is preparation for
the next steps. The only actual implementation of registering
integration events is in the **Webhook** integration. The rest will
follow on separate PRs.
Here's an example of how this looks like in the database table:
```json
{
"id": 7,
"integration_id": 2,
"created_at": "2024-07-18T18:11:11.376348+01:00",
"state": "failed",
"state_details": "Webhook request failed with status code: ECONNREFUSED",
"event": {
"id": 130,
"data": null,
"tags": [],
"type": "feature-environment-enabled",
"preData": null,
"project": "default",
"createdAt": "2024-07-18T17:11:10.821Z",
"createdBy": "admin",
"environment": "development",
"featureName": "test",
"createdByUserId": 1
},
"details": {
"url": "http://localhost:1337",
"body": "{ \"id\": 130, \"type\": \"feature-environment-enabled\", \"createdBy\": \"admin\", \"createdAt\": \"2024-07-18T17: 11: 10.821Z\", \"createdByUserId\": 1, \"data\": null, \"preData\": null, \"tags\": [], \"featureName\": \"test\", \"project\": \"default\", \"environment\": \"development\" }"
}
}
```
2024-07-19 11:07:52 +02:00
|
|
|
constructor(args: IAddonConfig) {
|
2021-01-19 10:42:45 +01:00
|
|
|
super(definition, args);
|
2024-09-24 11:25:12 +02:00
|
|
|
this.msgFormatter = new FeatureEventFormatterMd({
|
|
|
|
unleashUrl: args.unleashUrl,
|
|
|
|
});
|
2024-08-20 09:00:28 +02:00
|
|
|
this.flagResolver = args.flagResolver;
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
chore: register integration events in webhooks (#7621)
https://linear.app/unleash/issue/2-2450/register-integration-events-webhook
Registers integration events in the **Webhook** integration.
Even though this touches a lot of files, most of it is preparation for
the next steps. The only actual implementation of registering
integration events is in the **Webhook** integration. The rest will
follow on separate PRs.
Here's an example of how this looks like in the database table:
```json
{
"id": 7,
"integration_id": 2,
"created_at": "2024-07-18T18:11:11.376348+01:00",
"state": "failed",
"state_details": "Webhook request failed with status code: ECONNREFUSED",
"event": {
"id": 130,
"data": null,
"tags": [],
"type": "feature-environment-enabled",
"preData": null,
"project": "default",
"createdAt": "2024-07-18T17:11:10.821Z",
"createdBy": "admin",
"environment": "development",
"featureName": "test",
"createdByUserId": 1
},
"details": {
"url": "http://localhost:1337",
"body": "{ \"id\": 130, \"type\": \"feature-environment-enabled\", \"createdBy\": \"admin\", \"createdAt\": \"2024-07-18T17: 11: 10.821Z\", \"createdByUserId\": 1, \"data\": null, \"preData\": null, \"tags\": [], \"featureName\": \"test\", \"project\": \"default\", \"environment\": \"development\" }"
}
}
```
2024-07-19 11:07:52 +02:00
|
|
|
async handleEvent(
|
|
|
|
event: IEvent,
|
|
|
|
parameters: IParameters,
|
|
|
|
integrationId: number,
|
|
|
|
): Promise<void> {
|
|
|
|
let state: IntegrationEventState = 'success';
|
2024-07-19 13:56:55 +02:00
|
|
|
const stateDetails: string[] = [];
|
chore: register integration events in webhooks (#7621)
https://linear.app/unleash/issue/2-2450/register-integration-events-webhook
Registers integration events in the **Webhook** integration.
Even though this touches a lot of files, most of it is preparation for
the next steps. The only actual implementation of registering
integration events is in the **Webhook** integration. The rest will
follow on separate PRs.
Here's an example of how this looks like in the database table:
```json
{
"id": 7,
"integration_id": 2,
"created_at": "2024-07-18T18:11:11.376348+01:00",
"state": "failed",
"state_details": "Webhook request failed with status code: ECONNREFUSED",
"event": {
"id": 130,
"data": null,
"tags": [],
"type": "feature-environment-enabled",
"preData": null,
"project": "default",
"createdAt": "2024-07-18T17:11:10.821Z",
"createdBy": "admin",
"environment": "development",
"featureName": "test",
"createdByUserId": 1
},
"details": {
"url": "http://localhost:1337",
"body": "{ \"id\": 130, \"type\": \"feature-environment-enabled\", \"createdBy\": \"admin\", \"createdAt\": \"2024-07-18T17: 11: 10.821Z\", \"createdByUserId\": 1, \"data\": null, \"preData\": null, \"tags\": [], \"featureName\": \"test\", \"project\": \"default\", \"environment\": \"development\" }"
}
}
```
2024-07-19 11:07:52 +02:00
|
|
|
|
2024-07-22 13:13:10 +02:00
|
|
|
const {
|
|
|
|
url,
|
|
|
|
bodyTemplate,
|
|
|
|
contentType = 'application/json',
|
|
|
|
authorization,
|
|
|
|
customHeaders,
|
|
|
|
} = parameters;
|
2021-01-19 10:42:45 +01:00
|
|
|
const context = {
|
|
|
|
event,
|
2024-03-26 09:25:16 +01:00
|
|
|
// Stringify twice to avoid escaping in Mustache
|
|
|
|
eventJson: JSON.stringify(JSON.stringify(event)),
|
2024-07-25 11:45:20 +02:00
|
|
|
eventMarkdown: this.msgFormatter.format(event).text,
|
2021-01-19 10:42:45 +01:00
|
|
|
};
|
|
|
|
|
2024-01-12 10:25:59 +01:00
|
|
|
let body: string | undefined;
|
2024-07-22 13:13:10 +02:00
|
|
|
let sendingEvent = false;
|
2021-01-19 10:42:45 +01:00
|
|
|
|
|
|
|
if (typeof bodyTemplate === 'string' && bodyTemplate.length > 1) {
|
|
|
|
body = Mustache.render(bodyTemplate, context);
|
|
|
|
} else {
|
|
|
|
body = JSON.stringify(event);
|
2024-07-22 13:13:10 +02:00
|
|
|
sendingEvent = true;
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
2023-07-05 09:42:17 +02:00
|
|
|
let extraHeaders = {};
|
|
|
|
if (typeof customHeaders === 'string' && customHeaders.length > 1) {
|
|
|
|
try {
|
|
|
|
extraHeaders = JSON.parse(customHeaders);
|
|
|
|
} catch (e) {
|
2024-07-19 13:56:55 +02:00
|
|
|
state = 'successWithErrors';
|
2024-07-22 13:13:10 +02:00
|
|
|
const badHeadersMessage =
|
|
|
|
'Could not parse the JSON in the customHeaders parameter.';
|
|
|
|
stateDetails.push(badHeadersMessage);
|
|
|
|
this.logger.warn(badHeadersMessage);
|
2023-07-05 09:42:17 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 10:42:45 +01:00
|
|
|
const requestOpts = {
|
|
|
|
method: 'POST',
|
2022-11-09 11:45:30 +01:00
|
|
|
headers: {
|
2024-07-22 13:13:10 +02:00
|
|
|
'Content-Type': contentType,
|
2022-11-09 11:45:30 +01:00
|
|
|
Authorization: authorization || undefined,
|
2023-07-05 09:42:17 +02:00
|
|
|
...extraHeaders,
|
2022-11-09 11:45:30 +01:00
|
|
|
},
|
2021-01-19 10:42:45 +01:00
|
|
|
body,
|
|
|
|
};
|
|
|
|
const res = await this.fetchRetry(url, requestOpts);
|
|
|
|
|
2024-07-19 13:56:55 +02:00
|
|
|
this.logger.info(`Handled event "${event.type}".`);
|
chore: register integration events in webhooks (#7621)
https://linear.app/unleash/issue/2-2450/register-integration-events-webhook
Registers integration events in the **Webhook** integration.
Even though this touches a lot of files, most of it is preparation for
the next steps. The only actual implementation of registering
integration events is in the **Webhook** integration. The rest will
follow on separate PRs.
Here's an example of how this looks like in the database table:
```json
{
"id": 7,
"integration_id": 2,
"created_at": "2024-07-18T18:11:11.376348+01:00",
"state": "failed",
"state_details": "Webhook request failed with status code: ECONNREFUSED",
"event": {
"id": 130,
"data": null,
"tags": [],
"type": "feature-environment-enabled",
"preData": null,
"project": "default",
"createdAt": "2024-07-18T17:11:10.821Z",
"createdBy": "admin",
"environment": "development",
"featureName": "test",
"createdByUserId": 1
},
"details": {
"url": "http://localhost:1337",
"body": "{ \"id\": 130, \"type\": \"feature-environment-enabled\", \"createdBy\": \"admin\", \"createdAt\": \"2024-07-18T17: 11: 10.821Z\", \"createdByUserId\": 1, \"data\": null, \"preData\": null, \"tags\": [], \"featureName\": \"test\", \"project\": \"default\", \"environment\": \"development\" }"
}
}
```
2024-07-19 11:07:52 +02:00
|
|
|
|
|
|
|
if (res.ok) {
|
2024-07-22 13:13:10 +02:00
|
|
|
const successMessage = `Webhook request was successful with status code: ${res.status}.`;
|
|
|
|
stateDetails.push(successMessage);
|
|
|
|
this.logger.info(successMessage);
|
chore: register integration events in webhooks (#7621)
https://linear.app/unleash/issue/2-2450/register-integration-events-webhook
Registers integration events in the **Webhook** integration.
Even though this touches a lot of files, most of it is preparation for
the next steps. The only actual implementation of registering
integration events is in the **Webhook** integration. The rest will
follow on separate PRs.
Here's an example of how this looks like in the database table:
```json
{
"id": 7,
"integration_id": 2,
"created_at": "2024-07-18T18:11:11.376348+01:00",
"state": "failed",
"state_details": "Webhook request failed with status code: ECONNREFUSED",
"event": {
"id": 130,
"data": null,
"tags": [],
"type": "feature-environment-enabled",
"preData": null,
"project": "default",
"createdAt": "2024-07-18T17:11:10.821Z",
"createdBy": "admin",
"environment": "development",
"featureName": "test",
"createdByUserId": 1
},
"details": {
"url": "http://localhost:1337",
"body": "{ \"id\": 130, \"type\": \"feature-environment-enabled\", \"createdBy\": \"admin\", \"createdAt\": \"2024-07-18T17: 11: 10.821Z\", \"createdByUserId\": 1, \"data\": null, \"preData\": null, \"tags\": [], \"featureName\": \"test\", \"project\": \"default\", \"environment\": \"development\" }"
}
}
```
2024-07-19 11:07:52 +02:00
|
|
|
} else {
|
|
|
|
state = 'failed';
|
2024-07-22 13:13:10 +02:00
|
|
|
const failedMessage = `Webhook request failed with status code: ${res.status}.`;
|
|
|
|
stateDetails.push(failedMessage);
|
|
|
|
this.logger.warn(failedMessage);
|
chore: register integration events in webhooks (#7621)
https://linear.app/unleash/issue/2-2450/register-integration-events-webhook
Registers integration events in the **Webhook** integration.
Even though this touches a lot of files, most of it is preparation for
the next steps. The only actual implementation of registering
integration events is in the **Webhook** integration. The rest will
follow on separate PRs.
Here's an example of how this looks like in the database table:
```json
{
"id": 7,
"integration_id": 2,
"created_at": "2024-07-18T18:11:11.376348+01:00",
"state": "failed",
"state_details": "Webhook request failed with status code: ECONNREFUSED",
"event": {
"id": 130,
"data": null,
"tags": [],
"type": "feature-environment-enabled",
"preData": null,
"project": "default",
"createdAt": "2024-07-18T17:11:10.821Z",
"createdBy": "admin",
"environment": "development",
"featureName": "test",
"createdByUserId": 1
},
"details": {
"url": "http://localhost:1337",
"body": "{ \"id\": 130, \"type\": \"feature-environment-enabled\", \"createdBy\": \"admin\", \"createdAt\": \"2024-07-18T17: 11: 10.821Z\", \"createdByUserId\": 1, \"data\": null, \"preData\": null, \"tags\": [], \"featureName\": \"test\", \"project\": \"default\", \"environment\": \"development\" }"
}
}
```
2024-07-19 11:07:52 +02:00
|
|
|
}
|
|
|
|
|
2024-10-10 13:23:52 +02:00
|
|
|
if (this.flagResolver.isEnabled('webhookDomainLogging')) {
|
|
|
|
const domain = new URL(url).hostname;
|
|
|
|
this.logger.info(`Webhook invoked`, {
|
|
|
|
domain,
|
|
|
|
});
|
|
|
|
}
|
2024-08-22 11:26:53 +02:00
|
|
|
|
chore: register integration events in webhooks (#7621)
https://linear.app/unleash/issue/2-2450/register-integration-events-webhook
Registers integration events in the **Webhook** integration.
Even though this touches a lot of files, most of it is preparation for
the next steps. The only actual implementation of registering
integration events is in the **Webhook** integration. The rest will
follow on separate PRs.
Here's an example of how this looks like in the database table:
```json
{
"id": 7,
"integration_id": 2,
"created_at": "2024-07-18T18:11:11.376348+01:00",
"state": "failed",
"state_details": "Webhook request failed with status code: ECONNREFUSED",
"event": {
"id": 130,
"data": null,
"tags": [],
"type": "feature-environment-enabled",
"preData": null,
"project": "default",
"createdAt": "2024-07-18T17:11:10.821Z",
"createdBy": "admin",
"environment": "development",
"featureName": "test",
"createdByUserId": 1
},
"details": {
"url": "http://localhost:1337",
"body": "{ \"id\": 130, \"type\": \"feature-environment-enabled\", \"createdBy\": \"admin\", \"createdAt\": \"2024-07-18T17: 11: 10.821Z\", \"createdByUserId\": 1, \"data\": null, \"preData\": null, \"tags\": [], \"featureName\": \"test\", \"project\": \"default\", \"environment\": \"development\" }"
}
}
```
2024-07-19 11:07:52 +02:00
|
|
|
this.registerEvent({
|
|
|
|
integrationId,
|
|
|
|
state,
|
2024-07-19 13:56:55 +02:00
|
|
|
stateDetails: stateDetails.join('\n'),
|
chore: register integration events in webhooks (#7621)
https://linear.app/unleash/issue/2-2450/register-integration-events-webhook
Registers integration events in the **Webhook** integration.
Even though this touches a lot of files, most of it is preparation for
the next steps. The only actual implementation of registering
integration events is in the **Webhook** integration. The rest will
follow on separate PRs.
Here's an example of how this looks like in the database table:
```json
{
"id": 7,
"integration_id": 2,
"created_at": "2024-07-18T18:11:11.376348+01:00",
"state": "failed",
"state_details": "Webhook request failed with status code: ECONNREFUSED",
"event": {
"id": 130,
"data": null,
"tags": [],
"type": "feature-environment-enabled",
"preData": null,
"project": "default",
"createdAt": "2024-07-18T17:11:10.821Z",
"createdBy": "admin",
"environment": "development",
"featureName": "test",
"createdByUserId": 1
},
"details": {
"url": "http://localhost:1337",
"body": "{ \"id\": 130, \"type\": \"feature-environment-enabled\", \"createdBy\": \"admin\", \"createdAt\": \"2024-07-18T17: 11: 10.821Z\", \"createdByUserId\": 1, \"data\": null, \"preData\": null, \"tags\": [], \"featureName\": \"test\", \"project\": \"default\", \"environment\": \"development\" }"
}
}
```
2024-07-19 11:07:52 +02:00
|
|
|
event: serializeDates(event),
|
|
|
|
details: {
|
|
|
|
url,
|
|
|
|
contentType,
|
2024-07-22 13:13:10 +02:00
|
|
|
body: sendingEvent ? event : body,
|
chore: register integration events in webhooks (#7621)
https://linear.app/unleash/issue/2-2450/register-integration-events-webhook
Registers integration events in the **Webhook** integration.
Even though this touches a lot of files, most of it is preparation for
the next steps. The only actual implementation of registering
integration events is in the **Webhook** integration. The rest will
follow on separate PRs.
Here's an example of how this looks like in the database table:
```json
{
"id": 7,
"integration_id": 2,
"created_at": "2024-07-18T18:11:11.376348+01:00",
"state": "failed",
"state_details": "Webhook request failed with status code: ECONNREFUSED",
"event": {
"id": 130,
"data": null,
"tags": [],
"type": "feature-environment-enabled",
"preData": null,
"project": "default",
"createdAt": "2024-07-18T17:11:10.821Z",
"createdBy": "admin",
"environment": "development",
"featureName": "test",
"createdByUserId": 1
},
"details": {
"url": "http://localhost:1337",
"body": "{ \"id\": 130, \"type\": \"feature-environment-enabled\", \"createdBy\": \"admin\", \"createdAt\": \"2024-07-18T17: 11: 10.821Z\", \"createdByUserId\": 1, \"data\": null, \"preData\": null, \"tags\": [], \"featureName\": \"test\", \"project\": \"default\", \"environment\": \"development\" }"
}
}
```
2024-07-19 11:07:52 +02:00
|
|
|
},
|
|
|
|
});
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
}
|