From 69301597293ae5055a585b5a1615ab18a572eee0 Mon Sep 17 00:00:00 2001 From: Eddie Jaoude Date: Wed, 2 Oct 2024 16:36:50 +0100 Subject: [PATCH] feat: branches check --- src/checks/{branches.js => branches.ts} | 14 ++++++++------ src/checks/index.ts | 2 +- src/lib/github/getAllApi.ts | 3 +++ src/lib/github/getBranchesApi.ts | 16 ++++++++++++++++ src/models/data.ts | 2 ++ src/models/github/branch.ts | 19 +++++++++++++++++++ 6 files changed, 49 insertions(+), 7 deletions(-) rename src/checks/{branches.js => branches.ts} (78%) create mode 100644 src/lib/github/getBranchesApi.ts create mode 100644 src/models/github/branch.ts diff --git a/src/checks/branches.js b/src/checks/branches.ts similarity index 78% rename from src/checks/branches.js rename to src/checks/branches.ts index 6a6e9ff..e6a262d 100644 --- a/src/checks/branches.js +++ b/src/checks/branches.ts @@ -1,19 +1,21 @@ -export default function branches(branches) { +import { Branch } from "@/models/github/branch"; +import { StatusCheck } from "@/types/checks"; + +export default function branches(branches: Branch[]) { const min = 5; const max = 10; - let response = { - id: "branches", - href: "/repo/status", + const response: StatusCheck = { title: "Branches", + status: "unknown", + description: "-", + extra: "-", }; if (!branches) { return { ...response, - status: "-", description: "No data available", - extra: "-", }; } diff --git a/src/checks/index.ts b/src/checks/index.ts index 3a1ccfa..c2ade64 100644 --- a/src/checks/index.ts +++ b/src/checks/index.ts @@ -26,7 +26,7 @@ export default function checks(data: Data) { issues(data.repo), defaultBranch(data.repo), goodFirstIssue(data.issues), - // branches(data.branches), + branches(data.branches), // release(data.release), readme(data.community), license(data.community), diff --git a/src/lib/github/getAllApi.ts b/src/lib/github/getAllApi.ts index aedd7a3..56654d8 100644 --- a/src/lib/github/getAllApi.ts +++ b/src/lib/github/getAllApi.ts @@ -2,18 +2,21 @@ import Data from "@/models/data"; import getRepoApi from "./getRepoApi"; import getIssuesApi from "./getIssuesApi"; import getCommunityApi from "./getCommunityApi"; +import getBranchesApi from "./getBranchesApi"; export default async function getAllApi(repoUrl: string): Promise { const calls = await Promise.all([ getRepoApi(repoUrl), getIssuesApi(repoUrl), getCommunityApi(repoUrl), + getBranchesApi(repoUrl), ]); const data: Data = { repo: calls[0], issues: calls[1], community: calls[2], + branches: calls[3], }; return data; diff --git a/src/lib/github/getBranchesApi.ts b/src/lib/github/getBranchesApi.ts new file mode 100644 index 0000000..1117aaf --- /dev/null +++ b/src/lib/github/getBranchesApi.ts @@ -0,0 +1,16 @@ +import extractOwnerRepo from "./extractOwnerRepo"; +import { Branch } from "@/models/github/branch"; + +export default async function getBranchesApi(repoUrl: string) { + const { owner, repo } = extractOwnerRepo(repoUrl); + const res = await fetch( + `https://api.github.com/repos/${owner}/${repo}/branches`, + { + next: { revalidate: 3600 }, + } + ); + + const data: Branch[] = await res.json(); + + return data; +} diff --git a/src/models/data.ts b/src/models/data.ts index 0022194..69dd56c 100644 --- a/src/models/data.ts +++ b/src/models/data.ts @@ -1,3 +1,4 @@ +import { Branch } from "./github/branch"; import { Community } from "./github/community"; import { Issue } from "./github/issue"; import { Repo } from "./github/repo"; @@ -6,4 +7,5 @@ export default interface Data { repo: Repo; issues: Issue[]; community: Community; + branches: Branch[]; } diff --git a/src/models/github/branch.ts b/src/models/github/branch.ts new file mode 100644 index 0000000..5c30381 --- /dev/null +++ b/src/models/github/branch.ts @@ -0,0 +1,19 @@ +// To parse this data: +// +// import { Convert } from "./file"; +// +// const branch = Convert.toBranch(json); +// +// These functions will throw an error if the JSON doesn't +// match the expected interface, even if the JSON is valid. + +export interface Branch { + name: string; + commit: Commit; + protected: boolean; +} + +export interface Commit { + sha: string; + url: string; +}