mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-14 00:19:16 +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
|
> 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"}]}]}'
|
'{"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;
|
let text;
|
||||||
|
|
||||||
if (event.type === FEATURE_STALE_ON) {
|
if ([FEATURE_ARCHIVED, FEATURE_REVIVED].includes(event.type)) {
|
||||||
text = this.generateStaleText(event, true);
|
text = this.generateArchivedText(event);
|
||||||
} else if (event.type === FEATURE_STALE_OFF) {
|
} else if ([FEATURE_STALE_ON, FEATURE_STALE_OFF].includes(event.type)) {
|
||||||
text = this.generateStaleText(event, false);
|
text = this.generateStaleText(event);
|
||||||
} else {
|
} else {
|
||||||
text = this.generateText(event);
|
text = this.generateText(event);
|
||||||
}
|
}
|
||||||
@ -59,7 +59,7 @@ class SlackAddon extends Addon {
|
|||||||
type: 'button',
|
type: 'button',
|
||||||
value: 'featureToggle',
|
value: 'featureToggle',
|
||||||
style: 'primary',
|
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}`);
|
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 = [] }) {
|
findSlackChannels({ tags = [] }) {
|
||||||
return tags.filter(tag => tag.type === 'slack').map(t => t.value);
|
return tags.filter(tag => tag.type === 'slack').map(t => t.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
generateStaleText({ createdBy, data }, isStale) {
|
generateStaleText(event) {
|
||||||
const feature = `<${this.unleashUrl}/#/features/strategies/${data.name}|${data.name}>`;
|
const { createdBy, data, type } = event;
|
||||||
|
const isStale = type === FEATURE_STALE_ON;
|
||||||
|
const feature = `<${this.featureLink(event)}|${data.name}>`;
|
||||||
|
|
||||||
if (isStale) {
|
if (isStale) {
|
||||||
return `The feature toggle *${feature}* is now *ready to be removed* from the code. :technologist:
|
return `The feature toggle *${feature}* is now *ready to be removed* from the code. :technologist:
|
||||||
This was changed by ${createdBy}.`;
|
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 }) {
|
generateArchivedText(event) {
|
||||||
const eventName = this.eventName(type);
|
const { createdBy, data, type } = event;
|
||||||
const feature = `<${this.unleashUrl}/#/features/strategies/${data.name}|${data.name}>`;
|
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 enabled = `*Enabled*: ${data.enabled ? 'yes' : 'no'}`;
|
||||||
const stale = data.stale ? '("stale")' : '';
|
const stale = data.stale ? '("stale")' : '';
|
||||||
const typeStr = `*Type*: ${data.type}`;
|
const typeStr = `*Type*: ${data.type}`;
|
||||||
@ -105,21 +120,17 @@ This was changed by ${createdBy}.`;
|
|||||||
data.strategies,
|
data.strategies,
|
||||||
{ skipInvalid: true },
|
{ skipInvalid: true },
|
||||||
)}\`\`\``;
|
)}\`\`\``;
|
||||||
return `${createdBy} ${eventName} ${feature}
|
return `${createdBy} ${action} feature toggle ${feature}
|
||||||
${enabled}${stale} | ${typeStr} | ${project}
|
${enabled}${stale} | ${typeStr} | ${project}
|
||||||
${strategies}`;
|
${strategies}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
eventName(type) {
|
getAction(type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case FEATURE_CREATED:
|
case FEATURE_CREATED:
|
||||||
return 'created feature toggle';
|
return 'created';
|
||||||
case FEATURE_UPDATED:
|
case FEATURE_UPDATED:
|
||||||
return 'updated feature toggle';
|
return 'updated';
|
||||||
case FEATURE_ARCHIVED:
|
|
||||||
return 'archived feature toggle';
|
|
||||||
case FEATURE_REVIVED:
|
|
||||||
return 'revive feature toggle';
|
|
||||||
default:
|
default:
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
const test = require('ava');
|
const test = require('ava');
|
||||||
const proxyquire = require('proxyquire').noCallThru();
|
const proxyquire = require('proxyquire').noCallThru();
|
||||||
const { FEATURE_CREATED } = require('../event-type');
|
const { FEATURE_CREATED, FEATURE_ARCHIVED } = require('../event-type');
|
||||||
|
|
||||||
const SlackAddon = proxyquire.load('./slack', {
|
const SlackAddon = proxyquire.load('./slack', {
|
||||||
'./addon': class Addon {
|
'./addon': class Addon {
|
||||||
@ -43,6 +43,29 @@ test('Should call slack webhook', async t => {
|
|||||||
t.snapshot(addon.fetchRetryCalls[0].options.body);
|
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 => {
|
test('Should use default channel', async t => {
|
||||||
const addon = new SlackAddon({
|
const addon = new SlackAddon({
|
||||||
getLogger: noLogger,
|
getLogger: noLogger,
|
||||||
|
Loading…
Reference in New Issue
Block a user