1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00

added new custom service with slack integration helper functions; added axios package; added notifications for card create, delete, and update (move); added notifications for comment create

This commit is contained in:
Brad Bahls 2023-12-23 08:52:07 -07:00
parent fbd1b28312
commit b8ce112dc3
6 changed files with 110 additions and 0 deletions

View file

@ -56,6 +56,7 @@
} }
}, },
"dependencies": { "dependencies": {
"axios": "^1.6.2",
"concurrently": "^8.2.2", "concurrently": "^8.2.2",
"husky": "^8.0.3", "husky": "^8.0.3",
"lint-staged": "^15.1.0" "lint-staged": "^15.1.0"

View file

@ -1,4 +1,5 @@
const moment = require('moment'); const moment = require('moment');
const services = require('../../services/custom');
const Errors = { const Errors = {
NOT_ENOUGH_RIGHTS: { NOT_ENOUGH_RIGHTS: {
@ -108,6 +109,12 @@ module.exports = {
}) })
.intercept('positionMustBeInValues', () => Errors.POSITION_MUST_BE_PRESENT); .intercept('positionMustBeInValues', () => Errors.POSITION_MUST_BE_PRESENT);
const cardUrl = services.buildCardUrl(card);
const messageText = cardUrl + ' was created by ' + currentUser.username + ' in *' + list.name + '*';
services.sendSlackMessage(messageText)
.then(() => { console.log('Slack message sent successfully.'); })
.catch((error) => { console.error('Failed to send Slack message:', error.message); });
return { return {
item: card, item: card,
}; };

View file

@ -1,3 +1,5 @@
const services = require('../../services/custom');
const Errors = { const Errors = {
NOT_ENOUGH_RIGHTS: { NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights', notEnoughRights: 'Not enough rights',
@ -54,6 +56,11 @@ module.exports = {
throw Errors.CARD_NOT_FOUND; throw Errors.CARD_NOT_FOUND;
} }
const messageText = '*' + card.name + '* was deleted by ' + currentUser.username;
services.sendSlackMessage(messageText)
.then(() => { console.log('Slack message sent successfully.'); })
.catch((error) => { console.error('Failed to send Slack message:', error.message); });
return { return {
item: card, item: card,
}; };

View file

@ -1,4 +1,5 @@
const moment = require('moment'); const moment = require('moment');
const services = require('../../services/custom');
const Errors = { const Errors = {
NOT_ENOUGH_RIGHTS: { NOT_ENOUGH_RIGHTS: {
@ -175,6 +176,8 @@ module.exports = {
'isSubscribed', 'isSubscribed',
]); ]);
const cardPositionBefore = card.position;
card = await sails.helpers.cards.updateOne card = await sails.helpers.cards.updateOne
.with({ .with({
board, board,
@ -195,6 +198,17 @@ module.exports = {
throw Errors.CARD_NOT_FOUND; throw Errors.CARD_NOT_FOUND;
} }
const cardPositionAfter = card.position;
const cardMoved = cardPositionBefore !== cardPositionAfter;
if (cardMoved) {
const cardUrl = services.buildCardUrl(card);
const messageText = cardUrl + ' was moved by ' + currentUser.username + ' to *' + nextList.name + '*';
services.sendSlackMessage(messageText)
.then(() => { console.log('Slack message sent successfully.'); })
.catch((error) => { console.error('Failed to send Slack message:', error.message); });
}
return { return {
item: card, item: card,
}; };

View file

@ -1,3 +1,5 @@
const services = require('../../services/custom');
const Errors = { const Errors = {
NOT_ENOUGH_RIGHTS: { NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights', notEnoughRights: 'Not enough rights',
@ -63,6 +65,12 @@ module.exports = {
request: this.req, request: this.req,
}); });
const cardUrl = services.buildCardUrl(card);
const messageText = '*' + currentUser.username + '* commented on ' + cardUrl + ':\n>' + inputs.text;
services.sendSlackMessage(messageText)
.then(() => { console.log('Slack message sent successfully.'); })
.catch((error) => { console.error('Failed to send Slack message:', error.message); });
return { return {
item: action, item: action,
}; };

View file

@ -0,0 +1,73 @@
const axios = require('axios');
const slackPostUrl = 'https://slack.com/api/chat.postMessage';
const channelId = 'C06B6F4R9RT';
const plankaProdUrl = 'https://kanban.glitchsecure.com';
const plankaTestUrl = 'http://localhost:3000';
const plankaTestWebhookUrl = 'https://hooks.slack.com/services/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';
const slackAPIToken = process.env.SLACK_BOT_TOKEN;
async function sendSlackMessage(messageText) {
if (!slackAPIToken) {
throw new Error('No Slack BOT token found');
}
console.log('Sending to Slack');
const postData = {
blocks: [ {
type: 'section',
text: {
type: 'mrkdwn',
text: messageText,
},
}]
};
try {
// Prod path
const config = {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${slackAPIToken}`,
},
};
axios.post(slackPostUrl, { ...postData, channel: channelId }, config)
.then(response => {
console.log('Slack response:', response.data);
})
.catch(error => {
console.error('Error sending to Slack:', error.message);
});
// Testing in dev environment (Brad)
/*
const response = await axios.post(plankaTestWebhookUrl, postData, {
headers: {
'Content-Type': 'application/json'
}
});
*/
console.log('Slack response:', response.data);
return response.data;
} catch (error) {
console.error('Error sending to Slack:', error.message);
throw error;
}
}
function buildCardUrl(card) {
const url = plankaProdUrl + '/cards/' + card.id;
const cardUrl = '<' + url + '|' + card.name + '>';
console.log(cardUrl);
return cardUrl;
}
module.exports = {
sendSlackMessage,
buildCardUrl
};