refactor: ♻️ move env variables to init

pull/691/head
HannesOberreiter 2 years ago
parent a0c7c73372
commit 9ad2a340bf

@ -38,20 +38,11 @@ function buildMessage(user, card, action) {
*/ */
const handleSlack = () => { const handleSlack = () => {
const POST_MESSAGE_API_URL = 'https://slack.com/api/chat.postMessage'; const POST_MESSAGE_API_URL = 'https://slack.com/api/chat.postMessage';
const TOKEN = sails.config.custom.slackBotToken;
function getTokens() { const CHANNEL = sails.config.custom.slackChannelId;
if (!sails.config.custom.slackBotToken || !sails.config.custom.slackChannelId) {
return false;
}
return {
token: sails.config.custom.slackBotToken,
channel: sails.config.custom.slackChannelId,
};
}
const send = async ({ action, user, card }) => { const send = async ({ action, user, card }) => {
const tokens = getTokens(); if (!TOKEN || !CHANNEL) {
if (!tokens) {
return; return;
} }
@ -63,7 +54,7 @@ const handleSlack = () => {
} }
const data = { const data = {
channel: tokens.channel, channel: CHANNEL,
text: markdown, text: markdown,
as_user: false, as_user: false,
username: user.name, username: user.name,
@ -76,7 +67,7 @@ const handleSlack = () => {
body: JSON.stringify(data), body: JSON.stringify(data),
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Bearer ${tokens.token}`, Authorization: `Bearer ${TOKEN}`,
}, },
}); });
@ -102,27 +93,23 @@ const handleSlack = () => {
* @returns Object with send method * @returns Object with send method
*/ */
const handleWebhook = () => { const handleWebhook = () => {
function getWebhookUrl() { const URL = sails.config.custom.webhookUrl;
return sails.config.custom.webhookUrl || false; const TOKEN = sails.config.custom.webhookBearerToken;
}
function buildHeaders() { function buildHeaders() {
const bearer = sails.config.custom.webhookBearerToken || false;
const headers = { const headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}; };
if (bearer) { if (TOKEN) {
headers.Authorization = `Bearer ${bearer}`; headers.Authorization = `Bearer ${TOKEN}`;
} }
return headers; return headers;
} }
const send = async ({ action, user, card, board }) => { const send = async ({ action, user, card, board }) => {
const url = getWebhookUrl(); if (!URL) {
if (!url) {
return; return;
} }
@ -133,10 +120,11 @@ const handleWebhook = () => {
action, action,
board, board,
card, card,
user: _.pick(user, ['id', 'name', 'avatarUrl', 'email']),
}; };
try { try {
const response = await fetch(url, { const response = await fetch(URL, {
method: 'POST', method: 'POST',
headers: buildHeaders(), headers: buildHeaders(),
body: JSON.stringify(data), body: JSON.stringify(data),

Loading…
Cancel
Save