Skip to content

Commit

Permalink
🐛 Fix order of tokens, use new env
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Apr 20, 2021
1 parent 825fb19 commit d70f7a6
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 132 deletions.
10 changes: 0 additions & 10 deletions .github/dependabot.yml

This file was deleted.

15 changes: 0 additions & 15 deletions .github/workflows/dependabot.yml

This file was deleted.

3 changes: 2 additions & 1 deletion src/automations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { join } from "path";
import { Octokit } from "@octokit/rest";
import { config } from "dotenv";
import axios from "axios";
import { getSecret } from "./helpers/secrets";
config();

const createAutomatedIssue = async () => {
const octokit = new Octokit({
auth: process.env.AUTOMATION_TOKEN,
auth: getSecret("AUTOMATION_TOKEN"),
});
const searchResults = await octokit.search.repos({
q: "topic:upptime",
Expand Down
3 changes: 2 additions & 1 deletion src/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { join } from "path";
import { getConfig } from "./helpers/config";
import { commit, push } from "./helpers/git";
import { getOctokit } from "./helpers/github";
import { getOwnerRepo } from "./helpers/secrets";

export const updateDependencies = async () => {
let [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/");
const [owner, repo] = getOwnerRepo();
if (`${owner}/${repo}` !== "upptime/upptime") return;

const config = await getConfig();
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/calculate-response-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Downtimes } from "../interfaces";
import { getConfig } from "./config";
import { getOctokit } from "./github";
import { Octokit } from "@octokit/rest";
import { getOwnerRepo } from "./secrets";

/** Calculate the average of some numbers */
const avg = (array: number[]) => (array.length ? array.reduce((a, b) => a + b) / array.length : 0);
Expand Down Expand Up @@ -35,7 +36,7 @@ const getHistoryItems = async (
export const getResponseTimeForSite = async (
slug: string
): Promise<Omit<Downtimes & { currentStatus: "up" | "down" | "degraded" }, "dailyMinutesDown">> => {
let [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/");
const [owner, repo] = getOwnerRepo();
const octokit = await getOctokit();
const config = await getConfig();

Expand Down
3 changes: 2 additions & 1 deletion src/helpers/calculate-uptime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { join } from "path";
import { DownPecentages, Downtimes, SiteHistory } from "../interfaces";
import { getOctokit } from "./github";
import { checkOverlap } from "./overlap";
import { getOwnerRepo } from "./secrets";

/**
* Get the number of seconds a website has been down
* @param slug - Slug of the site
*/
const getDowntimeSecondsForSite = async (slug: string): Promise<Downtimes> => {
let [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/");
const [owner, repo] = getOwnerRepo();
const octokit = await getOctokit();
let day = 0;
let week = 0;
Expand Down
7 changes: 5 additions & 2 deletions src/helpers/environment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export const replaceEnvironmentVariables = (str: string) => {
Object.keys(process.env).forEach((key) => {
str = str.replace(`$${key}`, process.env[key] || `$${key}`);
const SECRETS_CONTEXT = process.env.SECRETS_CONTEXT || "{}";
const allSecrets: Record<string, string> = JSON.parse(SECRETS_CONTEXT);
const secrets: Record<string, any> = { ...JSON.parse(JSON.stringify(process.env)), allSecrets };
Object.keys(secrets).forEach((key) => {
str = str.replace(`$${key}`, secrets[key] || `$${key}`);
});
return str;
};
5 changes: 3 additions & 2 deletions src/helpers/github.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Octokit } from "@octokit/rest";
import { getConfig } from "./config";
import { getSecret } from "./secrets";

export const getOctokit = async (): Promise<Octokit> => {
const config = await getConfig();
return new Octokit({
auth: config.PAT || process.env.GH_PAT || process.env.GITHUB_TOKEN,
userAgent: config["user-agent"] || process.env.USER_AGENT || "KojBot",
auth: config.PAT || getSecret("GH_PAT") || getSecret("GITHUB_TOKEN"),
userAgent: config["user-agent"] || getSecret("USER_AGENT") || "KojBot",
});
};
3 changes: 2 additions & 1 deletion src/helpers/init-check.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import axios from "axios";
import { readFile } from "fs-extra";
import { join } from "path";
import { getOwnerRepo } from "./secrets";

export const shouldContinue = async (): Promise<boolean> => {
let [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/");
const [owner, repo] = getOwnerRepo();
if (`${owner}/${repo}` === "upptime/upptime") return true;
try {
const upptimeDefaultConfig = await axios.get(
Expand Down
23 changes: 12 additions & 11 deletions src/helpers/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import { UpptimeConfig } from "../interfaces";
import axios from "axios";
import nodemailer from "nodemailer";
import SMTPTransport from "nodemailer/lib/smtp-transport";
import { getSecret } from "./secrets";

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;
const token = getSecret("SLACK_APP_ACCESS_TOKEN");
if (token) {
const { data } = await axios.post(
"https://slack.com/api/chat.postMessage",
{ channel: notification.channel, text },
{ headers: { Authorization: `Bearer ${process.env.SLACK_BOT_ACCESS_TOKEN}` } }
{ headers: { Authorization: `Bearer ${getSecret("SLACK_BOT_ACCESS_TOKEN")}` } }
);
console.log("[debug] Slack response", data);
}
Expand All @@ -24,26 +25,26 @@ export const sendNotification = async (config: UpptimeConfig, text: string) => {
"[debug] Slack token",
(token || "").split("").join(" "),
{ channel: notification.channel, text },
{ headers: { Authorization: `Bearer ${process.env.SLACK_BOT_ACCESS_TOKEN}` } }
{ headers: { Authorization: `Bearer ${getSecret("SLACK_BOT_ACCESS_TOKEN")}` } }
);
} else if (notification.type === "discord") {
console.log("[debug] Sending Discord notification");
const webhookUrl = process.env.DISCORD_WEBHOOK_URL;
const webhookUrl = getSecret("DISCORD_WEBHOOK_URL");
if (webhookUrl) await axios.post(webhookUrl, { content: text });
} else if (notification.type === "email") {
console.log("[debug] Sending email notification");
const transporter = nodemailer.createTransport({
host: process.env.NOTIFICATION_SMTP_HOST,
port: process.env.NOTIFICATION_SMTP_PORT || 587,
secure: !!process.env.NOTIFICATION_SMTP_SECURE,
host: getSecret("NOTIFICATION_SMTP_HOST"),
port: getSecret("NOTIFICATION_SMTP_PORT") || 587,
secure: !!getSecret("NOTIFICATION_SMTP_SECURE"),
auth: {
user: process.env.NOTIFICATION_SMTP_USER,
pass: process.env.NOTIFICATION_SMTP_PASSWORD,
user: getSecret("NOTIFICATION_SMTP_USER"),
pass: getSecret("NOTIFICATION_SMTP_PASSWORD"),
},
} as SMTPTransport.Options);
await transporter.sendMail({
from: process.env.NOTIFICATION_SMTP_USER,
to: process.env.NOTIFICATION_EMAIL || process.env.NOTIFICATION_SMTP_USER,
from: getSecret("NOTIFICATION_SMTP_USER"),
to: getSecret("NOTIFICATION_EMAIL") || getSecret("NOTIFICATION_SMTP_USER"),
subject: text,
text: text,
html: `<p>${text}</p>`,
Expand Down
Loading

0 comments on commit d70f7a6

Please sign in to comment.