Skip to content

Commit

Permalink
feat: Google Chat notifications (#867)
Browse files Browse the repository at this point in the history
  • Loading branch information
maelgangloff authored Sep 4, 2024
1 parent 9699fbe commit 8fd0f68
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ services:

# - SLACK_BOT_TOKEN=
# - SLACK_CHANNEL_ID=
# - GOOGLE_CHAT_WEBHOOK_URL=
depends_on:
postgres:
condition: service_healthy
Expand Down
1 change: 1 addition & 0 deletions server/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ SECRET_KEY=notsecretkey

# SLACK_BOT_TOKEN=
# SLACK_CHANNEL_ID=
# GOOGLE_CHAT_WEBHOOK_URL=

## Do not edit this

Expand Down
20 changes: 14 additions & 6 deletions server/api/helpers/actions/create-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,7 +35,7 @@ const buildAndSendSlackMessage = async (card, action, actorUser) => {
return;
}

await sails.helpers.utils.sendSlackMessage(markdown);
await send(markdown);
};

module.exports = {
Expand Down Expand Up @@ -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,
Expand All @@ -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;
},
};
10 changes: 7 additions & 3 deletions server/api/helpers/cards/delete-one.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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);
}
}

Expand Down
41 changes: 41 additions & 0 deletions server/api/helpers/utils/send-google-chat-message.js
Original file line number Diff line number Diff line change
@@ -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}`);
}
},
};
1 change: 1 addition & 0 deletions server/config/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

0 comments on commit 8fd0f68

Please sign in to comment.