mirror of
https://github.com/wahyd4/upptime.git
synced 2026-08-09 05:06:31 +10:00
🔥 Remove JS files
This commit is contained in:
-107
@@ -1,107 +0,0 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
*.lcov
|
||||
|
||||
# nyc test coverage
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# TypeScript v1 declaration files
|
||||
typings/
|
||||
|
||||
# TypeScript cache
|
||||
*.tsbuildinfo
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
.eslintcache
|
||||
|
||||
# Microbundle cache
|
||||
.rpt2_cache/
|
||||
.rts2_cache_cjs/
|
||||
.rts2_cache_es/
|
||||
.rts2_cache_umd/
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variables file
|
||||
.env
|
||||
.env.test
|
||||
|
||||
# parcel-bundler cache (https://parceljs.org/)
|
||||
.cache
|
||||
|
||||
# Next.js build output
|
||||
.next
|
||||
|
||||
# Nuxt.js build / generate output
|
||||
.nuxt
|
||||
dist
|
||||
|
||||
# Gatsby files
|
||||
.cache/
|
||||
# Comment in the public line in if your project uses Gatsby and *not* Next.js
|
||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||
# public
|
||||
|
||||
# vuepress build output
|
||||
.vuepress/dist
|
||||
|
||||
# Serverless directories
|
||||
.serverless/
|
||||
|
||||
# FuseBox cache
|
||||
.fusebox/
|
||||
|
||||
# DynamoDB Local files
|
||||
.dynamodb/
|
||||
|
||||
# TernJS port file
|
||||
.tern-port
|
||||
|
||||
# Built files
|
||||
dist/
|
||||
@@ -1 +0,0 @@
|
||||
history/
|
||||
@@ -1,22 +0,0 @@
|
||||
{
|
||||
"plugins": [
|
||||
[
|
||||
"semantic-release-gitmoji",
|
||||
{
|
||||
"releaseRules": {
|
||||
"patch": {
|
||||
"include": [":bento:", ":recycle:"]
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"@semantic-release/github",
|
||||
"@semantic-release/npm",
|
||||
[
|
||||
"@semantic-release/git",
|
||||
{
|
||||
"message": ":bookmark: v${nextRelease.version} [skip ci]\n\nhttps://github.com/koj-co/status/releases/tag/${nextRelease.gitTag}"
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
import { Octokit } from "@octokit/rest";
|
||||
import slugify from "@sindresorhus/slugify";
|
||||
import { readFile, writeFile, ensureDir, writeJson, readJson } from "fs-extra";
|
||||
import { safeLoad } from "js-yaml";
|
||||
import { join } from "path";
|
||||
import { CanvasRenderService } from "chartjs-node-canvas";
|
||||
|
||||
const canvasRenderService = new CanvasRenderService(600, 400);
|
||||
|
||||
export const generateGraphs = async () => {
|
||||
const config = safeLoad(
|
||||
await readFile(join(".", ".upptimerc.yml"), "utf8")
|
||||
) as {
|
||||
sites: { name: string; url: string }[];
|
||||
owner: string;
|
||||
repo: string;
|
||||
userAgent?: string;
|
||||
PAT?: string;
|
||||
assignees?: string[];
|
||||
};
|
||||
const owner = config.owner;
|
||||
const repo = config.repo;
|
||||
|
||||
const octokit = new Octokit({
|
||||
auth: config.PAT || process.env.GH_PAT || process.env.GITHUB_TOKEN,
|
||||
userAgent: config.userAgent || process.env.USER_AGENT || "KojBot",
|
||||
});
|
||||
|
||||
await ensureDir(join(".", "graphs"));
|
||||
|
||||
for await (const site of config.sites) {
|
||||
const slug = slugify(site.name);
|
||||
|
||||
let uptime = 0;
|
||||
let responseTime = 0;
|
||||
try {
|
||||
const api: {
|
||||
slug: string;
|
||||
uptime: string;
|
||||
time: number;
|
||||
}[] = await readJson(join(".", "history", "summary.json"));
|
||||
const item = api.find((site) => site.slug === slug);
|
||||
if (item) {
|
||||
uptime = parseFloat(item.uptime);
|
||||
responseTime = item.time;
|
||||
}
|
||||
} catch (error) {}
|
||||
await ensureDir(join(".", "api", slug));
|
||||
await writeJson(join(".", "api", slug, "uptime.json"), {
|
||||
schemaVersion: 1,
|
||||
label: "uptime",
|
||||
message: `${uptime}%`,
|
||||
color:
|
||||
uptime > 95
|
||||
? "brightgreen"
|
||||
: uptime > 90
|
||||
? "green"
|
||||
: uptime > 85
|
||||
? "yellowgreen"
|
||||
: uptime > 80
|
||||
? "yellow"
|
||||
: uptime > 75
|
||||
? "orange"
|
||||
: "red",
|
||||
});
|
||||
await writeJson(join(".", "api", slug, "response-time.json"), {
|
||||
schemaVersion: 1,
|
||||
label: "response time",
|
||||
message: `${responseTime} ms`,
|
||||
color:
|
||||
responseTime < 200
|
||||
? "brightgreen"
|
||||
: responseTime < 400
|
||||
? "green"
|
||||
: responseTime < 600
|
||||
? "yellowgreen"
|
||||
: responseTime < 800
|
||||
? "yellow"
|
||||
: responseTime < 1000
|
||||
? "orange"
|
||||
: "red",
|
||||
});
|
||||
|
||||
const history = await octokit.repos.listCommits({
|
||||
owner,
|
||||
repo,
|
||||
path: `history/${slug}.yml`,
|
||||
per_page: 10,
|
||||
});
|
||||
if (!history.data.length) continue;
|
||||
const data: [number, string][] = history.data
|
||||
.filter(
|
||||
(item) =>
|
||||
item.commit.message.includes(" in ") &&
|
||||
Number(item.commit.message.split(" in ")[1].split("ms")[0]) !== 0
|
||||
)
|
||||
.map((item) => [
|
||||
Number(item.commit.message.split(" in ")[1].split("ms")[0]),
|
||||
String(item.commit.author.date),
|
||||
]);
|
||||
const image = await canvasRenderService.renderToBuffer({
|
||||
type: "line",
|
||||
data: {
|
||||
labels: [1, ...data.map((item) => item[1])],
|
||||
datasets: [
|
||||
{
|
||||
backgroundColor: "#89e0cf",
|
||||
borderColor: "#1abc9c",
|
||||
data: [1, ...data.map((item) => item[0])],
|
||||
},
|
||||
],
|
||||
},
|
||||
options: {
|
||||
legend: { display: false },
|
||||
scales: {
|
||||
xAxes: [
|
||||
{
|
||||
display: false,
|
||||
gridLines: {
|
||||
display: false,
|
||||
},
|
||||
},
|
||||
],
|
||||
yAxes: [
|
||||
{
|
||||
display: false,
|
||||
gridLines: {
|
||||
display: false,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
await writeFile(join(".", "graphs", `${slug}.png`), image);
|
||||
}
|
||||
};
|
||||
|
||||
generateGraphs();
|
||||
Generated
-7503
File diff suppressed because it is too large
Load Diff
@@ -1,54 +0,0 @@
|
||||
{
|
||||
"name": "upptime-example",
|
||||
"version": "1.13.3",
|
||||
"description": "This repository uses GitHub Actions as an uptime monitor to track the status of Koj websites.",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"readme": "ts-node readme.ts",
|
||||
"update": "ts-node update.ts",
|
||||
"update-with-commit": "ts-node update.ts commit",
|
||||
"graphs": "ts-node graph.ts",
|
||||
"update-template": "update-template https://github.com/koj-co/upptime",
|
||||
"build": "tsc",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/koj-co/status.git"
|
||||
},
|
||||
"keywords": [
|
||||
"status",
|
||||
"statuskit",
|
||||
"uptime",
|
||||
"uptime-monitor"
|
||||
],
|
||||
"author": "Anand Chowdhary <mail@anandchowdhary.com>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/koj-co/status/issues"
|
||||
},
|
||||
"homepage": "https://github.com/koj-co/status#readme",
|
||||
"devDependencies": {
|
||||
"@semantic-release/git": "^9.0.0",
|
||||
"@types/chart.js": "^2.9.23",
|
||||
"@types/fs-extra": "^9.0.1",
|
||||
"@types/js-yaml": "^3.12.5",
|
||||
"canvas": "^2.6.1",
|
||||
"semantic-release": "^17.1.1",
|
||||
"semantic-release-gitmoji": "^1.3.4",
|
||||
"ts-node": "^8.10.2",
|
||||
"typescript": "^3.9.7"
|
||||
},
|
||||
"dependencies": {
|
||||
"@octokit/rest": "^18.0.3",
|
||||
"@sindresorhus/slugify": "^1.1.0",
|
||||
"axios": "^0.20.0",
|
||||
"chart.js": "^2.9.3",
|
||||
"chartjs-node-canvas": "^3.0.6",
|
||||
"fs-extra": "^9.0.1",
|
||||
"js-yaml": "^3.14.0",
|
||||
"node-libcurl": "^2.2.0",
|
||||
"update-template": "^1.0.1",
|
||||
"upptime": "0.0.0"
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
import { generateSummary } from "./summary";
|
||||
|
||||
generateSummary();
|
||||
-192
@@ -1,192 +0,0 @@
|
||||
import { Octokit } from "@octokit/rest";
|
||||
import slugify from "@sindresorhus/slugify";
|
||||
import { readFile } from "fs-extra";
|
||||
import { safeLoad } from "js-yaml";
|
||||
import { join } from "path";
|
||||
|
||||
export const generateSummary = async () => {
|
||||
const config = safeLoad(
|
||||
await readFile(join(".", ".upptimerc.yml"), "utf8")
|
||||
) as {
|
||||
sites: { name: string; url: string }[];
|
||||
owner: string;
|
||||
repo: string;
|
||||
userAgent?: string;
|
||||
PAT?: string;
|
||||
assignees?: string[];
|
||||
};
|
||||
const owner = config.owner;
|
||||
const repo = config.repo;
|
||||
|
||||
const octokit = new Octokit({
|
||||
auth: config.PAT || process.env.GH_PAT || process.env.GITHUB_TOKEN,
|
||||
userAgent: config.userAgent || process.env.USER_AGENT || "KojBot",
|
||||
});
|
||||
|
||||
let readmeContent = await readFile(join(".", "README.md"), "utf8");
|
||||
|
||||
const startText = readmeContent.split("<!--start: status pages-->")[0];
|
||||
const endText = readmeContent.split("<!--end: status pages-->")[1];
|
||||
|
||||
const pageStatuses: Array<{
|
||||
url: string;
|
||||
status: string;
|
||||
slug: string;
|
||||
time: number;
|
||||
uptime: string;
|
||||
name: string;
|
||||
}> = [];
|
||||
|
||||
let numberOfDown = 0;
|
||||
for await (const site of config.sites) {
|
||||
const slug = slugify(site.name);
|
||||
let startTime = new Date().toISOString();
|
||||
try {
|
||||
startTime =
|
||||
(await readFile(join(".", "history", `${slug}.yml`), "utf8"))
|
||||
.split("\n")
|
||||
.find((line) => line.toLocaleLowerCase().includes("- starttime"))
|
||||
?.split("startTime:")[1]
|
||||
.trim() || new Date().toISOString();
|
||||
} catch (error) {}
|
||||
let secondsDown = 0;
|
||||
const history = await octokit.repos.listCommits({
|
||||
owner,
|
||||
repo,
|
||||
path: `history/${slug}.yml`,
|
||||
per_page: 100,
|
||||
});
|
||||
const issues = await octokit.issues.listForRepo({
|
||||
owner,
|
||||
repo,
|
||||
labels: slug,
|
||||
filter: "all",
|
||||
per_page: 100,
|
||||
});
|
||||
issues.data.forEach((issue) => {
|
||||
if (issue.closed_at)
|
||||
secondsDown += Math.floor(
|
||||
(new Date(issue.closed_at).getTime() -
|
||||
new Date(issue.created_at).getTime()) /
|
||||
1000
|
||||
);
|
||||
else
|
||||
secondsDown += Math.floor(
|
||||
(new Date().getTime() - new Date(issue.created_at).getTime()) / 1000
|
||||
);
|
||||
});
|
||||
const uptime = (
|
||||
100 -
|
||||
100 *
|
||||
(secondsDown /
|
||||
((new Date().getTime() - new Date(startTime).getTime()) / 1000))
|
||||
).toFixed(2);
|
||||
if (!history.data.length) continue;
|
||||
const averageTime =
|
||||
history.data
|
||||
.filter(
|
||||
(item) =>
|
||||
item.commit.message.includes(" in ") &&
|
||||
Number(item.commit.message.split(" in ")[1].split("ms")[0]) !== 0 &&
|
||||
!isNaN(Number(item.commit.message.split(" in ")[1].split("ms")[0]))
|
||||
)
|
||||
.map((item) =>
|
||||
Number(item.commit.message.split(" in ")[1].split("ms")[0])
|
||||
)
|
||||
.reduce((p, c) => p + c, 0) / history.data.length;
|
||||
const status = history.data[0].commit.message.split(" ")[0].includes("🟩")
|
||||
? "up"
|
||||
: "down";
|
||||
pageStatuses.push({
|
||||
name: site.name,
|
||||
url: site.url,
|
||||
slug,
|
||||
status,
|
||||
uptime,
|
||||
time: Math.floor(averageTime),
|
||||
});
|
||||
if (status === "down") {
|
||||
numberOfDown++;
|
||||
}
|
||||
}
|
||||
|
||||
if (readmeContent.includes("<!--start: status pages-->")) {
|
||||
readmeContent = `${startText}<!--start: status pages-->
|
||||
|
||||
| URL | Status | History | Response Time | Uptime |
|
||||
| --- | ------ | ------- | ------------- | ------ |
|
||||
${pageStatuses
|
||||
.map(
|
||||
(page) =>
|
||||
`| ${
|
||||
page.url.startsWith("$") ? page.name : `[${page.name}](${page.url})`
|
||||
} | ${page.status === "up" ? "🟩 Up" : "🟥 Down"} | [${
|
||||
page.slug
|
||||
}.yml](https://github.com/${owner}/${repo}/commits/master/history/${
|
||||
page.slug
|
||||
}.yml) | <img alt="Response time graph" src="./graphs/${
|
||||
page.slug
|
||||
}.png" height="20"> ${page.time}ms | `
|
||||
)
|
||||
.join("\n")}
|
||||
|
||||
<!--end: status pages-->${endText}`;
|
||||
}
|
||||
|
||||
// Add live status line
|
||||
readmeContent = readmeContent
|
||||
.split("\n")
|
||||
.map((line) => {
|
||||
if (line.includes("<!--live status-->")) {
|
||||
line = `${line.split("<!--live status-->")[0]}<!--live status--> **${
|
||||
numberOfDown === 0
|
||||
? "🟩 All systems operational"
|
||||
: numberOfDown === config.sites.length
|
||||
? "🟥 Complete outage"
|
||||
: "🟨 Partial outage"
|
||||
}**`;
|
||||
}
|
||||
return line;
|
||||
})
|
||||
.join("\n");
|
||||
|
||||
const sha = (
|
||||
await octokit.repos.getContent({
|
||||
owner,
|
||||
repo,
|
||||
path: "README.md",
|
||||
})
|
||||
).data.sha;
|
||||
await octokit.repos.createOrUpdateFileContents({
|
||||
owner,
|
||||
repo,
|
||||
path: "README.md",
|
||||
message: ":pencil: Update summary in README [skip ci]",
|
||||
content: Buffer.from(readmeContent).toString("base64"),
|
||||
sha,
|
||||
});
|
||||
let summarySha: string | undefined = undefined;
|
||||
try {
|
||||
summarySha = (
|
||||
await octokit.repos.getContent({
|
||||
owner,
|
||||
repo,
|
||||
path: "history/summary.json",
|
||||
})
|
||||
).data.sha;
|
||||
} catch (error) {}
|
||||
await octokit.repos.createOrUpdateFileContents({
|
||||
owner,
|
||||
repo,
|
||||
path: "history/summary.json",
|
||||
message: ":card_file_box: Update status summary [skip ci]",
|
||||
content: Buffer.from(JSON.stringify(pageStatuses, null, 2)).toString(
|
||||
"base64"
|
||||
),
|
||||
sha: summarySha,
|
||||
});
|
||||
};
|
||||
@@ -1,20 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"moduleResolution": "node",
|
||||
"target": "esnext",
|
||||
"module": "commonjs",
|
||||
"lib": ["dom", "esnext"],
|
||||
"strict": true,
|
||||
"sourceMap": true,
|
||||
"declaration": true,
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"declarationDir": "./dist",
|
||||
"outDir": "./dist",
|
||||
"typeRoots": ["node_modules/@types", "@types"]
|
||||
},
|
||||
"include": ["update.ts", "readme.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
@@ -1,295 +0,0 @@
|
||||
import { Octokit } from "@octokit/rest";
|
||||
import slugify from "@sindresorhus/slugify";
|
||||
import { readFile } from "fs-extra";
|
||||
import { safeLoad } from "js-yaml";
|
||||
import axios from "axios";
|
||||
import { Curl, CurlFeature } from "node-libcurl";
|
||||
import { join } from "path";
|
||||
import { generateSummary } from "./summary";
|
||||
|
||||
const shouldCommit = process.argv[2] === "commit";
|
||||
|
||||
export const update = async () => {
|
||||
const config = safeLoad(
|
||||
await readFile(join(".", ".upptimerc.yml"), "utf8")
|
||||
) as {
|
||||
sites: Array<{
|
||||
name: string;
|
||||
url: string;
|
||||
method?: string;
|
||||
assignees?: string[];
|
||||
}>;
|
||||
notifications?: Array<{
|
||||
type: string;
|
||||
[index: string]: string;
|
||||
}>;
|
||||
owner: string;
|
||||
repo: string;
|
||||
userAgent?: string;
|
||||
PAT?: string;
|
||||
assignees?: string[];
|
||||
};
|
||||
const owner = config.owner;
|
||||
const repo = config.repo;
|
||||
|
||||
const octokit = new Octokit({
|
||||
auth: config.PAT || process.env.GH_PAT || process.env.GITHUB_TOKEN,
|
||||
userAgent: config.userAgent || process.env.USER_AGENT || "KojBot",
|
||||
});
|
||||
|
||||
let hasDelta = false;
|
||||
for await (const site of config.sites) {
|
||||
const slug = slugify(site.name);
|
||||
console.log("Checking", site.url);
|
||||
let currentStatus = "unknown";
|
||||
let startTime = new Date().toISOString();
|
||||
|
||||
try {
|
||||
currentStatus =
|
||||
(await readFile(join(".", "history", `${slug}.yml`), "utf8"))
|
||||
.split("\n")
|
||||
.find((line) => line.toLocaleLowerCase().includes("- status"))
|
||||
?.split(":")[1]
|
||||
.trim() || "unknown";
|
||||
startTime =
|
||||
(await readFile(join(".", "history", `${slug}.yml`), "utf8"))
|
||||
.split("\n")
|
||||
.find((line) => line.toLocaleLowerCase().includes("- starttime"))
|
||||
?.split("startTime:")[1]
|
||||
.trim() || new Date().toISOString();
|
||||
} catch (error) {}
|
||||
|
||||
const performTestOnce = async () => {
|
||||
const result = await curl(
|
||||
site.url.startsWith("$")
|
||||
? process.env[site.url.substr(1, site.url.length)] || ""
|
||||
: site.url,
|
||||
site.method
|
||||
);
|
||||
console.log("Result", result);
|
||||
const responseTime = (result.totalTime * 1000).toFixed(0);
|
||||
const status: "up" | "down" =
|
||||
result.httpCode >= 400 || result.httpCode < 200 ? "down" : "up";
|
||||
return { result, responseTime, status };
|
||||
};
|
||||
|
||||
let { result, responseTime, status } = await performTestOnce();
|
||||
/**
|
||||
* If the site is down, we perform the test 2 more times to make
|
||||
* sure that it's not a false alarm
|
||||
*/
|
||||
if (status === "down") {
|
||||
wait(1000);
|
||||
const secondTry = await performTestOnce();
|
||||
if (secondTry.status === "up") {
|
||||
result = secondTry.result;
|
||||
responseTime = secondTry.responseTime;
|
||||
status = secondTry.status;
|
||||
} else {
|
||||
wait(10000);
|
||||
const thirdTry = await performTestOnce();
|
||||
if (thirdTry.status === "up") {
|
||||
result = thirdTry.result;
|
||||
responseTime = thirdTry.responseTime;
|
||||
status = thirdTry.status;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (shouldCommit || currentStatus !== status) {
|
||||
const content = `- url: ${site.url}
|
||||
- status: ${status}
|
||||
- code: ${result.httpCode}
|
||||
- responseTime: ${responseTime}
|
||||
- lastUpdated: ${new Date().toISOString()}
|
||||
- startTime: ${startTime}
|
||||
`;
|
||||
|
||||
let sha: string | undefined = "";
|
||||
try {
|
||||
sha = (
|
||||
await octokit.repos.getContent({
|
||||
owner,
|
||||
repo,
|
||||
path: `history/${slug}.yml`,
|
||||
})
|
||||
).data.sha;
|
||||
} catch (error) {}
|
||||
const fileUpdateResult = await octokit.repos.createOrUpdateFileContents(
|
||||
{
|
||||
owner,
|
||||
repo,
|
||||
path: `history/${slug}.yml`,
|
||||
message: `${status === "up" ? "🟩" : "🟥"} ${
|
||||
site.name
|
||||
} is ${status} (${result.httpCode} in ${responseTime}ms) [skip ci]`,
|
||||
content: Buffer.from(content).toString("base64"),
|
||||
sha,
|
||||
}
|
||||
);
|
||||
|
||||
if (currentStatus !== status) {
|
||||
console.log("Status is different", currentStatus, "to", status);
|
||||
hasDelta = true;
|
||||
|
||||
const issues = await octokit.issues.list({
|
||||
owner,
|
||||
repo,
|
||||
labels: slug,
|
||||
filter: "all",
|
||||
state: "open",
|
||||
sort: "created",
|
||||
direction: "desc",
|
||||
per_page: 1,
|
||||
});
|
||||
console.log(`Found ${issues.data.length} issues`);
|
||||
|
||||
// If the site was just recorded as down, open an issue
|
||||
if (status === "down") {
|
||||
if (!issues.data.length) {
|
||||
const newIssue = await octokit.issues.create({
|
||||
owner,
|
||||
repo,
|
||||
title: `🛑 ${site.name} is down`,
|
||||
body: `In [\`${fileUpdateResult.data.commit.sha.substr(
|
||||
0,
|
||||
7
|
||||
)}\`](https://github.com/${owner}/${repo}/commit/${
|
||||
fileUpdateResult.data.commit.sha
|
||||
}), ${site.name} (${site.url}) was **down**:
|
||||
|
||||
- HTTP code: ${result.httpCode}
|
||||
- Response time: ${responseTime} ms
|
||||
`,
|
||||
assignees: [
|
||||
...(config.assignees || []),
|
||||
...(site.assignees || []),
|
||||
],
|
||||
labels: ["status", slug],
|
||||
});
|
||||
await octokit.issues.lock({
|
||||
owner,
|
||||
repo,
|
||||
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}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
console.log("Sent notifications");
|
||||
} else {
|
||||
console.log("An issue is already open for this");
|
||||
}
|
||||
} else if (issues.data.length) {
|
||||
// If the site just came back up
|
||||
await octokit.issues.createComment({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: issues.data[0].number,
|
||||
body: `**Resolved:** ${
|
||||
site.name
|
||||
} is back up in [\`${fileUpdateResult.data.commit.sha.substr(
|
||||
0,
|
||||
7
|
||||
)}\`](https://github.com/${owner}/${repo}/commit/${
|
||||
fileUpdateResult.data.commit.sha
|
||||
}).`,
|
||||
});
|
||||
console.log("Created comment in issue");
|
||||
await octokit.issues.update({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: issues.data[0].number,
|
||||
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}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
console.log("Sent notifications");
|
||||
} else {
|
||||
console.log("Could not find a relevant issue", issues.data);
|
||||
}
|
||||
} else {
|
||||
console.log("Status is the same", currentStatus, status);
|
||||
}
|
||||
} else {
|
||||
console.log("Skipping commit, ", "status is", status);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("ERROR", error);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasDelta) generateSummary();
|
||||
};
|
||||
|
||||
const curl = (
|
||||
url: string,
|
||||
method = "GET"
|
||||
): Promise<{ httpCode: number; totalTime: number }> =>
|
||||
new Promise((resolve) => {
|
||||
const curl = new Curl();
|
||||
curl.enable(CurlFeature.Raw);
|
||||
curl.setOpt("URL", url);
|
||||
curl.setOpt("FOLLOWLOCATION", 1);
|
||||
curl.setOpt("MAXREDIRS", 3);
|
||||
curl.setOpt("USERAGENT", "Koj Bot");
|
||||
curl.setOpt("CONNECTTIMEOUT", 10);
|
||||
curl.setOpt("TIMEOUT", 30);
|
||||
curl.setOpt("HEADER", 1);
|
||||
curl.setOpt("VERBOSE", false);
|
||||
curl.setOpt("CUSTOMREQUEST", method);
|
||||
curl.on("error", () => {
|
||||
curl.close();
|
||||
return resolve({ httpCode: 0, totalTime: 0 });
|
||||
});
|
||||
curl.on("end", () => {
|
||||
let httpCode = 0;
|
||||
let totalTime = 0;
|
||||
try {
|
||||
httpCode = Number(curl.getInfo("RESPONSE_CODE"));
|
||||
totalTime = Number(curl.getInfo("TOTAL_TIME"));
|
||||
} catch (error) {
|
||||
curl.close();
|
||||
return resolve({ httpCode, totalTime });
|
||||
}
|
||||
return resolve({ httpCode, totalTime });
|
||||
});
|
||||
curl.perform();
|
||||
});
|
||||
|
||||
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
update();
|
||||
Reference in New Issue
Block a user