mirror of
https://github.com/plankanban/planka.git
synced 2025-08-10 07:55:27 +02:00
chore: replace axios by fetch
Fix lint errors too
This commit is contained in:
parent
9b6766cff6
commit
4e02efa0ee
6 changed files with 68 additions and 66 deletions
|
@ -56,7 +56,6 @@
|
|||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^1.6.2",
|
||||
"concurrently": "^8.2.2",
|
||||
"husky": "^8.0.3",
|
||||
"lint-staged": "^15.1.0"
|
||||
|
|
|
@ -111,10 +111,10 @@ module.exports = {
|
|||
.intercept('positionMustBeInValues', () => Errors.POSITION_MUST_BE_PRESENT);
|
||||
|
||||
const cardUrl = services.buildCardUrl(card);
|
||||
const messageText = cardUrl + ' was created by ' + currentUser.name + ' in *' + list.name + '*';
|
||||
services.sendSlackMessage(messageText)
|
||||
.then(() => { console.log('Slack message sent successfully.'); })
|
||||
.catch((error) => { console.error('Failed to send Slack message:', error.message); });
|
||||
const messageText = `${cardUrl} was created by ${currentUser.name} in *${list.name}*`;
|
||||
services.sendSlackMessage(messageText).catch((error) => {
|
||||
throw new Error('Failed to send Slack message:', error.message);
|
||||
});
|
||||
|
||||
return {
|
||||
item: card,
|
||||
|
|
|
@ -56,10 +56,10 @@ module.exports = {
|
|||
throw Errors.CARD_NOT_FOUND;
|
||||
}
|
||||
|
||||
const messageText = '*' + card.name + '* was deleted by ' + currentUser.name;
|
||||
services.sendSlackMessage(messageText)
|
||||
.then(() => { console.log('Slack message sent successfully.'); })
|
||||
.catch((error) => { console.error('Failed to send Slack message:', error.message); });
|
||||
const messageText = `*${card.name}* was deleted by ${currentUser.name}`;
|
||||
services.sendSlackMessage(messageText).catch((error) => {
|
||||
throw new Error('Failed to send Slack message:', error.message);
|
||||
});
|
||||
|
||||
return {
|
||||
item: card,
|
||||
|
|
|
@ -203,10 +203,10 @@ module.exports = {
|
|||
|
||||
if (cardMoved) {
|
||||
const cardUrl = services.buildCardUrl(card);
|
||||
const messageText = cardUrl + ' was moved by ' + currentUser.name + ' to *' + nextList.name + '*';
|
||||
services.sendSlackMessage(messageText)
|
||||
.then(() => { console.log('Slack message sent successfully.'); })
|
||||
.catch((error) => { console.error('Failed to send Slack message:', error.message); });
|
||||
const messageText = `${cardUrl} was moved by ${currentUser.name} to *${nextList.name}*`;
|
||||
services.sendSlackMessage(messageText).catch((error) => {
|
||||
throw new Error('Failed to send Slack message:', error.message);
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
|
@ -67,10 +67,10 @@ module.exports = {
|
|||
});
|
||||
|
||||
const cardUrl = services.buildCardUrl(card);
|
||||
const messageText = '*' + currentUser.name + '* 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); });
|
||||
const messageText = `*${currentUser.name}* commented on ${cardUrl}:\n>${inputs.text}`;
|
||||
services.sendSlackMessage(messageText).catch((error) => {
|
||||
throw new Error('Failed to send Slack message:', error.message);
|
||||
});
|
||||
|
||||
return {
|
||||
item: action,
|
||||
|
|
|
@ -1,58 +1,61 @@
|
|||
const axios = require('axios');
|
||||
const slackPostUrl = 'https://slack.com/api/chat.postMessage';
|
||||
const channelId = process.env.SLACK_CHANNEL_ID;
|
||||
const slackAPIToken = process.env.SLACK_BOT_TOKEN;
|
||||
const plankaProdUrl = process.env.BASE_URL;
|
||||
|
||||
async function sendSlackMessage(messageText) {
|
||||
if (!slackAPIToken) {
|
||||
throw new Error('No Slack BOT token found');
|
||||
}
|
||||
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 {
|
||||
const config = {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${slackAPIToken}`,
|
||||
const postData = {
|
||||
blocks: [
|
||||
{
|
||||
type: 'section',
|
||||
text: {
|
||||
type: 'mrkdwn',
|
||||
text: messageText,
|
||||
},
|
||||
};
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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
|
||||
},
|
||||
],
|
||||
channel: channelId,
|
||||
};
|
||||
|
||||
const config = {
|
||||
headers: {
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
Authorization: `Bearer ${slackAPIToken}`,
|
||||
},
|
||||
};
|
||||
|
||||
const response = await fetch(slackPostUrl, {
|
||||
method: 'POST',
|
||||
headers: config.headers,
|
||||
body: JSON.stringify(postData),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
sails.log.Error('Error sending to Slack :', response.error);
|
||||
return Promise.reject(response);
|
||||
}
|
||||
|
||||
const responseText = await new Response(response.body).text();
|
||||
const jsonBody = JSON.parse(responseText);
|
||||
if (!jsonBody.ok) {
|
||||
sails.log.Error('Error sending to Slack :', jsonBody.error);
|
||||
return Promise.reject(response);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
function buildCardUrl(card) {
|
||||
const url = `${plankaProdUrl}/cards/${card.id}`;
|
||||
const cardUrl = `<${url}|${card.name}>`;
|
||||
return cardUrl;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
sendSlackMessage,
|
||||
buildCardUrl,
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue