From 3bb60ab2ba4322a69a11baac92b2cb2e0be181ac Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Fri, 30 Oct 2020 18:12:06 +0530 Subject: [PATCH] :recycle: Move notification to helper function --- src/notifications.ts | 25 ++++++++++++++++++++ src/update.ts | 56 +++++--------------------------------------- 2 files changed, 31 insertions(+), 50 deletions(-) create mode 100644 src/notifications.ts diff --git a/src/notifications.ts b/src/notifications.ts new file mode 100644 index 00000000..480d0f72 --- /dev/null +++ b/src/notifications.ts @@ -0,0 +1,25 @@ +import { UpptimeConfig } from "./interfaces"; +import axios from "axios"; + +export const sendNotification = async (config: UpptimeConfig, text: string) => { + console.log("[debug] Sending notification", text); + console.log(`[debug] Notification config has ${(config.notifications || []).length} keys`); + for await (const notification of config.notifications || []) { + if (notification.type === "slack") { + console.log("[debug] Sending Slack notification to channel", notification.channel); + const token = process.env.SLACK_APP_ACCESS_TOKEN; + if (token) + await axios.post( + "https://slack.com/api/chat.postMessage", + { channel: notification.channel, text }, + { headers: { Authorization: `Bearer ${process.env.SLACK_BOT_ACCESS_TOKEN}` } } + ); + console.log("[debug] Slack token found?", !!token); + } else if (notification.type === "discord") { + console.log("[debug] Sendind Discord notification"); + const webhookUrl = process.env.DISCORD_WEBHOOK_URL; + if (webhookUrl) await axios.post(webhookUrl, { content: text }); + } + } + console.log("[debug] Notifications are sent"); +}; diff --git a/src/update.ts b/src/update.ts index 2f05bca6..8c980612 100644 --- a/src/update.ts +++ b/src/update.ts @@ -7,6 +7,7 @@ import { Curl, CurlFeature } from "node-libcurl"; import { join } from "path"; import { generateSummary } from "./summary"; import { UpptimeConfig } from "./interfaces"; +import { sendNotification } from "./notifications"; export const update = async (shouldCommit = false) => { const config = safeLoad(await readFile(join(".", ".upptimerc.yml"), "utf8")) as UpptimeConfig; @@ -153,31 +154,10 @@ export const update = async (shouldCommit = false) => { issue_number: newIssue.data.number, }); console.log("Opened and locked a new issue"); - for await (const notification of config.notifications || []) { - if (notification.type === "slack") { - const token = process.env.SLACK_APP_ACCESS_TOKEN; - if (token) - await axios.post( - "https://slack.com/api/chat.postMessage", - { - channel: notification.channel, - text: `🟥 ${site.name} (${site.url}) is **down**: ${newIssue.data.html_url}`, - }, - { - headers: { - Authorization: `Bearer ${process.env.SLACK_BOT_ACCESS_TOKEN}`, - }, - } - ); - } else if (notification.type === "discord") { - const webhookUrl = process.env.DISCORD_WEBHOOK_URL; - if (webhookUrl) - await axios.post(webhookUrl, { - content: `🟥 ${site.name} (${site.url}) is **down**: ${newIssue.data.html_url}`, - }); - } - } - console.log("Sent notifications"); + await sendNotification( + config, + `🟥 ${site.name} (${site.url}) is **down**: ${newIssue.data.html_url}` + ); } else { console.log("An issue is already open for this"); } @@ -204,31 +184,7 @@ export const update = async (shouldCommit = false) => { state: "closed", }); console.log("Closed issue"); - for await (const notification of config.notifications || []) { - if (notification.type === "slack") { - const token = process.env.SLACK_APP_ACCESS_TOKEN; - if (token) - await axios.post( - "https://slack.com/api/chat.postMessage", - { - channel: notification.channel, - text: `🟩 ${site.name} (${site.url}) is back up.`, - }, - { - headers: { - Authorization: `Bearer ${process.env.SLACK_BOT_ACCESS_TOKEN}`, - }, - } - ); - } else if (notification.type === "discord") { - const webhookUrl = process.env.DISCORD_WEBHOOK_URL; - if (webhookUrl) - await axios.post(webhookUrl, { - content: `🟩 ${site.name} (${site.url}) is back up.`, - }); - } - } - console.log("Sent notifications"); + await sendNotification(config, `🟩 ${site.name} (${site.url}) is back up.`); } else { console.log("Could not find a relevant issue", issues.data); }