Skip to content

Commit

Permalink
feat: id can be webhook name
Browse files Browse the repository at this point in the history
  • Loading branch information
wrn14897 committed Feb 28, 2024
1 parent 6b5c9c5 commit 202824d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
17 changes: 15 additions & 2 deletions packages/api/src/tasks/__tests__/checkAlerts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,27 +191,40 @@ describe('checkAlerts', () => {
});

it('translateExternalActionsToInternal', () => {
// normal
expect(
translateExternalActionsToInternal('@slack_webhook-123'),
).toMatchInlineSnapshot(
`"{{__hdx_notify_channel__ channel=\\"slack_webhook\\" id=\\"123\\"}}"`,
);

// with body string
expect(
translateExternalActionsToInternal('blabla @action-id'),
).toMatchInlineSnapshot(
`"blabla {{__hdx_notify_channel__ channel=\\"action\\" id=\\"id\\"}}"`,
);

// multiple actions
expect(
translateExternalActionsToInternal('blabla @action-id @action2-id2'),
).toMatchInlineSnapshot(
`"blabla {{__hdx_notify_channel__ channel=\\"action\\" id=\\"id\\"}} {{__hdx_notify_channel__ channel=\\"action2\\" id=\\"id2\\"}}"`,
);

// id with special characters
expect(
translateExternalActionsToInternal('email: mike@hyperdx.io'),
).toMatchInlineSnapshot(`"email: mike@hyperdx.io"`);
translateExternalActionsToInternal('send @email-mike@hyperdx.io'),
).toMatchInlineSnapshot(
`"send {{__hdx_notify_channel__ channel=\\"email\\" id=\\"mike@hyperdx.io\\"}}"`,
);

// id with multiple dashes
expect(
translateExternalActionsToInternal('@action-id-with-multiple-dashes'),
).toMatchInlineSnapshot(
`"{{__hdx_notify_channel__ channel=\\"action\\" id=\\"id-with-multiple-dashes\\"}}"`,
);
});

it('renderAlertTemplate', async () => {
Expand Down
14 changes: 11 additions & 3 deletions packages/api/src/tasks/checkAlerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,15 @@ export const notifyChannel = async ({
switch (channel) {
case 'slack_webhook': {
const webhook = await Webhook.findOne({
_id: id,
team: teamId,
$or: [
{
_id: id,
},
{
name: id,
},
],
});
// ONLY SUPPORTS SLACK WEBHOOKS FOR NOW
if (webhook?.service === 'slack') {
Expand Down Expand Up @@ -265,9 +272,10 @@ export const getDefaultExternalAction = (

export const translateExternalActionsToInternal = (template: string) => {
// ex: @slack_webhook-1234_5678 -> "{{NOTIFY_FN_NAME channel="slack_webhook" id="1234_5678}}"
return template.replace(/(?: |^)@([a-zA-Z0-9_-]+)/g, (match, input) => {
return template.replace(/(?: |^)@([a-zA-Z0-9.@_-]+)/g, (match, input) => {
const prefix = match.startsWith(' ') ? ' ' : '';
const [channel, id] = input.split('-');
const [channel, ...ids] = input.split('-');
const id = ids.join('-');
// TODO: sanity check ??
return `${prefix}{{${NOTIFY_FN_NAME} channel="${channel}" id="${id}"}}`;
});
Expand Down

0 comments on commit 202824d

Please sign in to comment.