@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 75 lines 2.75 KB
Raw
1 import {
2 type GetProjectBranchesResponse,
3 getProjectBranches,
4 } from "@distilled.cloud/prisma/management";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import { PrismaPaginationError } from "./Pagination.ts";
8 import { isPrismaDevId } from "./Refs.ts";
9
10 export class BranchesConflict extends Data.TaggedError("BranchesConflict")<{
11 readonly message: string;
12 readonly cause?: unknown;
13 }> {}
14
15 // Distilled emits the cursor-paginated list operations as plain ops, so
16 // callers walk `pagination` themselves (see `src/Neon/Project.ts`).
17 const listBranches = (projectId: string, gitName?: string) =>
18 Effect.gen(function* () {
19 const branches: GetProjectBranchesResponse["data"][number][] = [];
20 let cursor: string | undefined;
21 while (true) {
22 const page = yield* getProjectBranches({
23 projectId,
24 limit: 100,
25 ...(gitName === undefined ? {} : { gitName }),
26 ...(cursor === undefined ? {} : { cursor }),
27 });
28 branches.push(...page.data);
29 const nextCursor = page.pagination.nextCursor;
30 if (!page.pagination.hasMore) break;
31 if (nextCursor === null) {
32 return yield* new PrismaPaginationError({
33 message:
34 "Invalid Prisma Management API pagination response from getProjectBranches: hasMore was true without a non-empty nextCursor",
35 });
36 }
37 cursor = nextCursor;
38 }
39 return branches;
40 });
41
42 /**
43 * Resolve the branch a resource targets: `branchId` as given, `branchGitName`
44 * by lookup, or the project's default branch when both are omitted.
45 */
46 export const desiredBranchId = Effect.fn(function* (
47 projectId: string,
48 props: { branchId?: string; branchGitName?: string },
49 ) {
50 if (props.branchId !== undefined && !isPrismaDevId(props.branchId)) {
51 return { resolved: true as const, id: props.branchId };
52 }
53 if (props.branchGitName !== undefined) {
54 const branches = yield* listBranches(projectId, props.branchGitName);
55 if (branches.length > 1) {
56 return yield* new BranchesConflict({
57 message: `Prisma returned multiple branches named '${props.branchGitName}' in project '${projectId}'; refusing an ambiguous match.`,
58 });
59 }
60 return branches[0]
61 ? { resolved: true as const, id: branches[0].id }
62 : { resolved: false as const };
63 }
64 const branches = yield* listBranches(projectId);
65 const defaults = branches.filter((branch) => branch.isDefault);
66 if (defaults.length > 1) {
67 return yield* new BranchesConflict({
68 message: `Prisma returned multiple default branches for project '${projectId}'; refusing an ambiguous match.`,
69 });
70 }
71 const defaultBranch = defaults[0];
72 return defaultBranch
73 ? { resolved: true as const, id: defaultBranch.id }
74 : { resolved: false as const };
75 });