diff --git a/packages/api/src/tasks/__tests__/checkAlerts.test.ts b/packages/api/src/tasks/__tests__/checkAlerts.test.ts index 96070f0dd..ff8ecaa83 100644 --- a/packages/api/src/tasks/__tests__/checkAlerts.test.ts +++ b/packages/api/src/tasks/__tests__/checkAlerts.test.ts @@ -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 () => { diff --git a/packages/api/src/tasks/checkAlerts.ts b/packages/api/src/tasks/checkAlerts.ts index 0d293af73..dfd71b792 100644 --- a/packages/api/src/tasks/checkAlerts.ts +++ b/packages/api/src/tasks/checkAlerts.ts @@ -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') { @@ -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}"}}`; });