generated from pabio/github-actions-starter
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Add helpers for uptime, config, octokit
- Loading branch information
1 parent
52c8d45
commit 45e0612
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { readFile } from "fs-extra"; | ||
import { safeLoad } from "js-yaml"; | ||
import { getOctokit } from "./github"; | ||
import { join } from "path"; | ||
import { SiteHistory } from "../interfaces"; | ||
|
||
/** | ||
* Get the number of seconds a website has been down | ||
* @param slug - Slug of the site | ||
*/ | ||
const getDowntimeSecondsForSite = async (slug: string): Promise<number> => { | ||
let [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/"); | ||
const octokit = await getOctokit(); | ||
let msDown = 0; | ||
|
||
// Get all the issues for this website | ||
const { data } = await octokit.issues.listForRepo({ | ||
owner, | ||
repo, | ||
labels: `status,${slug}`, | ||
filter: "all", | ||
per_page: 100, | ||
}); | ||
|
||
// If this issue has been closed already, calculate the difference | ||
// between when it was closed and when it was opened | ||
// If this issue is still open, calculate the time since it was opened | ||
data.forEach( | ||
(issue) => | ||
(msDown += | ||
new Date(issue.closed_at || new Date()).getTime() - new Date(issue.created_at).getTime()) | ||
); | ||
|
||
return Math.round(msDown / 1000); | ||
}; | ||
|
||
/** | ||
* Get the uptime percentage for a website | ||
* @returns Percent string, e.g., 94.43% | ||
* @param slug - Slug of the site | ||
*/ | ||
export const getUptimePercentForSite = async (slug: string): Promise<string> => { | ||
const site = safeLoad(await readFile(join(".", "history", `${slug}.yml`), "utf8")) as SiteHistory; | ||
// Time when we started tracking this website's downtime | ||
const startDate = new Date(site.startTime ?? new Date()); | ||
|
||
// Number of seconds we have been tracking this site | ||
const totalSeconds = (new Date().getTime() - startDate.getTime()) / 1000; | ||
|
||
// Number of seconds the site has been down | ||
const downtimeSeconds = await getDowntimeSecondsForSite(slug); | ||
|
||
// Return a percentage string | ||
return `${((downtimeSeconds / totalSeconds) * 100).toFixed(2)}%`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { readFile } from "fs-extra"; | ||
import { safeLoad } from "js-yaml"; | ||
import { join } from "path"; | ||
import { UpptimeConfig } from "../interfaces"; | ||
|
||
let __config: UpptimeConfig | undefined = undefined; | ||
export const getConfig = async (): Promise<UpptimeConfig> => { | ||
if (__config) return __config; | ||
const config = safeLoad(await readFile(join(".", ".upptimerc.yml"), "utf8")) as UpptimeConfig; | ||
__config = config; | ||
return config; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Octokit } from "@octokit/rest"; | ||
import { getConfig } from "./config"; | ||
|
||
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", | ||
}); | ||
}; |