From 8fd0f682d9da3ee648263bb0d847517794b0446f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gangloff?= Date: Wed, 4 Sep 2024 15:33:43 +0200 Subject: [PATCH] feat: Google Chat notifications (#867) --- docker-compose.yml | 1 + server/.env.sample | 1 + server/api/helpers/actions/create-one.js | 20 ++++++--- server/api/helpers/cards/delete-one.js | 10 +++-- .../helpers/utils/send-google-chat-message.js | 41 +++++++++++++++++++ server/config/custom.js | 1 + 6 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 server/api/helpers/utils/send-google-chat-message.js diff --git a/docker-compose.yml b/docker-compose.yml index 28ba07b89..ecbf6eaa4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -72,6 +72,7 @@ services: # - SLACK_BOT_TOKEN= # - SLACK_CHANNEL_ID= + # - GOOGLE_CHAT_WEBHOOK_URL= depends_on: postgres: condition: service_healthy diff --git a/server/.env.sample b/server/.env.sample index 32a91dd59..c7418927c 100644 --- a/server/.env.sample +++ b/server/.env.sample @@ -61,6 +61,7 @@ SECRET_KEY=notsecretkey # SLACK_BOT_TOKEN= # SLACK_CHANNEL_ID= +# GOOGLE_CHAT_WEBHOOK_URL= ## Do not edit this diff --git a/server/api/helpers/actions/create-one.js b/server/api/helpers/actions/create-one.js index fb0d18563..eb9badf72 100644 --- a/server/api/helpers/actions/create-one.js +++ b/server/api/helpers/actions/create-one.js @@ -14,7 +14,7 @@ const valuesValidator = (value) => { return true; }; -const buildAndSendSlackMessage = async (card, action, actorUser) => { +const buildAndSendMessage = async (card, action, actorUser, send) => { const cardLink = `<${sails.config.custom.baseUrl}/cards/${card.id}|${card.name}>`; let markdown; @@ -35,7 +35,7 @@ const buildAndSendSlackMessage = async (card, action, actorUser) => { return; } - await sails.helpers.utils.sendSlackMessage(markdown); + await send(markdown); }; module.exports = { @@ -94,10 +94,6 @@ module.exports = { user: values.user, }); - if (sails.config.custom.slackBotToken) { - buildAndSendSlackMessage(values.card, action, values.user); - } - const subscriptionUserIds = await sails.helpers.cards.getSubscriptionUserIds( action.cardId, action.userId, @@ -119,6 +115,18 @@ module.exports = { ), ); + if (sails.config.custom.slackBotToken) { + buildAndSendMessage(values.card, action, values.user, sails.helpers.utils.sendSlackMessage); + } + + if (sails.config.custom.googleChatWebhookUrl) { + buildAndSendMessage( + values.card, + action, + values.user, + sails.helpers.utils.sendGoogleChatMessage, + ); + } return action; }, }; diff --git a/server/api/helpers/cards/delete-one.js b/server/api/helpers/cards/delete-one.js index 1400bfcb8..a7a35967e 100644 --- a/server/api/helpers/cards/delete-one.js +++ b/server/api/helpers/cards/delete-one.js @@ -1,5 +1,5 @@ -const buildAndSendSlackMessage = async (card, actorUser) => { - await sails.helpers.utils.sendSlackMessage(`*${card.name}* was deleted by ${actorUser.name}`); +const buildAndSendMessage = async (card, actorUser, send) => { + await send(`*${card.name}* was deleted by ${actorUser.name}`); }; module.exports = { @@ -56,7 +56,11 @@ module.exports = { }); if (sails.config.custom.slackBotToken) { - buildAndSendSlackMessage(card, inputs.actorUser); + buildAndSendMessage(card, inputs.actorUser, sails.helpers.utils.sendSlackMessage); + } + + if (sails.config.custom.googleChatWebhookUrl) { + buildAndSendMessage(card, inputs.actorUser, sails.helpers.utils.sendGoogleChatMessage); } } diff --git a/server/api/helpers/utils/send-google-chat-message.js b/server/api/helpers/utils/send-google-chat-message.js new file mode 100644 index 000000000..e000f208a --- /dev/null +++ b/server/api/helpers/utils/send-google-chat-message.js @@ -0,0 +1,41 @@ +module.exports = { + inputs: { + markdown: { + type: 'string', + required: true, + }, + }, + + async fn(inputs) { + const headers = { + 'Content-Type': 'application/json; charset=utf-8', + }; + + const body = { + text: inputs.markdown, + }; + + let response; + try { + response = await fetch(sails.config.custom.googleChatWebhookUrl, { + headers, + method: 'POST', + body: JSON.stringify(body), + }); + } catch (error) { + sails.log.error(`Error sending to Google Chat: ${error}`); + return; + } + + if (!response.ok) { + sails.log.error(`Error sending to Google Chat: ${response.error}`); + return; + } + + const responseJson = await response.json(); + + if (!responseJson.ok) { + sails.log.error(`Error sending to Google Chat: ${responseJson.error}`); + } + }, +}; diff --git a/server/config/custom.js b/server/config/custom.js index 8047b73d9..cfafaf016 100644 --- a/server/config/custom.js +++ b/server/config/custom.js @@ -77,4 +77,5 @@ module.exports.custom = { slackBotToken: process.env.SLACK_BOT_TOKEN, slackChannelId: process.env.SLACK_CHANNEL_ID, + googleChatWebhookUrl: process.env.GOOGLE_CHAT_WEBHOOK_URL, };