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';
|
2021-11-12 13:15:51 +01:00
|
|
|
import { IAddonConfig } 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-11-12 13:15:51 +01:00
|
|
|
import { IEvent } from '../types/events';
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2023-07-05 09:42:17 +02:00
|
|
|
interface ISlackAddonParameters {
|
|
|
|
url: string;
|
|
|
|
username?: string;
|
|
|
|
defaultChannel: string;
|
|
|
|
emojiIcon?: string;
|
|
|
|
customHeaders?: string;
|
|
|
|
}
|
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
|
2023-07-05 09:42:17 +02:00
|
|
|
async handleEvent(
|
|
|
|
event: IEvent,
|
|
|
|
parameters: ISlackAddonParameters,
|
|
|
|
): Promise<void> {
|
2021-01-19 10:42:45 +01:00
|
|
|
const {
|
|
|
|
url,
|
|
|
|
defaultChannel,
|
|
|
|
username = 'Unleash',
|
2023-07-05 09:42:17 +02:00
|
|
|
emojiIcon = ':unleash:',
|
|
|
|
customHeaders,
|
2021-01-19 10:42:45 +01:00
|
|
|
} = 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,
|
2023-07-05 09:42:17 +02:00
|
|
|
icon_emoji: emojiIcon, // eslint-disable-line camelcase
|
2021-01-19 10:42:45 +01:00
|
|
|
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
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
2023-07-05 09:42:17 +02:00
|
|
|
let extraHeaders = {};
|
|
|
|
if (typeof customHeaders === 'string' && customHeaders.length > 1) {
|
|
|
|
try {
|
|
|
|
extraHeaders = JSON.parse(customHeaders);
|
|
|
|
} catch (e) {
|
|
|
|
this.logger.warn(
|
|
|
|
`Could not parse the json in the customHeaders parameter. [${customHeaders}]`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-01-19 10:42:45 +01:00
|
|
|
const requestOpts = {
|
|
|
|
method: 'POST',
|
2023-07-05 09:42:17 +02:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...extraHeaders,
|
|
|
|
},
|
2021-01-19 10:42:45 +01:00
|
|
|
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;
|