2021-08-12 15:04:37 +02:00
|
|
|
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
|
|
|
|
2021-09-29 12:57:58 +02:00
|
|
|
import {
|
2021-10-28 14:09:11 +02:00
|
|
|
FeatureEventFormatter,
|
|
|
|
FeatureEventFormatterMd,
|
|
|
|
LinkStyle,
|
|
|
|
} from './feature-event-formatter-md';
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
export default class SlackAddon extends Addon {
|
2021-10-28 14:09:11 +02:00
|
|
|
private msgFormatter: FeatureEventFormatter;
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
constructor(args: IAddonConfig) {
|
|
|
|
super(slackDefinition, args);
|
2021-10-28 14:09:11 +02:00
|
|
|
this.msgFormatter = new FeatureEventFormatterMd(
|
|
|
|
args.unleashUrl,
|
|
|
|
LinkStyle.SLACK,
|
|
|
|
);
|
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-10-28 14:09:11 +02:00
|
|
|
const text = this.msgFormatter.format(event);
|
|
|
|
const featureLink = this.msgFormatter.featureLink(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-10-28 14:09:11 +02:00
|
|
|
url: featureLink,
|
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
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SlackAddon;
|