mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-09 00:18:00 +01:00
fix: messages to slack for archied toggles (#750)
Co-authored-by: Fredrik Strand Oseberg <fredrik.no@gmail.com>
This commit is contained in:
parent
020b9beb13
commit
f4aba80763
@ -9,3 +9,9 @@ Generated by [AVA](https://avajs.dev).
|
||||
> Snapshot 1
|
||||
|
||||
'{"username":"Unleash","icon_emoji":":unleash:","text":"some@user.com created feature toggle <http://some-url.com/#/features/strategies/some-toggle|some-toggle>\\n*Enabled*: no | *Type*: undefined | *Project*: undefined\\n*Activation strategies*: ```- name: default\\n```","channel":"#undefined","attachments":[{"actions":[{"name":"featureToggle","text":"Open in Unleash","type":"button","value":"featureToggle","style":"primary","url":"http://some-url.com/#/features/strategies/some-toggle"}]}]}'
|
||||
|
||||
## Should call slack webhook for archived toggle
|
||||
|
||||
> Snapshot 1
|
||||
|
||||
'{"username":"Unleash","icon_emoji":":unleash:","text":"The feature toggle *<http://some-url.com/#/archive/strategies/some-toggle|some-toggle>* was *archived* by some@user.com.","channel":"#undefined","attachments":[{"actions":[{"name":"featureToggle","text":"Open in Unleash","type":"button","value":"featureToggle","style":"primary","url":"http://some-url.com/#/archive/strategies/some-toggle"}]}]}'
|
||||
|
Binary file not shown.
@ -36,10 +36,10 @@ class SlackAddon extends Addon {
|
||||
|
||||
let text;
|
||||
|
||||
if (event.type === FEATURE_STALE_ON) {
|
||||
text = this.generateStaleText(event, true);
|
||||
} else if (event.type === FEATURE_STALE_OFF) {
|
||||
text = this.generateStaleText(event, false);
|
||||
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);
|
||||
} else {
|
||||
text = this.generateText(event);
|
||||
}
|
||||
@ -59,7 +59,7 @@ class SlackAddon extends Addon {
|
||||
type: 'button',
|
||||
value: 'featureToggle',
|
||||
style: 'primary',
|
||||
url: `${this.unleashUrl}/#/features/strategies/${event.data.name}`,
|
||||
url: this.featureLink(event),
|
||||
},
|
||||
],
|
||||
},
|
||||
@ -80,23 +80,38 @@ class SlackAddon extends Addon {
|
||||
this.logger.info(`Handled event ${event.type}. Status codes=${codes}`);
|
||||
}
|
||||
|
||||
featureLink(event) {
|
||||
const path = event.type === FEATURE_ARCHIVED ? 'archive' : 'features';
|
||||
return `${this.unleashUrl}/#/${path}/strategies/${event.data.name}`;
|
||||
}
|
||||
|
||||
findSlackChannels({ tags = [] }) {
|
||||
return tags.filter(tag => tag.type === 'slack').map(t => t.value);
|
||||
}
|
||||
|
||||
generateStaleText({ createdBy, data }, isStale) {
|
||||
const feature = `<${this.unleashUrl}/#/features/strategies/${data.name}|${data.name}>`;
|
||||
generateStaleText(event) {
|
||||
const { createdBy, data, type } = event;
|
||||
const isStale = type === FEATURE_STALE_ON;
|
||||
const feature = `<${this.featureLink(event)}|${data.name}>`;
|
||||
|
||||
if (isStale) {
|
||||
return `The feature toggle *${feature}* is now *ready to be removed* from the code. :technologist:
|
||||
This was changed by ${createdBy}.`;
|
||||
}
|
||||
return `The feature toggle *${feature}* was is *unmarked as stale*. This was changed by ${createdBy}.`;
|
||||
return `The feature toggle *${feature}* was *unmarked as stale* by ${createdBy}.`;
|
||||
}
|
||||
|
||||
generateText({ createdBy, data, type }) {
|
||||
const eventName = this.eventName(type);
|
||||
const feature = `<${this.unleashUrl}/#/features/strategies/${data.name}|${data.name}>`;
|
||||
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}.`;
|
||||
}
|
||||
|
||||
generateText(event) {
|
||||
const { createdBy, data, type } = event;
|
||||
const action = this.getAction(type);
|
||||
const feature = `<${this.featureLink(event)}|${data.name}>`;
|
||||
const enabled = `*Enabled*: ${data.enabled ? 'yes' : 'no'}`;
|
||||
const stale = data.stale ? '("stale")' : '';
|
||||
const typeStr = `*Type*: ${data.type}`;
|
||||
@ -105,21 +120,17 @@ This was changed by ${createdBy}.`;
|
||||
data.strategies,
|
||||
{ skipInvalid: true },
|
||||
)}\`\`\``;
|
||||
return `${createdBy} ${eventName} ${feature}
|
||||
return `${createdBy} ${action} feature toggle ${feature}
|
||||
${enabled}${stale} | ${typeStr} | ${project}
|
||||
${strategies}`;
|
||||
}
|
||||
|
||||
eventName(type) {
|
||||
getAction(type) {
|
||||
switch (type) {
|
||||
case FEATURE_CREATED:
|
||||
return 'created feature toggle';
|
||||
return 'created';
|
||||
case FEATURE_UPDATED:
|
||||
return 'updated feature toggle';
|
||||
case FEATURE_ARCHIVED:
|
||||
return 'archived feature toggle';
|
||||
case FEATURE_REVIVED:
|
||||
return 'revive feature toggle';
|
||||
return 'updated';
|
||||
default:
|
||||
return type;
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
const test = require('ava');
|
||||
const proxyquire = require('proxyquire').noCallThru();
|
||||
const { FEATURE_CREATED } = require('../event-type');
|
||||
const { FEATURE_CREATED, FEATURE_ARCHIVED } = require('../event-type');
|
||||
|
||||
const SlackAddon = proxyquire.load('./slack', {
|
||||
'./addon': class Addon {
|
||||
@ -43,6 +43,29 @@ test('Should call slack webhook', async t => {
|
||||
t.snapshot(addon.fetchRetryCalls[0].options.body);
|
||||
});
|
||||
|
||||
test('Should call slack webhook for archived toggle', async t => {
|
||||
const addon = new SlackAddon({
|
||||
getLogger: noLogger,
|
||||
unleashUrl: 'http://some-url.com',
|
||||
});
|
||||
const event = {
|
||||
type: FEATURE_ARCHIVED,
|
||||
createdBy: 'some@user.com',
|
||||
data: {
|
||||
name: 'some-toggle',
|
||||
},
|
||||
};
|
||||
|
||||
const parameters = {
|
||||
url: 'http://hooks.slack.com',
|
||||
};
|
||||
|
||||
await addon.handleEvent(event, parameters);
|
||||
t.is(addon.fetchRetryCalls.length, 1);
|
||||
t.is(addon.fetchRetryCalls[0].url, parameters.url);
|
||||
t.snapshot(addon.fetchRetryCalls[0].options.body);
|
||||
});
|
||||
|
||||
test('Should use default channel', async t => {
|
||||
const addon = new SlackAddon({
|
||||
getLogger: noLogger,
|
||||
|
Loading…
Reference in New Issue
Block a user