fix: Refactoring
parent
b1ad354815
commit
cee4129c16
@ -0,0 +1,53 @@
|
||||
const POST_MESSAGE_API_URL = 'https://slack.com/api/chat.postMessage';
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
markdown: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
const headers = {
|
||||
Authorization: `Bearer ${sails.config.custom.slackBotToken}`,
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
};
|
||||
|
||||
const body = {
|
||||
blocks: [
|
||||
{
|
||||
type: 'section',
|
||||
text: {
|
||||
type: 'mrkdwn',
|
||||
text: inputs.markdown,
|
||||
},
|
||||
},
|
||||
],
|
||||
channel: sails.config.custom.slackChannelId,
|
||||
};
|
||||
|
||||
let response;
|
||||
try {
|
||||
response = await fetch(POST_MESSAGE_API_URL, {
|
||||
headers,
|
||||
method: 'POST',
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
} catch (error) {
|
||||
sails.log.error(error); // TODO: provide description text?
|
||||
return;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
sails.log.error('Error sending to Slack: %s', response.error);
|
||||
return;
|
||||
}
|
||||
|
||||
const responseJson = await response.json();
|
||||
|
||||
if (!responseJson.ok) {
|
||||
sails.log.error('Error sending to Slack: %s', responseJson.error);
|
||||
}
|
||||
},
|
||||
};
|
||||
@ -1,61 +0,0 @@
|
||||
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');
|
||||
}
|
||||
|
||||
const postData = {
|
||||
blocks: [
|
||||
{
|
||||
type: 'section',
|
||||
text: {
|
||||
type: 'mrkdwn',
|
||||
text: messageText,
|
||||
},
|
||||
},
|
||||
],
|
||||
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…
Reference in New Issue