Skip to content

Commit

Permalink
refactor: remove dependency on github addresses #94
Browse files Browse the repository at this point in the history
  • Loading branch information
Raj Nandan Sharma authored and Raj Nandan Sharma committed Nov 29, 2024
1 parent a824a5d commit 749593f
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 170 deletions.
70 changes: 26 additions & 44 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ async function Build() {
site.hasGithub = true;
}

if (site.hasGithub && !!!site.github.incidentSince) {
site.github.incidentSince = 720;
}
if (site.hasGithub && !!!site.github.apiURL) {
site.github.apiURL = "https://api.github.com";
}

const FOLDER_DB = databaseFolder;
const FOLDER_SITE = FOLDER_DB + "/site.json";
const FOLDER_MONITOR = FOLDER_DB + "/monitors.json";
Expand Down Expand Up @@ -275,9 +282,6 @@ async function Build() {
monitors[i].hasAPI = hasAPI;
}

if (site.github.incidentSince === undefined || site.github.incidentSince === null) {
site.github.incidentSince = 720;
}
if (site.siteName === undefined) {
site.siteName = site.title;
}
Expand Down Expand Up @@ -336,7 +340,6 @@ async function Build() {

fs.ensureFileSync(FOLDER_MONITOR);
fs.ensureFileSync(FOLDER_SITE);

try {
fs.writeFileSync(FOLDER_MONITOR, JSON.stringify(monitors, null, 4));
fs.writeFileSync(FOLDER_SITE, JSON.stringify(site, null, 4));
Expand All @@ -346,68 +349,47 @@ async function Build() {
}

if (site.hasGithub) {
const ghowner = site.github.owner;
const ghrepo = site.github.repo;
const ghlabels = await GetAllGHLabels(ghowner, ghrepo);
const ghLabels = await GetAllGHLabels(site);
const tagsAndDescription = monitors.map((monitor) => {
return { tag: monitor.tag, description: monitor.name };
});
//add incident label if does not exist

if (ghlabels.indexOf("incident") === -1) {
await CreateGHLabel(ghowner, ghrepo, "incident", "Status of the site");
if (ghLabels.indexOf("incident") === -1) {
await CreateGHLabel(site, "incident", "Status of the site");
}
if (ghlabels.indexOf("resolved") === -1) {
await CreateGHLabel(ghowner, ghrepo, "resolved", "Incident is resolved", "65dba6");
if (ghLabels.indexOf("resolved") === -1) {
await CreateGHLabel(site, "resolved", "Incident is resolved", "65dba6");
}
if (ghlabels.indexOf("identified") === -1) {
await CreateGHLabel(ghowner, ghrepo, "identified", "Incident is Identified", "EBE3D5");
if (ghLabels.indexOf("identified") === -1) {
await CreateGHLabel(site, "identified", "Incident is Identified", "EBE3D5");
}
if (ghlabels.indexOf("manual") === -1) {
await CreateGHLabel(ghowner, ghrepo, "manual", "Manually Created Incident", "6499E9");
if (ghLabels.indexOf("manual") === -1) {
await CreateGHLabel(site, "manual", "Manually Created Incident", "6499E9");
}
if (ghlabels.indexOf("auto") === -1) {
await CreateGHLabel(
ghowner,
ghrepo,
"auto",
"Automatically Created Incident",
"D6C0B3"
);
if (ghLabels.indexOf("auto") === -1) {
await CreateGHLabel(site, "auto", "Automatically Created Incident", "D6C0B3");
}
if (ghlabels.indexOf("investigating") === -1) {
await CreateGHLabel(
ghowner,
ghrepo,
"investigating",
"Incident is investigated",
"D4E2D4"
);
if (ghLabels.indexOf("investigating") === -1) {
await CreateGHLabel(site, "investigating", "Incident is investigated", "D4E2D4");
}
if (ghlabels.indexOf("incident-degraded") === -1) {
if (ghLabels.indexOf("incident-degraded") === -1) {
await CreateGHLabel(
ghowner,
ghrepo,
site,
"incident-degraded",
"Status is degraded of the site",
"f5ba60"
);
}
if (ghlabels.indexOf("incident-down") === -1) {
await CreateGHLabel(
ghowner,
ghrepo,
"incident-down",
"Status is down of the site",
"ea3462"
);
if (ghLabels.indexOf("incident-down") === -1) {
await CreateGHLabel(site, "incident-down", "Status is down of the site", "ea3462");
}
//add tags if does not exist
for (let i = 0; i < tagsAndDescription.length; i++) {
const tag = tagsAndDescription[i].tag;
const description = tagsAndDescription[i].description;
if (ghlabels.indexOf(tag) === -1) {
await CreateGHLabel(ghowner, ghrepo, tag, description);
if (ghLabels.indexOf(tag) === -1) {
await CreateGHLabel(site, tag, description);
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"dns2": "^2.1.0",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"figlet": "^1.8.0",
"fs-extra": "^11.1.1",
"js-yaml": "^4.1.0",
"lucide-svelte": "^0.292.0",
Expand Down
10 changes: 5 additions & 5 deletions src/lib/server/alerting.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ async function createGHIncident(siteData, monitor, alert, commonData) {
}

githubLabels.push("auto");
let resp = await CreateIssue(siteData.github, title, body, githubLabels);
let resp = await CreateIssue(title, body, githubLabels);

return GHIssueToKenerIncident(resp);
}

async function closeGHIncident(siteData, alert) {
let incidentNumber = alert.incidentNumber;
let issue = await GetIncidentByNumber(siteData.github, incidentNumber);
let issue = await GetIncidentByNumber(incidentNumber);
if (issue === null) {
return;
}
Expand All @@ -94,17 +94,17 @@ async function closeGHIncident(siteData, alert) {
body = body.trim();
body = body + " " + `[end_datetime:${endDatetime}]`;

let resp = await UpdateIssueLabels(siteData.github, incidentNumber, labels, body);
let resp = await UpdateIssueLabels(incidentNumber, labels, body);
if (resp === null) {
return;
}
await CloseIssue(siteData.github, incidentNumber);
await CloseIssue(incidentNumber);
return GHIssueToKenerIncident(resp);
}

//add comment to incident
async function addCommentToIncident(siteData, alert, comment) {
let resp = await AddComment(siteData.github, alert.incidentNumber, comment);
let resp = await AddComment(alert.incidentNumber, comment);
return resp;
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/server/cron-minute.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const alertingQueue = new Queue({
});

async function manualIncident(monitor, githubConfig) {
let incidentsResp = await GetIncidentsManual(monitor.tag, githubConfig, "open");
let incidentsResp = await GetIncidentsManual(monitor.tag, "open");

let manualData = {};
if (incidentsResp.length == 0) {
Expand Down Expand Up @@ -80,7 +80,7 @@ async function manualIncident(monitor, githubConfig) {
if (end_time <= GetNowTimestampUTC() && incident.state === "open") {
//close the issue after 30 secs
setTimeout(async () => {
await CloseIssue(githubConfig, incidentNumber);
await CloseIssue(incidentNumber);
}, 30000);
}
} else {
Expand Down
Loading

0 comments on commit 749593f

Please sign in to comment.