Skip to content

Commit

Permalink
chore: daily development
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Dec 17, 2024
1 parent 428e369 commit 62f242e
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 96 deletions.
41 changes: 41 additions & 0 deletions packages/canyon-platform/app/api/project/[id]/chart/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import prisma from "@/lib/prisma";

import { NextRequest } from "next/server";
import { percent } from "canyon-data";

export async function GET(request: NextRequest) {
const { pathname } = request.nextUrl;
const id = pathname.split("/")[3];

const coverages = await prisma.coverage
.findMany({
where: {
projectID: id,
covType: "all",
},
select: {
sha: true,
projectID: true,
statementsCovered: true,
statementsTotal: true,
updatedAt: true,
branch: true,
},
orderBy: {
updatedAt: "desc",
},
})
.then((r) => {
return r.map((item) => {
return {
sha: item.sha,
projectID: item.projectID,
statements: percent(item.statementsCovered, item.statementsTotal),
branch: item.branch,
lastReportTime: item.updatedAt,
};
});
});

return Response.json(coverages);
}
69 changes: 69 additions & 0 deletions packages/canyon-platform/app/api/project/[id]/compartment/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import prisma from "@/lib/prisma";

import { NextRequest } from "next/server";
import { percent } from "canyon-data";
import dayjs from "dayjs";

export async function GET(request: NextRequest) {
const { pathname } = request.nextUrl;

const id = pathname.split("/")[3];

const coverages = await prisma.coverage
.findMany({
where: {
projectID: id,
},
select: {
sha: true,
projectID: true,
statementsCovered: true,
statementsTotal: true,
updatedAt: true,
branch: true,
covType: true,
},
orderBy: {
updatedAt: "desc",
},
})
.then((r) => {
return r.map((item) => {
return {
sha: item.sha,
projectID: item.projectID,
statements: percent(item.statementsCovered, item.statementsTotal),
branch: item.branch,
lastReportTime: item.updatedAt,
covType: item.covType,
};
});
});

const maxCoverage = Math.max(
...coverages
.filter((item) => item.covType === "all")
.map((item) => item.statements),
);
const totalTimes = coverages.filter((item) => item.covType === "all").length;

const latest = coverages.filter((item) => item.covType === "agg")[0];
return Response.json([
{
label: "projects.total_times",
value: String(totalTimes),
},
{
label: "projects.max_coverage",
value: maxCoverage + "%",
},
{
label: "projects.latest_report_time",
value: dayjs(latest.lastReportTime).format("MM-DD HH:mm"),
},
{
label: "projects.latest_report_coverage",
value: latest.statements + "%",
},
]);
}
92 changes: 81 additions & 11 deletions packages/canyon-platform/app/api/project/[id]/record/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,61 @@ import prisma from "@/lib/prisma";

import { NextRequest } from "next/server";
import { percent } from "canyon-data";
import axios from "axios";

async function getCommits(projectID: string, shas: string[]) {
const [provider, id, slug] = projectID.split("-");
const gitProvider = await prisma.gitProvider.findFirst({
where: {
id: provider,
},
});

if (gitProvider) {
// TODO: 从gitlab获取文件内容,还去要判断provider type,例如github。
// 判断provider类型,例如gitlab、github、gitea

switch (gitProvider.type) {
case "gitlab":
const { url: gitlabUrl, privateToken: gitlabPrivateToken } =
gitProvider;
return Promise.all(
shas.map((sha) =>
axios
.get(
`${gitlabUrl}/api/v4/projects/${id}/repository/commits/${sha}`,
{
params: {
ref: sha,
},
headers: {
"private-token": gitlabPrivateToken,
},
},
)
.then(({ data }) => data),
),
);
case "github":
return [];
case "gitea":
return [];
default:
return [];
}
} else {
return [];
}
}

export async function GET(request: NextRequest) {
const { pathname } = request.nextUrl;
// const provider = pathname.split("/")[2];
const id = pathname.split("/")[3];
console.log("id", id);
// const slug = pathname.split("/")[4];
// const projectID = `${provider}-${id}-${slug}`;

const coverages = await prisma.coverage
.findMany({
where: {
projectID: id,
covType: "all",
},
select: {
sha: true,
Expand All @@ -23,6 +65,8 @@ export async function GET(request: NextRequest) {
statementsTotal: true,
updatedAt: true,
branch: true,
covType: true,
reportID: true,
},
orderBy: {
updatedAt: "desc",
Expand All @@ -31,16 +75,42 @@ export async function GET(request: NextRequest) {
.then((r) => {
return r.map((item) => {
return {
sha: item.sha,
projectID: item.projectID,
times: 0,
...item,
statements: percent(item.statementsCovered, item.statementsTotal),
message: "message",
branch: item.branch,
lastReportTime: item.updatedAt,
// message: "message",
};
});
});

return Response.json(coverages);
const rows = coverages
.filter((item) => item.covType === "all")
.map((item) => ({
...item,
aggs: [],
}));

// 获取gitlab commit message

const shas = await getCommits(
id,
rows.map((item) => item.sha),
);

const aggs = coverages.filter((item) => item.covType === "agg");

for (let i = 0; i < aggs.length; i++) {
const row = rows.find((item) => item.sha === aggs[i].sha);
if (row) {
row.aggs.push(aggs[i]);
}
}

return Response.json(
rows.map((item) => ({
...item,
times: item.aggs.length,
message: shas.find((sha) => sha.id === item.sha)?.message,
})),
);
}
12 changes: 3 additions & 9 deletions packages/canyon-platform/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
// import Image from "next/image";
import { Button } from "antd";

export default function Home() {
return (
<div>
<Button>按我</Button>
</div>
);
import { redirect } from "next/navigation";
export default function Page() {
redirect("/projects");
}
Loading

0 comments on commit 62f242e

Please sign in to comment.