|
| 1 | +/** |
| 2 | + * Creating a review, and listing them. |
| 3 | + * |
| 4 | + * Creating one is where the commits are pinned, so it fetches first and then |
| 5 | + * resolves the tips from the refs that fetch produced (D-27). A stale pin is |
| 6 | + * the worst failure this app can have: the run completes, the report reads as |
| 7 | + * authoritative, and it describes code the branch has already moved past. A |
| 8 | + * fetch that fails therefore blocks creation rather than falling back. |
| 9 | + */ |
| 10 | + |
| 11 | +import { z } from "zod"; |
| 12 | +import { reviewEffortSchema, reviewProfileSchema } from "@/lib/domain/enums"; |
| 13 | +import { |
| 14 | + listDependencyLinks, |
| 15 | + recordFetch, |
| 16 | + requireProject, |
| 17 | +} from "@/server/db/repositories/projects"; |
| 18 | +import { createReview, listActiveReviews } from "@/server/db/repositories/reviews"; |
| 19 | +import { fetchAll, mergeBase, resolveCommit } from "@/server/gitops/repo"; |
| 20 | +import { created, handler, ok, readJson } from "@/server/api/respond"; |
| 21 | +import { runtime } from "@/server/runtime"; |
| 22 | +import { listProjects } from "@/server/db/repositories/projects"; |
| 23 | +import { listReviewsForProject } from "@/server/db/repositories/reviews"; |
| 24 | + |
| 25 | +export const dynamic = "force-dynamic"; |
| 26 | + |
| 27 | +const body = z.object({ |
| 28 | + projectId: z.string().min(1), |
| 29 | + fromBranch: z.string().min(1), |
| 30 | + intoBranch: z.string().min(1), |
| 31 | + model: z.string().min(1), |
| 32 | + profileId: reviewProfileSchema.default("full-context"), |
| 33 | + effort: reviewEffortSchema.default("high"), |
| 34 | + intent: z.string().optional(), |
| 35 | + linked: z |
| 36 | + .object({ |
| 37 | + projectId: z.string().min(1), |
| 38 | + fromBranch: z.string().min(1), |
| 39 | + intoBranch: z.string().min(1), |
| 40 | + }) |
| 41 | + .optional(), |
| 42 | +}); |
| 43 | + |
| 44 | +export function GET(): Promise<Response> { |
| 45 | + return handler(async () => { |
| 46 | + const { db } = runtime(); |
| 47 | + const reviews = listProjects(db).flatMap((project) => |
| 48 | + listReviewsForProject(db, project.id).map((review) => ({ |
| 49 | + ...review, |
| 50 | + projectName: project.name, |
| 51 | + })), |
| 52 | + ); |
| 53 | + reviews.sort((a, b) => b.createdAt.localeCompare(a.createdAt)); |
| 54 | + return ok({ reviews, active: listActiveReviews(db).map((review) => review.id) }); |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +export function POST(request: Request): Promise<Response> { |
| 59 | + return handler(async () => { |
| 60 | + const { db } = runtime(); |
| 61 | + const input = await readJson(request, body); |
| 62 | + const project = requireProject(db, input.projectId); |
| 63 | + |
| 64 | + // Fetched immediately before pinning, so the commits recorded are the tips |
| 65 | + // the remote has now rather than whatever the last clone left behind. |
| 66 | + await fetchAll(project.clonePath); |
| 67 | + recordFetch(db, project.id); |
| 68 | + const pins = await pin(project.clonePath, input.fromBranch, input.intoBranch); |
| 69 | + |
| 70 | + let linked; |
| 71 | + if (input.linked) { |
| 72 | + const dependency = requireProject(db, input.linked.projectId); |
| 73 | + const isLinked = listDependencyLinks(db, project.id).some( |
| 74 | + (link) => link.dependencyProjectId === dependency.id, |
| 75 | + ); |
| 76 | + if (!isLinked) { |
| 77 | + throw Response.json( |
| 78 | + { |
| 79 | + error: `${dependency.name} is not a dependency of ${project.name}.`, |
| 80 | + code: "NotLinked", |
| 81 | + }, |
| 82 | + { status: 400 }, |
| 83 | + ); |
| 84 | + } |
| 85 | + // Both sides fetched before either is pinned, so the pair describes one |
| 86 | + // moment rather than two. |
| 87 | + await fetchAll(dependency.clonePath); |
| 88 | + recordFetch(db, dependency.id); |
| 89 | + linked = { |
| 90 | + ...input.linked, |
| 91 | + projectId: dependency.id, |
| 92 | + ...(await pin(dependency.clonePath, input.linked.fromBranch, input.linked.intoBranch)), |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + const review = createReview(db, { |
| 97 | + projectId: project.id, |
| 98 | + fromBranch: input.fromBranch, |
| 99 | + intoBranch: input.intoBranch, |
| 100 | + ...pins, |
| 101 | + model: input.model, |
| 102 | + profileId: input.profileId, |
| 103 | + engineMode: "headless", |
| 104 | + effort: input.effort, |
| 105 | + ...(input.intent === undefined ? {} : { intent: input.intent }), |
| 106 | + ...(linked === undefined ? {} : { linked }), |
| 107 | + }); |
| 108 | + |
| 109 | + return created({ review }); |
| 110 | + }); |
| 111 | +} |
| 112 | + |
| 113 | +async function pin(repoDir: string, fromBranch: string, intoBranch: string) { |
| 114 | + return { |
| 115 | + fromCommit: await resolveCommit(repoDir, fromBranch), |
| 116 | + intoCommit: await resolveCommit(repoDir, intoBranch), |
| 117 | + mergeBaseCommit: await mergeBase(repoDir, intoBranch, fromBranch), |
| 118 | + }; |
| 119 | +} |
0 commit comments