1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/src/lib/addons/slack.ts
Christopher Kolstad 186fda1657
task: Add customHeaders as possible parameter. (#4139)
###What
Adds an optional sensitive parameter for customHeaders to all current
addons. It is sensitive because the user might be including api key headers.
2023-07-05 09:42:17 +02:00

112 lines
3.4 KiB
TypeScript

import Addon from './addon';
import slackDefinition from './slack-definition';
import { IAddonConfig } from '../types/model';
import {
FeatureEventFormatter,
FeatureEventFormatterMd,
LinkStyle,
} from './feature-event-formatter-md';
import { IEvent } from '../types/events';
interface ISlackAddonParameters {
url: string;
username?: string;
defaultChannel: string;
emojiIcon?: string;
customHeaders?: string;
}
export default class SlackAddon extends Addon {
private msgFormatter: FeatureEventFormatter;
constructor(args: IAddonConfig) {
super(slackDefinition, args);
this.msgFormatter = new FeatureEventFormatterMd(
args.unleashUrl,
LinkStyle.SLACK,
);
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async handleEvent(
event: IEvent,
parameters: ISlackAddonParameters,
): Promise<void> {
const {
url,
defaultChannel,
username = 'Unleash',
emojiIcon = ':unleash:',
customHeaders,
} = parameters;
const slackChannels = this.findSlackChannels(event);
if (slackChannels.length === 0) {
slackChannels.push(defaultChannel);
}
const text = this.msgFormatter.format(event);
const featureLink = this.msgFormatter.featureLink(event);
const requests = slackChannels.map((channel) => {
const body = {
username,
icon_emoji: emojiIcon, // eslint-disable-line camelcase
text,
channel: `#${channel}`,
attachments: [
{
actions: [
{
name: 'featureToggle',
text: 'Open in Unleash',
type: 'button',
value: 'featureToggle',
style: 'primary',
url: featureLink,
},
],
},
],
};
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}]`,
);
}
}
const requestOpts = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...extraHeaders,
},
body: JSON.stringify(body),
};
return this.fetchRetry(url, requestOpts);
});
const results = await Promise.all(requests);
const codes = results.map((res) => res.status).join(', ');
this.logger.info(`Handled event ${event.type}. Status codes=${codes}`);
}
findSlackChannels({ tags }: Pick<IEvent, 'tags'>): string[] {
if (tags) {
return tags
.filter((tag) => tag.type === 'slack')
.map((t) => t.value);
}
return [];
}
}
module.exports = SlackAddon;