2021-08-12 15:04:37 +02:00
|
|
|
import YAML from 'js-yaml';
|
|
|
|
import Addon from './addon';
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
import slackDefinition from './slack-definition';
|
|
|
|
import { IAddonConfig, IEvent } from '../types/model';
|
2021-01-19 10:42:45 +01:00
|
|
|
|
|
|
|
const {
|
|
|
|
FEATURE_CREATED,
|
|
|
|
FEATURE_UPDATED,
|
|
|
|
FEATURE_ARCHIVED,
|
|
|
|
FEATURE_REVIVED,
|
2021-02-18 09:18:04 +01:00
|
|
|
FEATURE_STALE_ON,
|
|
|
|
FEATURE_STALE_OFF,
|
2021-04-29 10:21:29 +02:00
|
|
|
} = require('../types/events');
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
export default class SlackAddon extends Addon {
|
|
|
|
unleashUrl: string;
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
constructor(args: IAddonConfig) {
|
|
|
|
super(slackDefinition, args);
|
2021-02-05 11:30:30 +01:00
|
|
|
this.unleashUrl = args.unleashUrl;
|
2021-01-19 10:42:45 +01: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> {
|
2021-01-19 10:42:45 +01:00
|
|
|
const {
|
|
|
|
url,
|
|
|
|
defaultChannel,
|
|
|
|
username = 'Unleash',
|
|
|
|
iconEmoji = ':unleash:',
|
|
|
|
} = parameters;
|
|
|
|
|
|
|
|
const slackChannels = this.findSlackChannels(event);
|
|
|
|
|
|
|
|
if (slackChannels.length === 0) {
|
|
|
|
slackChannels.push(defaultChannel);
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:18:04 +01:00
|
|
|
let text;
|
|
|
|
|
2021-03-05 12:59:35 +01:00
|
|
|
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-02-18 09:18:04 +01:00
|
|
|
} else {
|
|
|
|
text = this.generateText(event);
|
|
|
|
}
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
const requests = slackChannels.map((channel) => {
|
2021-01-19 10:42:45 +01:00
|
|
|
const body = {
|
|
|
|
username,
|
|
|
|
icon_emoji: iconEmoji, // eslint-disable-line camelcase
|
|
|
|
text,
|
|
|
|
channel: `#${channel}`,
|
|
|
|
attachments: [
|
|
|
|
{
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
name: 'featureToggle',
|
|
|
|
text: 'Open in Unleash',
|
|
|
|
type: 'button',
|
|
|
|
value: 'featureToggle',
|
|
|
|
style: 'primary',
|
2021-03-05 12:59:35 +01:00
|
|
|
url: this.featureLink(event),
|
2021-01-19 10:42:45 +01:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const requestOpts = {
|
|
|
|
method: 'POST',
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.fetchRetry(url, requestOpts);
|
|
|
|
});
|
|
|
|
|
|
|
|
const results = await Promise.all(requests);
|
2021-08-12 15:04:37 +02:00
|
|
|
const codes = results.map((res) => res.status).join(', ');
|
2021-01-19 10:42:45 +01:00
|
|
|
this.logger.info(`Handled event ${event.type}. Status codes=${codes}`);
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
featureLink(event: IEvent): string {
|
2021-03-05 12:59:35 +01:00
|
|
|
const path = event.type === FEATURE_ARCHIVED ? 'archive' : 'features';
|
2021-05-07 14:03:15 +02:00
|
|
|
return `${this.unleashUrl}/${path}/strategies/${event.data.name}`;
|
2021-03-05 12:59:35 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
findSlackChannels({ tags }: Pick<IEvent, 'tags'>): string[] {
|
|
|
|
if (tags) {
|
|
|
|
return tags
|
|
|
|
.filter((tag) => tag.type === 'slack')
|
|
|
|
.map((t) => t.value);
|
|
|
|
}
|
|
|
|
return [];
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
generateStaleText(event: IEvent): string {
|
2021-03-05 12:59:35 +01:00
|
|
|
const { createdBy, data, type } = event;
|
|
|
|
const isStale = type === FEATURE_STALE_ON;
|
|
|
|
const feature = `<${this.featureLink(event)}|${data.name}>`;
|
2021-02-18 09:18:04 +01:00
|
|
|
|
|
|
|
if (isStale) {
|
|
|
|
return `The feature toggle *${feature}* is now *ready to be removed* from the code. :technologist:
|
|
|
|
This was changed by ${createdBy}.`;
|
|
|
|
}
|
2021-03-05 12:59:35 +01:00
|
|
|
return `The feature toggle *${feature}* was *unmarked as stale* by ${createdBy}.`;
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
generateArchivedText(event: IEvent): string {
|
2021-03-05 12:59:35 +01:00
|
|
|
const { createdBy, data, type } = event;
|
|
|
|
const action = type === FEATURE_ARCHIVED ? 'archived' : 'revived';
|
|
|
|
const feature = `<${this.featureLink(event)}|${data.name}>`;
|
|
|
|
return `The feature toggle *${feature}* was *${action}* by ${createdBy}.`;
|
2021-02-18 09:18:04 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
generateText(event: IEvent): string {
|
2021-03-05 12:59:35 +01:00
|
|
|
const { createdBy, data, type } = event;
|
|
|
|
const action = this.getAction(type);
|
|
|
|
const feature = `<${this.featureLink(event)}|${data.name}>`;
|
2021-01-19 10:42:45 +01:00
|
|
|
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 23:00:47 +02:00
|
|
|
const strategies = `*Activation strategies*: \`\`\`${YAML.safeDump(
|
2021-01-19 10:42:45 +01:00
|
|
|
data.strategies,
|
|
|
|
{ skipInvalid: true },
|
|
|
|
)}\`\`\``;
|
2021-03-05 12:59:35 +01:00
|
|
|
return `${createdBy} ${action} feature toggle ${feature}
|
2021-01-19 10:42:45 +01:00
|
|
|
${enabled}${stale} | ${typeStr} | ${project}
|
2021-08-27 10:15:56 +02:00
|
|
|
${data.strategies ? strategies : ''}`;
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
getAction(type: string): string {
|
2021-01-19 10:42:45 +01:00
|
|
|
switch (type) {
|
|
|
|
case FEATURE_CREATED:
|
2021-03-05 12:59:35 +01:00
|
|
|
return 'created';
|
2021-01-19 10:42:45 +01:00
|
|
|
case FEATURE_UPDATED:
|
2021-03-05 12:59:35 +01:00
|
|
|
return 'updated';
|
2021-01-19 10:42:45 +01:00
|
|
|
default:
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SlackAddon;
|