Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

feat: move hardcode values to flagsmith #259

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/app/account/repo/settings/[id]/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ const initialState = {
errors: undefined,
};

export default function Form({ id, ignore, schedule, disabled = false }) {
let { optionalchecks } = useFlags(["optionalchecks"]);
export default function Form({ id, ignore, disabled = false }) {
let { optionalchecks, schedule } = useFlags(["optionalchecks", "schedule"]);
optionalchecks = JSON.parse(optionalchecks.value);
schedule = JSON.parse(schedule.value);
const [state, formAction] = useFormState(saveSettings, initialState);

return (
Expand Down Expand Up @@ -55,9 +56,9 @@ export default function Form({ id, ignore, schedule, disabled = false }) {
<legend className="text-sm font-semibold leading-6">Automate</legend>
<div className="mt-2">
<Checkbox
id="schedule"
name="schedule"
text="Schedule"
id="automate"
name="automate"
text="Automate"
value={true}
disabled={true}
defaultChecked={true}
Expand All @@ -71,13 +72,9 @@ export default function Form({ id, ignore, schedule, disabled = false }) {
id="schedule"
name="schedule"
text="How often to perform these checks? (days)"
options={[
{ text: "1 day", value: 1 },
{ text: "7 days", value: 7 },
{ text: "30 days", value: 30 },
]}
options={schedule}
value={7}
disabled={true}
// disabled={true}
// defaultChecked={schedule}
classNameSelect="max-w-32"
/>
Expand Down
101 changes: 60 additions & 41 deletions src/app/api/system/checks/route.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { differenceInHours } from "date-fns";
import { differenceInSeconds } from "date-fns";
import Flagsmith from "flagsmith-nodejs";

import prisma from "@/models/db";
import getAllRepoData from "@/utils/github";
import checks from "@/utils/checks";

export const dynamic = "force-dynamic";

// TODO: move to a reusable location (server-side only)
const flagsmith = new Flagsmith({
environmentKey: process.env.FLAGSMITH_ENVIRONMENT_API_KEY,
});

export async function GET(request, { params }) {
// protect for system calls only with valid token
if (request.nextUrl.searchParams.get("token") !== process.env.API_TOKEN) {
return Response.json({ error: "permission denied" }, { status: 401 });
}

let flags;
let refresh;
try {
flags = await flagsmith.getEnvironmentFlags();
refresh = flags.getFeatureValue("refresh");
} catch (e) {
console.log(e);
refresh = 24 * 7;
}

// get all repositories
const repos = await prisma.repository.findMany({
include: {
Expand All @@ -35,7 +51,7 @@ export async function GET(request, { params }) {
try {
if (
!repo.checks[0] ||
differenceInHours(new Date(), repo.checks[0].createdAt) >= 24 * 7 // TODO: move to Flagsmith
differenceInSeconds(new Date(), repo.checks[0].createdAt) >= refresh
) {
repoStatus.run.push({
id: repo.id,
Expand All @@ -59,56 +75,59 @@ export async function GET(request, { params }) {
}
});

console.log("CHECKS IGNORED", repoStatus.ignore);
console.log("CHECKS ERRORED", repoStatus.error);
console.log("CHECKS IGNORED", repoStatus.ignore.length);
console.log("CHECKS ERRORED", repoStatus.error.length);
console.log("CHECKS GOING TO RUN", repoStatus.run.length);

// perform checks on these repositories
// use the owners github token (repository->user->account.access_token)
let runs = { attempted: [], successful: [] };
repoStatus.run.map(async (repo) => {
try {
const responses = await getAllRepoData(repo.url, repo.token);
await Promise.all(
repoStatus.run.map(async (repo) => {
try {
const responses = await getAllRepoData(repo.url, repo.token);

// save github api data
const githubResponseRepo = await prisma.githubResponse.create({
data: {
repository: {
connect: {
id: repo.id,
// save github api data
const githubResponseRepo = await prisma.githubResponse.create({
data: {
repository: {
connect: {
id: repo.id,
},
},
...responses,
},
...responses,
},
});
});

// perform check
const results = checks(githubResponseRepo);
// perform check
const results = checks(githubResponseRepo);

// save results
await prisma.check.create({
data: {
repository: {
connect: { id: repo.id },
},
githubResponse: {
connect: { id: githubResponseRepo.id },
// save results
await prisma.check.create({
data: {
repository: {
connect: { id: repo.id },
},
githubResponse: {
connect: { id: githubResponseRepo.id },
},
red: results.summary.error?.length || 0,
amber: results.summary.warning?.length || 0,
green: results.summary.success?.length || 0,
healthchecks: results.checks.map((check) => check.id),
data: results.checks,
allData: results.allChecks,
ignoreChecks: results.ignoreChecks,
},
red: results.summary.error?.length || 0,
amber: results.summary.warning?.length || 0,
green: results.summary.success?.length || 0,
healthchecks: results.checks.map((check) => check.id),
data: results.checks,
allData: results.allChecks,
ignoreChecks: results.ignoreChecks,
},
});
});

runs.successful.push({ url: repo.url });
} catch (e) {
console.error(e);
runs.attempted.push({ url: repo.url });
}
});
runs.successful.push({ url: repo.url });
} catch (e) {
console.error(e);
runs.attempted.push({ url: repo.url });
}
}),
);

console.log("CHECKS PERFORMED", runs);

Expand Down
Loading