2021-08-12 15:04:37 +02:00
|
|
|
import YAML from 'js-yaml';
|
|
|
|
import Addon from './addon';
|
|
|
|
import {
|
2021-05-03 22:08:14 +02:00
|
|
|
FEATURE_CREATED,
|
|
|
|
FEATURE_UPDATED,
|
|
|
|
FEATURE_ARCHIVED,
|
|
|
|
FEATURE_REVIVED,
|
|
|
|
FEATURE_STALE_ON,
|
|
|
|
FEATURE_STALE_OFF,
|
2021-10-07 10:22:20 +02:00
|
|
|
FEATURE_ENVIRONMENT_ENABLED,
|
|
|
|
FEATURE_STRATEGY_UPDATE,
|
|
|
|
FEATURE_STRATEGY_ADD,
|
|
|
|
FEATURE_ENVIRONMENT_DISABLED,
|
|
|
|
FEATURE_STRATEGY_REMOVE,
|
|
|
|
FEATURE_METADATA_UPDATED,
|
|
|
|
FEATURE_PROJECT_CHANGE,
|
2021-08-12 15:04:37 +02:00
|
|
|
} from '../types/events';
|
2021-05-03 22:08:14 +02:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
import definition from './datadog-definition';
|
|
|
|
import { LogProvider } from '../logger';
|
|
|
|
import { IEvent } from '../types/model';
|
2021-05-03 22:08:14 +02:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
export default class DatadogAddon extends Addon {
|
|
|
|
unleashUrl: string;
|
|
|
|
|
|
|
|
constructor(config: { unleashUrl: string; getLogger: LogProvider }) {
|
|
|
|
super(definition, config);
|
|
|
|
this.unleashUrl = config.unleashUrl;
|
2021-05-03 22:08:14 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async handleEvent(event: IEvent, parameters: any): Promise<void> {
|
|
|
|
const { url = 'https://api.datadoghq.com/api/v1/events', apiKey } =
|
|
|
|
parameters;
|
2021-05-03 22:08:14 +02:00
|
|
|
let text;
|
|
|
|
|
|
|
|
if ([FEATURE_ARCHIVED, FEATURE_REVIVED].includes(event.type)) {
|
|
|
|
text = this.generateArchivedText(event);
|
|
|
|
} else if ([FEATURE_STALE_ON, FEATURE_STALE_OFF].includes(event.type)) {
|
|
|
|
text = this.generateStaleText(event);
|
2021-10-07 10:22:20 +02:00
|
|
|
} else if (
|
|
|
|
[
|
|
|
|
FEATURE_ENVIRONMENT_DISABLED,
|
|
|
|
FEATURE_ENVIRONMENT_ENABLED,
|
|
|
|
].includes(event.type)
|
|
|
|
) {
|
|
|
|
text = this.generateEnvironmentToggleText(event);
|
|
|
|
} else if (
|
|
|
|
[
|
|
|
|
FEATURE_STRATEGY_ADD,
|
|
|
|
FEATURE_STRATEGY_REMOVE,
|
|
|
|
FEATURE_STRATEGY_UPDATE,
|
|
|
|
].includes(event.type)
|
|
|
|
) {
|
|
|
|
text = this.generateStrategyChangeText(event);
|
|
|
|
} else if (FEATURE_METADATA_UPDATED === event.type) {
|
|
|
|
text = this.generateMetadataText(event);
|
|
|
|
} else if (FEATURE_PROJECT_CHANGE === event.type) {
|
|
|
|
text = this.generateProjectChangeText(event);
|
2021-05-03 22:08:14 +02:00
|
|
|
} else {
|
|
|
|
text = this.generateText(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { tags: eventTags } = event;
|
|
|
|
const tags =
|
2021-08-12 15:04:37 +02:00
|
|
|
eventTags && eventTags.map((tag) => `${tag.value}:${tag.type}`);
|
2021-05-03 22:08:14 +02:00
|
|
|
const body = {
|
|
|
|
text: `%%% \n ${text} \n %%% `,
|
|
|
|
title: 'Unleash notification update',
|
|
|
|
tags,
|
|
|
|
};
|
|
|
|
|
|
|
|
const requestOpts = {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'DD-API-KEY': apiKey,
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
};
|
|
|
|
const res = await this.fetchRetry(url, requestOpts);
|
|
|
|
this.logger.info(
|
|
|
|
`Handled event ${event.type}. Status codes=${res.status}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-07 10:22:20 +02:00
|
|
|
generateEnvironmentToggleText(event: IEvent): string {
|
|
|
|
const { environment, project, data, type } = event;
|
|
|
|
const toggleStatus =
|
|
|
|
type === FEATURE_ENVIRONMENT_ENABLED ? 'enabled' : 'disabled';
|
|
|
|
const feature = `<${this.featureLink(event)}|${data.name}>`;
|
|
|
|
return `The feature toggle *${feature}* in the ${project} project was ${toggleStatus} in environment *${environment}*`;
|
|
|
|
}
|
|
|
|
|
|
|
|
generateStrategyChangeText(event: IEvent): string {
|
|
|
|
const { environment, project, data, type } = event;
|
|
|
|
const feature = `<${this.strategiesLink(event)}|${data.featureName}>`;
|
|
|
|
let action;
|
|
|
|
if (FEATURE_STRATEGY_UPDATE === type) {
|
|
|
|
action = 'updated in';
|
|
|
|
} else if (FEATURE_STRATEGY_ADD) {
|
|
|
|
action = 'added to';
|
|
|
|
} else {
|
|
|
|
action = 'removed from';
|
|
|
|
}
|
|
|
|
const strategyText = `a ${data.name} strategy ${action} the *${environment}* environment`;
|
|
|
|
return `The feature toggle *${feature}* in project: ${project} had ${strategyText}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
generateMetadataText(event: IEvent): string {
|
|
|
|
const { createdBy, project, data } = event;
|
|
|
|
const feature = `<${this.featureLink(event)}|${data.name}>`;
|
|
|
|
return `${createdBy} updated the metadata for ${feature} in project ${project}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
generateProjectChangeText(event: IEvent): string {
|
|
|
|
const { createdBy, project, data } = event;
|
|
|
|
return `${createdBy} moved ${data.name} to ${project}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
strategiesLink(event: IEvent): string {
|
|
|
|
return `${this.unleashUrl}/projects/${event.project}/features2/${event.data.featureName}/strategies?environment=${event.environment}`;
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
featureLink(event: IEvent): string {
|
2021-05-03 22:08:14 +02:00
|
|
|
const path = event.type === FEATURE_ARCHIVED ? 'archive' : 'features';
|
2021-05-07 14:05:42 +02:00
|
|
|
return `${this.unleashUrl}/${path}/strategies/${event.data.name}`;
|
2021-05-03 22:08:14 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
generateStaleText(event: IEvent): string {
|
2021-05-03 22:08:14 +02:00
|
|
|
const { createdBy, data, type } = event;
|
|
|
|
const isStale = type === FEATURE_STALE_ON;
|
|
|
|
const feature = `[${data.name}](${this.featureLink(event)})`;
|
|
|
|
|
|
|
|
if (isStale) {
|
2021-05-07 14:05:42 +02:00
|
|
|
return `The feature toggle *${feature}* is now *ready to be removed* from the code.
|
2021-05-03 22:08:14 +02:00
|
|
|
This was changed by ${createdBy}.`;
|
|
|
|
}
|
|
|
|
return `The feature toggle *${feature}* was *unmarked as stale* by ${createdBy}.`;
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
generateArchivedText(event: IEvent): string {
|
2021-05-03 22:08:14 +02:00
|
|
|
const { createdBy, data, type } = event;
|
|
|
|
const action = type === FEATURE_ARCHIVED ? 'archived' : 'revived';
|
|
|
|
const feature = `[${data.name}](${this.featureLink(event)})`;
|
|
|
|
return `The feature toggle *${feature}* was *${action}* by ${createdBy}.`;
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
generateText(event: IEvent): string {
|
2021-05-03 22:08:14 +02:00
|
|
|
const { createdBy, data, type } = event;
|
|
|
|
const action = this.getAction(type);
|
|
|
|
const feature = `[${data.name}](${this.featureLink(event)})`;
|
|
|
|
const enabled = `**Enabled**: ${data.enabled ? 'yes' : 'no'}`;
|
|
|
|
const stale = data.stale ? '("stale")' : '';
|
|
|
|
const typeStr = `**Type**: ${data.type}`;
|
|
|
|
const project = `**Project**: ${data.project}`;
|
2021-08-12 15:04:37 +02:00
|
|
|
const strategies = `**Activation strategies**: \`\`\`${YAML.dump(
|
2021-05-03 22:08:14 +02:00
|
|
|
data.strategies,
|
|
|
|
{ skipInvalid: true },
|
|
|
|
)}\`\`\``;
|
|
|
|
return `${createdBy} ${action} feature toggle ${feature}
|
|
|
|
${enabled}${stale} | ${typeStr} | ${project}
|
2021-08-27 10:15:56 +02:00
|
|
|
${data.strategies ? strategies : ''}`;
|
2021-05-03 22:08:14 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
getAction(type: string): string {
|
2021-05-03 22:08:14 +02:00
|
|
|
switch (type) {
|
|
|
|
case FEATURE_CREATED:
|
|
|
|
return 'created';
|
|
|
|
case FEATURE_UPDATED:
|
|
|
|
return 'updated';
|
|
|
|
default:
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|