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

Commit

Permalink
feat: my repos to report workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiejaoude committed Jul 19, 2024
1 parent 93482bb commit 174bce1
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 109 deletions.
5 changes: 3 additions & 2 deletions src/app/account/repo/checks/[id]/action.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use server";

import { redirect } from "next/navigation";
import { getServerSession } from "next-auth/next";
import { differenceInHours } from "date-fns";

Expand Down Expand Up @@ -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 },
Expand All @@ -75,5 +76,5 @@ export async function performChecks(formData) {
},
});

// redirect
redirect(`/account/repo/report/${check.id}`);
}
40 changes: 8 additions & 32 deletions src/app/account/repo/checks/[id]/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,18 +26,20 @@ export default async function Page({ params }) {
},
},
});
console.log("======", repository);

return (
<>
<Title text="Check list">
<Title
text={`Check list for the repo: ${repository.owner} / ${repository.repo}`}
>
<Form id={id} />
</Title>
<List
data={repository.checks.map((check) => ({
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,
})}`,
Expand Down
30 changes: 26 additions & 4 deletions src/app/account/repo/list/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 (
<>
<Title text="Repo list" />
Expand All @@ -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",
}))}
/>
</>
Expand Down
98 changes: 98 additions & 0 deletions src/app/account/repo/report/[id]/page.js
Original file line number Diff line number Diff line change
@@ -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,
}))}
/>
</>
);
}
71 changes: 0 additions & 71 deletions src/app/account/repo/status/[...repo]/page.js

This file was deleted.

4 changes: 4 additions & 0 deletions src/utils/checks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

0 comments on commit 174bce1

Please sign in to comment.