diff --git a/src/app/account/repo/checks/[id]/action.js b/src/app/account/repo/checks/[id]/action.js index c9ddac3..7d41643 100644 --- a/src/app/account/repo/checks/[id]/action.js +++ b/src/app/account/repo/checks/[id]/action.js @@ -1,5 +1,6 @@ "use server"; +import { redirect } from "next/navigation"; import { getServerSession } from "next-auth/next"; import { differenceInHours } from "date-fns"; @@ -59,7 +60,7 @@ export async function performChecks(formData) { const results = checks(githubResponseRepo.repo); // save results - await prisma.check.create({ + const check = await prisma.check.create({ data: { repository: { connect: { id }, @@ -75,5 +76,5 @@ export async function performChecks(formData) { }, }); - // redirect + redirect(`/account/repo/report/${check.id}`); } diff --git a/src/app/account/repo/checks/[id]/page.js b/src/app/account/repo/checks/[id]/page.js index 0fc4320..101746e 100644 --- a/src/app/account/repo/checks/[id]/page.js +++ b/src/app/account/repo/checks/[id]/page.js @@ -6,33 +6,7 @@ import prisma from "@/models/db"; import List from "@/components/List"; import Title from "@/components/Title"; import Form from "./form"; - -const repos = [ - { - id: 1, - href: "/repo/status", - title: "10/5/2024", - status: "success", - extra: "Initiated 1m 32s ago", - description: "Deploys from GitHub", - }, - { - id: 2, - href: "/repo/status", - title: "2/3/2024", - status: "warning", - extra: "Initiated 1m 32s ago", - description: "Deploys from GitHub", - }, - { - id: 3, - href: "/repo/status", - title: "1/1/2024", - status: "error", - extra: "Initiated 1m 32s ago", - description: "Deploys from GitHub", - }, -]; +import { worstCheck } from "@/utils/checks"; export default async function Page({ params }) { const { id } = params; @@ -52,18 +26,20 @@ export default async function Page({ params }) { }, }, }); - console.log("======", repository); + return ( <> - + <Title + text={`Check list for the repo: ${repository.owner} / ${repository.repo}`} + > <Form id={id} /> ({ id: check.id, - href: `/account/repo/status/${check.id}`, - title: "1/1/2024", - status: "error", + href: `/account/repo/report/${check.id}`, + title: `Error: ${check.red}, Warning: ${check.amber}, Successful: ${check.green}`, + status: worstCheck(check), extra: `Added ${formatDistance(check.createdAt, new Date(), { addSuffix: true, })}`, diff --git a/src/app/account/repo/list/page.js b/src/app/account/repo/list/page.js index dce1fb7..368b935 100644 --- a/src/app/account/repo/list/page.js +++ b/src/app/account/repo/list/page.js @@ -5,6 +5,7 @@ import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import prisma from "@/models/db"; import List from "@/components/List"; import Title from "@/components/Title"; +import { worstCheck } from "@/utils/checks"; export default async function Page() { const session = await getServerSession(authOptions); @@ -15,10 +16,21 @@ export default async function Page() { const user = await prisma.user.findUnique({ where: { id: session.user.id }, include: { - repositories: true, + repositories: { + include: { + checks: { + orderBy: { + createdAt: "desc", + }, + take: 1, + }, + }, + }, }, }); + console.log(user.repositories[0].checks); + return ( <> @@ -27,11 +39,21 @@ export default async function Page() { id: repo.id, href: `/account/repo/checks/${repo.id}`, title: `${repo.owner} / ${repo.repo}`, - status: "-", - extra: `Added ${formatDistance(repo.createdAt, new Date(), { + status: repo.checks[0] ? worstCheck(repo.checks[0]) : "-", + description: `Added ${formatDistance(repo.createdAt, new Date(), { addSuffix: true, })}`, - description: "-", + extra: repo.checks[0] + ? `Last check performed ${formatDistance( + repo.checks[0].createdAt, + new Date(), + { + addSuffix: true, + } + )} with ${repo.checks[0].red} error(s), ${ + repo.checks[0].amber + } warning(s), ${repo.checks[0].green} success(es)` + : "No checks performed yet", }))} /> </> diff --git a/src/app/account/repo/report/[id]/page.js b/src/app/account/repo/report/[id]/page.js new file mode 100644 index 0000000..2da68c2 --- /dev/null +++ b/src/app/account/repo/report/[id]/page.js @@ -0,0 +1,98 @@ +import { + ClockIcon, + CodeBracketIcon, + LanguageIcon, + StarIcon, + TicketIcon, +} from "@heroicons/react/20/solid"; +import { getServerSession } from "next-auth/next"; +import { formatDistance } from "date-fns"; + +import { authOptions } from "@/app/api/auth/[...nextauth]/route"; +import prisma from "@/models/db"; +import Heading from "@/components/Heading"; +import List from "@/components/List"; +import Stats from "@/components/Stats"; +import { checkSummary } from "@/utils/checks"; + +export default async function Page({ params }) { + const id = params.id; + const session = await getServerSession(authOptions); + if (!session) { + redirect("/"); + } + + const check = await prisma.check.findUnique({ + where: { id }, + include: { + repository: { + include: { + user: true, + }, + }, + githubResponse: true, + }, + }); + + // TODO add to above query and redirect if no result + if (session.user.id !== check.repository.user.id) { + redirect("/"); + } + + const summary = checkSummary(check.data); + + return ( + <> + <Heading + title={`${check.repository.owner} / ${check.repository.repo}`} + actions={[ + { + text: "GitHub Repo", + url: check.repository.url, + icon: CodeBracketIcon, + }, + ]} + extras={[ + { icon: StarIcon, text: check.githubResponse.repo.stargazers_count }, + { icon: LanguageIcon, text: check.githubResponse.repo.language }, + { + icon: ClockIcon, + text: formatDistance( + check.githubResponse.repo.updated_at, + new Date(), + { + addSuffix: true, + } + ), + }, + { icon: TicketIcon, text: check.githubResponse.repo.open_issues }, + ]} + /> + <Stats + data={[ + { + name: "Success", + stat: summary.success?.length || 0, + status: "success", + }, + { + name: "Warning", + stat: summary.warning?.length || 0, + status: "warning", + }, + { name: "Error", stat: summary.error?.length || 0, status: "error" }, + ]} + /> + <List + data={check.data.map((check) => ({ + id: check.id, + href: `/checks/${check.id}`, + title: check.title, + status: check.status, + extra: check.extra, + description: check.description, + }))} + /> + </> + ); +} diff --git a/src/app/account/repo/status/[...repo]/page.js b/src/app/account/repo/status/[...repo]/page.js deleted file mode 100644 index 77dcb43..0000000 --- a/src/app/account/repo/status/[...repo]/page.js +++ /dev/null @@ -1,71 +0,0 @@ -import { - ClockIcon, - CodeBracketIcon, - LanguageIcon, - StarIcon, - TicketIcon, -} from "@heroicons/react/20/solid"; -import { getServerSession } from "next-auth/next"; -import { formatDistance } from "date-fns"; - -import { authOptions } from "@/app/api/auth/[...nextauth]/route"; -import prisma from "@/models/db"; -import Heading from "@/components/Heading"; -import List from "@/components/List"; -import Stats from "@/components/Stats"; - -export default async function Page({ params }) { - const repositoryId = params.repo[0]; - const checkId = params.repo[1]; - const session = await getServerSession(authOptions); - - const repository = await prisma.repository.findUnique({ - where: { - id: repositoryId, - user: { id: session.user.id }, - checks: { id: checkId }, - }, - include: { - user: true, - checks: true, - }, - }); - - const results = repository.checks[0].data; - - console.log("======", repository); - - // 3. if no check id perform check - // 4. validate if can use existing github api data (5mins?) - // 5. inform user what was used: new/exiting check/github api - - return ( - <> - <Heading - title={`${repository.owner} / ${repository.repo}`} - actions={[ - { text: "GitHub Repo", url: repo.html_url, icon: CodeBracketIcon }, - ]} - extras={[ - { icon: StarIcon, text: repo.stargazers_count }, - { icon: LanguageIcon, text: repo.language }, - { - icon: ClockIcon, - text: formatDistance(repo.updated_at, new Date(), { - addSuffix: true, - }), - }, - { icon: TicketIcon, text: repo.open_issues }, - ]} - /> - <Stats - data={[ - { name: "Success", stat: results.summary.success?.length || 0 }, - { name: "Warning", stat: results.summary.warning?.length || 0 }, - { name: "Error", stat: results.summary.error?.length || 0 }, - ]} - /> - <List data={results.checks} /> - </> - ); -} diff --git a/src/utils/checks/index.js b/src/utils/checks/index.js index fcbfcee..42a7822 100644 --- a/src/utils/checks/index.js +++ b/src/utils/checks/index.js @@ -23,3 +23,7 @@ export default function checks(repo) { export function checkSummary(checks) { return Object.groupBy(checks, ({ status }) => status); } + +export function worstCheck(check) { + return check.red > 0 ? "error" : check.amber > 0 ? "warning" : "success"; +}