Skip to content

Commit

Permalink
✨ Add support for custom CI schedule (fixed #71)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 28, 2020
1 parent 8c78d77 commit 3d93e8c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/helpers/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const GRAPHS_CI_SCHEDULE = "0 0 * * *";
export const RESPONSE_TIME_CI_SCHEDULE = "0 23 * * *";
export const STATIC_SITE_CI_SCHEDULE = "0 1 * * *";
export const SUMMARY_CI_SCHEDULE = "0 0 * * *";
export const UPDATE_TEMPLATE_CI_SCHEDULE = "0 0 * * *";
export const UPDATES_CI_SCHEDULE = "0 3 * * *";
export const UPTIME_CI_SCHEDULE = "*/5 * * * *";
9 changes: 9 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ export interface UpptimeConfig {
completeOutage?: string;
partialOutage?: string;
} & Record<string, string>;
workflowSchedule: {
graphs?: string;
responseTime?: string;
staticSite?: string;
summary?: string;
updateTemplate?: string;
updates?: string;
uptime?: string;
};
}

export interface SiteHistory {
Expand Down
40 changes: 39 additions & 1 deletion src/update-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ import { execSync } from "child_process";
import { copy, readdir, readFile, remove, writeFile } from "fs-extra";
import { join } from "path";
import { getConfig } from "./helpers/config";
import {
GRAPHS_CI_SCHEDULE,
RESPONSE_TIME_CI_SCHEDULE,
STATIC_SITE_CI_SCHEDULE,
SUMMARY_CI_SCHEDULE,
UPDATE_TEMPLATE_CI_SCHEDULE,
UPDATES_CI_SCHEDULE,
UPTIME_CI_SCHEDULE,
} from "./helpers/constants";
import { commit, push } from "./helpers/git";
import { getOctokit } from "./helpers/github";

export const updateTemplate = async () => {
const [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/");
const octokit = await getOctokit();
const config = await getConfig();

// Remove the .github/workflows directory completely
await remove(join(".", ".github", "workflows"));
Expand All @@ -28,13 +38,42 @@ export const updateTemplate = async () => {
await copy(join(".", "__upptime", "src", "workflows"), join(".", ".github", "workflows"));
await remove(join(".", "__upptime"));
const workflowFiles = await readdir(join(".", ".github", "workflows"));
const workflowSchedule = config.workflowSchedule || {};
for await (const file of workflowFiles) {
const contents = await readFile(join(".", ".github", "workflows", file), "utf8");
await writeFile(
join(".", ".github", "workflows", file),
contents
.replace(new RegExp("UPTIME_MONITOR_VERSION", "g"), latestRelease)
.replace(new RegExp("CURRENT_DATE", "g"), new Date().toISOString())
.replace(
new RegExp("GRAPHS_CI_SCHEDULE", "g"),
workflowSchedule.graphs || GRAPHS_CI_SCHEDULE
)
.replace(
new RegExp("RESPONSE_TIME_CI_SCHEDULE", "g"),
workflowSchedule.responseTime || RESPONSE_TIME_CI_SCHEDULE
)
.replace(
new RegExp("STATIC_SITE_CI_SCHEDULE", "g"),
workflowSchedule.staticSite || STATIC_SITE_CI_SCHEDULE
)
.replace(
new RegExp("SUMMARY_CI_SCHEDULE", "g"),
workflowSchedule.summary || SUMMARY_CI_SCHEDULE
)
.replace(
new RegExp("UPDATE_TEMPLATE_CI_SCHEDULE", "g"),
workflowSchedule.updateTemplate || UPDATE_TEMPLATE_CI_SCHEDULE
)
.replace(
new RegExp("UPDATES_CI_SCHEDULE", "g"),
workflowSchedule.updates || UPDATES_CI_SCHEDULE
)
.replace(
new RegExp("UPTIME_CI_SCHEDULE", "g"),
workflowSchedule.uptime || UPTIME_CI_SCHEDULE
)
);
}
console.log("Added new .github/workflows");
Expand All @@ -47,7 +86,6 @@ export const updateTemplate = async () => {
} catch (error) {}
console.log("Removed template files");

const config = await getConfig();
const slugs = config.sites.map((site) => site.slug || slugify(site.name));
const filesToKeep = ["LICENSE"];

Expand Down

0 comments on commit 3d93e8c

Please sign in to comment.