Skip to content

Commit

Permalink
♻️ Move notification to helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 30, 2020
1 parent 38380a0 commit 3bb60ab
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 50 deletions.
25 changes: 25 additions & 0 deletions src/notifications.ts
Original file line number Diff line number Diff line change
@@ -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");
};
56 changes: 6 additions & 50 deletions src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 3bb60ab

Please sign in to comment.