2021-01-19 10:42:45 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const YAML = require('js-yaml');
|
|
|
|
const Addon = require('./addon');
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
const definition = require('./slack-definition');
|
|
|
|
|
|
|
|
class SlackAddon extends Addon {
|
|
|
|
constructor(args) {
|
|
|
|
super(definition, args);
|
2021-02-05 11:30:30 +01:00
|
|
|
this.unleashUrl = args.unleashUrl;
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async handleEvent(event, parameters) {
|
|
|
|
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
|
|
|
|
|
|
|
const requests = slackChannels.map(channel => {
|
|
|
|
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);
|
|
|
|
const codes = results.map(res => res.status).join(', ');
|
|
|
|
this.logger.info(`Handled event ${event.type}. Status codes=${codes}`);
|
|
|
|
}
|
|
|
|
|
2021-03-05 12:59:35 +01:00
|
|
|
featureLink(event) {
|
|
|
|
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-01-19 10:42:45 +01:00
|
|
|
findSlackChannels({ tags = [] }) {
|
|
|
|
return tags.filter(tag => tag.type === 'slack').map(t => t.value);
|
|
|
|
}
|
|
|
|
|
2021-03-05 12:59:35 +01:00
|
|
|
generateStaleText(event) {
|
|
|
|
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}.`;
|
|
|
|
}
|
|
|
|
|
|
|
|
generateArchivedText(event) {
|
|
|
|
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-03-05 12:59:35 +01:00
|
|
|
generateText(event) {
|
|
|
|
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}`;
|
|
|
|
const strategies = `*Activation strategies*: \`\`\`${YAML.safeDump(
|
|
|
|
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}
|
|
|
|
${strategies}`;
|
|
|
|
}
|
|
|
|
|
2021-03-05 12:59:35 +01:00
|
|
|
getAction(type) {
|
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;
|