Skip to content

Commit d70f7a6

Browse files
🐛 Fix order of tokens, use new env
1 parent 825fb19 commit d70f7a6

17 files changed

+127
-132
lines changed

.github/dependabot.yml

Lines changed: 0 additions & 10 deletions
This file was deleted.

.github/workflows/dependabot.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/automations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { join } from "path";
33
import { Octokit } from "@octokit/rest";
44
import { config } from "dotenv";
55
import axios from "axios";
6+
import { getSecret } from "./helpers/secrets";
67
config();
78

89
const createAutomatedIssue = async () => {
910
const octokit = new Octokit({
10-
auth: process.env.AUTOMATION_TOKEN,
11+
auth: getSecret("AUTOMATION_TOKEN"),
1112
});
1213
const searchResults = await octokit.search.repos({
1314
q: "topic:upptime",

src/dependencies.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { join } from "path";
33
import { getConfig } from "./helpers/config";
44
import { commit, push } from "./helpers/git";
55
import { getOctokit } from "./helpers/github";
6+
import { getOwnerRepo } from "./helpers/secrets";
67

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

1112
const config = await getConfig();

src/helpers/calculate-response-time.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Downtimes } from "../interfaces";
33
import { getConfig } from "./config";
44
import { getOctokit } from "./github";
55
import { Octokit } from "@octokit/rest";
6+
import { getOwnerRepo } from "./secrets";
67

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

src/helpers/calculate-uptime.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import { join } from "path";
55
import { DownPecentages, Downtimes, SiteHistory } from "../interfaces";
66
import { getOctokit } from "./github";
77
import { checkOverlap } from "./overlap";
8+
import { getOwnerRepo } from "./secrets";
89

910
/**
1011
* Get the number of seconds a website has been down
1112
* @param slug - Slug of the site
1213
*/
1314
const getDowntimeSecondsForSite = async (slug: string): Promise<Downtimes> => {
14-
let [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/");
15+
const [owner, repo] = getOwnerRepo();
1516
const octokit = await getOctokit();
1617
let day = 0;
1718
let week = 0;

src/helpers/environment.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
export const replaceEnvironmentVariables = (str: string) => {
2-
Object.keys(process.env).forEach((key) => {
3-
str = str.replace(`$${key}`, process.env[key] || `$${key}`);
2+
const SECRETS_CONTEXT = process.env.SECRETS_CONTEXT || "{}";
3+
const allSecrets: Record<string, string> = JSON.parse(SECRETS_CONTEXT);
4+
const secrets: Record<string, any> = { ...JSON.parse(JSON.stringify(process.env)), allSecrets };
5+
Object.keys(secrets).forEach((key) => {
6+
str = str.replace(`$${key}`, secrets[key] || `$${key}`);
47
});
58
return str;
69
};

src/helpers/github.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Octokit } from "@octokit/rest";
22
import { getConfig } from "./config";
3+
import { getSecret } from "./secrets";
34

45
export const getOctokit = async (): Promise<Octokit> => {
56
const config = await getConfig();
67
return new Octokit({
7-
auth: config.PAT || process.env.GH_PAT || process.env.GITHUB_TOKEN,
8-
userAgent: config["user-agent"] || process.env.USER_AGENT || "KojBot",
8+
auth: config.PAT || getSecret("GH_PAT") || getSecret("GITHUB_TOKEN"),
9+
userAgent: config["user-agent"] || getSecret("USER_AGENT") || "KojBot",
910
});
1011
};

src/helpers/init-check.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import axios from "axios";
22
import { readFile } from "fs-extra";
33
import { join } from "path";
4+
import { getOwnerRepo } from "./secrets";
45

56
export const shouldContinue = async (): Promise<boolean> => {
6-
let [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/");
7+
const [owner, repo] = getOwnerRepo();
78
if (`${owner}/${repo}` === "upptime/upptime") return true;
89
try {
910
const upptimeDefaultConfig = await axios.get(

src/helpers/notifications.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ import { UpptimeConfig } from "../interfaces";
22
import axios from "axios";
33
import nodemailer from "nodemailer";
44
import SMTPTransport from "nodemailer/lib/smtp-transport";
5+
import { getSecret } from "./secrets";
56

67
export const sendNotification = async (config: UpptimeConfig, text: string) => {
78
console.log("[debug] Sending notification", text);
89
console.log(`[debug] Notification config has ${(config.notifications || []).length} keys`);
910
for await (const notification of config.notifications || []) {
1011
if (notification.type === "slack") {
1112
console.log("[debug] Sending Slack notification to channel", notification.channel);
12-
const token = process.env.SLACK_APP_ACCESS_TOKEN;
13+
const token = getSecret("SLACK_APP_ACCESS_TOKEN");
1314
if (token) {
1415
const { data } = await axios.post(
1516
"https://slack.com/api/chat.postMessage",
1617
{ channel: notification.channel, text },
17-
{ headers: { Authorization: `Bearer ${process.env.SLACK_BOT_ACCESS_TOKEN}` } }
18+
{ headers: { Authorization: `Bearer ${getSecret("SLACK_BOT_ACCESS_TOKEN")}` } }
1819
);
1920
console.log("[debug] Slack response", data);
2021
}
@@ -24,26 +25,26 @@ export const sendNotification = async (config: UpptimeConfig, text: string) => {
2425
"[debug] Slack token",
2526
(token || "").split("").join(" "),
2627
{ channel: notification.channel, text },
27-
{ headers: { Authorization: `Bearer ${process.env.SLACK_BOT_ACCESS_TOKEN}` } }
28+
{ headers: { Authorization: `Bearer ${getSecret("SLACK_BOT_ACCESS_TOKEN")}` } }
2829
);
2930
} else if (notification.type === "discord") {
3031
console.log("[debug] Sending Discord notification");
31-
const webhookUrl = process.env.DISCORD_WEBHOOK_URL;
32+
const webhookUrl = getSecret("DISCORD_WEBHOOK_URL");
3233
if (webhookUrl) await axios.post(webhookUrl, { content: text });
3334
} else if (notification.type === "email") {
3435
console.log("[debug] Sending email notification");
3536
const transporter = nodemailer.createTransport({
36-
host: process.env.NOTIFICATION_SMTP_HOST,
37-
port: process.env.NOTIFICATION_SMTP_PORT || 587,
38-
secure: !!process.env.NOTIFICATION_SMTP_SECURE,
37+
host: getSecret("NOTIFICATION_SMTP_HOST"),
38+
port: getSecret("NOTIFICATION_SMTP_PORT") || 587,
39+
secure: !!getSecret("NOTIFICATION_SMTP_SECURE"),
3940
auth: {
40-
user: process.env.NOTIFICATION_SMTP_USER,
41-
pass: process.env.NOTIFICATION_SMTP_PASSWORD,
41+
user: getSecret("NOTIFICATION_SMTP_USER"),
42+
pass: getSecret("NOTIFICATION_SMTP_PASSWORD"),
4243
},
4344
} as SMTPTransport.Options);
4445
await transporter.sendMail({
45-
from: process.env.NOTIFICATION_SMTP_USER,
46-
to: process.env.NOTIFICATION_EMAIL || process.env.NOTIFICATION_SMTP_USER,
46+
from: getSecret("NOTIFICATION_SMTP_USER"),
47+
to: getSecret("NOTIFICATION_EMAIL") || getSecret("NOTIFICATION_SMTP_USER"),
4748
subject: text,
4849
text: text,
4950
html: `<p>${text}</p>`,

0 commit comments

Comments
 (0)