This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: run checks regularly on a schedule (#235)
- Loading branch information
1 parent
64375fc
commit 228d387
Showing
3 changed files
with
115 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
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,14 @@ | ||
name: Run checks | ||
permissions: read-all | ||
on: | ||
workflow_dispatch: | ||
schedule: | ||
- cron: 0 3 * * * | ||
jobs: | ||
api: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: hit api | ||
uses: fjogeleit/http-request-action@v1 | ||
with: | ||
url: "https://healthcheck.eddiehubcommunity.org/api/system/checks?token=${{ secrets.API_TOKEN }}" |
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,100 @@ | ||
import { differenceInHours } from "date-fns"; | ||
|
||
import prisma from "@/models/db"; | ||
import getAllRepoData from "@/utils/github"; | ||
import checks from "@/utils/checks"; | ||
|
||
export const dynamic = "force-dynamic"; | ||
|
||
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 }); | ||
} | ||
|
||
// get all repositories | ||
const repos = await prisma.repository.findMany({ | ||
include: { | ||
checks: { | ||
orderBy: { | ||
createdAt: "desc", | ||
}, | ||
take: 1, | ||
}, | ||
user: { | ||
include: { | ||
accounts: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// filter out repositories that had checks in the last X hours | ||
let repoStatus = { ignore: [], run: [] }; | ||
repos.forEach((repo) => { | ||
if ( | ||
!repo.checks[0] || | ||
differenceInHours(new Date(), repo.checks[0].createdAt) >= 24 * 7 // TODO: move to Flagsmith | ||
) { | ||
repoStatus.run.push({ | ||
id: repo.id, | ||
url: repo.url, | ||
token: repo.user.accounts[0].access_token, | ||
lastChecked: repo.checks[0].createdAt, | ||
}); | ||
} else { | ||
repoStatus.ignore.push({ | ||
id: repo.id, | ||
url: repo.url, | ||
lastChecked: repo.checks[0].createdAt, | ||
}); | ||
} | ||
}); | ||
|
||
// perform checks on these repositories | ||
// use the owners github token (repository->user->account.access_token) | ||
let runs = []; | ||
repoStatus.run.map(async (repo) => { | ||
const responses = await getAllRepoData(repo.url, repo.token); | ||
|
||
// save github api data | ||
const githubResponseRepo = await prisma.githubResponse.create({ | ||
data: { | ||
repository: { | ||
connect: { | ||
id: repo.id, | ||
}, | ||
}, | ||
...responses, | ||
}, | ||
}); | ||
|
||
// perform check | ||
const results = checks(githubResponseRepo); | ||
|
||
// 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, | ||
}, | ||
}); | ||
|
||
runs.push({ url: repo.url }); | ||
}); | ||
|
||
console.log(runs); | ||
|
||
return Response.json(runs); | ||
} |