Skip to content

Commit 3d35868

Browse files
Add pull-request thread setup for local and worktree modes
- Add git APIs to resolve PR refs and prepare PR thread workspaces - Support fetching PR head refs into local branches and reusing existing worktrees - Wire new websocket/contracts/web composer flows for PR-based thread creation - Expand server/web/contracts tests for PR routing and branch/worktree behavior
1 parent 767de27 commit 3d35868

27 files changed

Lines changed: 1545 additions & 75 deletions

‎apps/server/src/git/Layers/GitCore.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ const makeIsolatedGitCore = (gitService: GitServiceShape) =>
105105
readConfigValue: (cwd, key) => core.readConfigValue(cwd, key),
106106
listBranches: (input) => core.listBranches(input),
107107
createWorktree: (input) => core.createWorktree(input),
108+
fetchPullRequestBranch: (input) => core.fetchPullRequestBranch(input),
108109
removeWorktree: (input) => core.removeWorktree(input),
109110
renameBranch: (input) => core.renameBranch(input),
110111
createBranch: (input) => core.createBranch(input),
@@ -149,6 +150,15 @@ function createGitWorktree(input: Parameters<GitCoreShape["createWorktree"]>[0])
149150
});
150151
}
151152

153+
function fetchGitPullRequestBranch(
154+
input: Parameters<GitCoreShape["fetchPullRequestBranch"]>[0],
155+
) {
156+
return Effect.gen(function* () {
157+
const core = yield* GitCore;
158+
return yield* core.fetchPullRequestBranch(input);
159+
});
160+
}
161+
152162
function removeGitWorktree(input: Parameters<GitCoreShape["removeWorktree"]>[0]) {
153163
return Effect.gen(function* () {
154164
const core = yield* GitCore;
@@ -1006,6 +1016,28 @@ it.layer(TestLayer)("git integration", (it) => {
10061016
}),
10071017
);
10081018

1019+
it.effect("creates a worktree for an existing branch when newBranch is omitted", () =>
1020+
Effect.gen(function* () {
1021+
const tmp = yield* makeTmpDir();
1022+
yield* initRepoWithCommit(tmp);
1023+
yield* createGitBranch({ cwd: tmp, branch: "feature/existing-worktree" });
1024+
1025+
const wtPath = path.join(tmp, "wt-existing");
1026+
const result = yield* createGitWorktree({
1027+
cwd: tmp,
1028+
branch: "feature/existing-worktree",
1029+
path: wtPath,
1030+
});
1031+
1032+
expect(result.worktree.path).toBe(wtPath);
1033+
expect(result.worktree.branch).toBe("feature/existing-worktree");
1034+
const branchOutput = yield* git(wtPath, ["branch", "--show-current"]);
1035+
expect(branchOutput).toBe("feature/existing-worktree");
1036+
1037+
yield* removeGitWorktree({ cwd: tmp, path: wtPath });
1038+
}),
1039+
);
1040+
10091041
it.effect("throws when new branch name already exists", () =>
10101042
Effect.gen(function* () {
10111043
const tmp = yield* makeTmpDir();
@@ -1168,6 +1200,37 @@ it.layer(TestLayer)("git integration", (it) => {
11681200
);
11691201
});
11701202

1203+
describe("fetchPullRequestBranch", () => {
1204+
it.effect("fetches a GitHub pull request ref into a local branch without checkout", () =>
1205+
Effect.gen(function* () {
1206+
const tmp = yield* makeTmpDir();
1207+
yield* initRepoWithCommit(tmp);
1208+
const remoteDir = yield* makeTmpDir("git-remote-");
1209+
yield* git(remoteDir, ["init", "--bare"]);
1210+
yield* git(tmp, ["remote", "add", "origin", remoteDir]);
1211+
yield* git(tmp, ["push", "-u", "origin", "main"]);
1212+
yield* git(tmp, ["checkout", "-b", "feature/pr-fetch"]);
1213+
yield* writeTextFile(path.join(tmp, "pr-fetch.txt"), "fetch me\n");
1214+
yield* git(tmp, ["add", "pr-fetch.txt"]);
1215+
yield* git(tmp, ["commit", "-m", "Add PR fetch branch"]);
1216+
yield* git(tmp, ["push", "-u", "origin", "feature/pr-fetch"]);
1217+
yield* git(tmp, ["push", "origin", "HEAD:refs/pull/55/head"]);
1218+
yield* git(tmp, ["checkout", "main"]);
1219+
1220+
yield* fetchGitPullRequestBranch({
1221+
cwd: tmp,
1222+
prNumber: 55,
1223+
branch: "feature/pr-fetch",
1224+
});
1225+
1226+
const localBranches = yield* git(tmp, ["branch", "--list", "feature/pr-fetch"]);
1227+
expect(localBranches).toContain("feature/pr-fetch");
1228+
const currentBranch = yield* git(tmp, ["branch", "--show-current"]);
1229+
expect(currentBranch).toBe("main");
1230+
}),
1231+
);
1232+
});
1233+
11711234
// ── Full flow: thread switching simulation ──
11721235

11731236
describe("full flow: thread switching (checkout toggling)", () => {

‎apps/server/src/git/Layers/GitCore.ts‎

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,29 @@ const makeGitCore = Effect.gen(function* () {
418418
allowNonZeroExit: true,
419419
}).pipe(Effect.map((result) => result.code === 0));
420420

421+
const listRemoteNames = (cwd: string): Effect.Effect<ReadonlyArray<string>, GitCommandError> =>
422+
runGitStdout("GitCore.listRemoteNames", cwd, ["remote"]).pipe(
423+
Effect.map((stdout) => parseRemoteNames(stdout).toReversed()),
424+
);
425+
426+
const resolvePrimaryRemoteName = (cwd: string): Effect.Effect<string, GitCommandError> =>
427+
Effect.gen(function* () {
428+
if (yield* originRemoteExists(cwd)) {
429+
return "origin";
430+
}
431+
const remotes = yield* listRemoteNames(cwd);
432+
const [firstRemote] = remotes;
433+
if (firstRemote) {
434+
return firstRemote;
435+
}
436+
return yield* createGitCommandError(
437+
"GitCore.resolvePrimaryRemoteName",
438+
cwd,
439+
["remote"],
440+
"No git remote is configured for this repository.",
441+
);
442+
});
443+
421444
const resolveBaseBranchForNoUpstream = (
422445
cwd: string,
423446
branch: string,
@@ -1015,16 +1038,20 @@ const makeGitCore = Effect.gen(function* () {
10151038

10161039
const createWorktree: GitCoreShape["createWorktree"] = (input) =>
10171040
Effect.gen(function* () {
1018-
const sanitizedBranch = input.newBranch.replace(/\//g, "-");
1041+
const targetBranch = input.newBranch ?? input.branch;
1042+
const sanitizedBranch = targetBranch.replace(/\//g, "-");
10191043
const repoName = path.basename(input.cwd);
10201044
const homeDir = process.env.HOME ?? process.env.USERPROFILE ?? "/tmp";
10211045
const worktreePath =
10221046
input.path ?? path.join(homeDir, ".t3", "worktrees", repoName, sanitizedBranch);
1047+
const args = input.newBranch
1048+
? ["worktree", "add", "-b", input.newBranch, worktreePath, input.branch]
1049+
: ["worktree", "add", worktreePath, input.branch];
10231050

10241051
yield* executeGit(
10251052
"GitCore.createWorktree",
10261053
input.cwd,
1027-
["worktree", "add", "-b", input.newBranch, worktreePath, input.branch],
1054+
args,
10281055
{
10291056
fallbackErrorMessage: "git worktree add failed",
10301057
},
@@ -1033,11 +1060,30 @@ const makeGitCore = Effect.gen(function* () {
10331060
return {
10341061
worktree: {
10351062
path: worktreePath,
1036-
branch: input.newBranch,
1063+
branch: targetBranch,
10371064
},
10381065
};
10391066
});
10401067

1068+
const fetchPullRequestBranch: GitCoreShape["fetchPullRequestBranch"] = (input) =>
1069+
Effect.gen(function* () {
1070+
const remoteName = yield* resolvePrimaryRemoteName(input.cwd);
1071+
yield* executeGit(
1072+
"GitCore.fetchPullRequestBranch",
1073+
input.cwd,
1074+
[
1075+
"fetch",
1076+
"--quiet",
1077+
"--no-tags",
1078+
remoteName,
1079+
`+refs/pull/${input.prNumber}/head:refs/heads/${input.branch}`,
1080+
],
1081+
{
1082+
fallbackErrorMessage: "git fetch pull request branch failed",
1083+
},
1084+
);
1085+
}).pipe(Effect.asVoid);
1086+
10411087
const removeWorktree: GitCoreShape["removeWorktree"] = (input) =>
10421088
Effect.gen(function* () {
10431089
const args = ["worktree", "remove"];
@@ -1197,6 +1243,7 @@ const makeGitCore = Effect.gen(function* () {
11971243
readConfigValue,
11981244
listBranches,
11991245
createWorktree,
1246+
fetchPullRequestBranch,
12001247
removeWorktree,
12011248
renameBranch,
12021249
createBranch,
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { Effect } from "effect";
3+
4+
vi.mock("../../processRunner", () => ({
5+
runProcess: vi.fn(),
6+
}));
7+
8+
import { runProcess } from "../../processRunner";
9+
import { GitHubCli } from "../Services/GitHubCli.ts";
10+
import { GitHubCliLive } from "./GitHubCli.ts";
11+
12+
const mockedRunProcess = vi.mocked(runProcess);
13+
14+
describe("GitHubCliLive", () => {
15+
it("parses pull request view output", async () => {
16+
mockedRunProcess.mockResolvedValueOnce({
17+
stdout: JSON.stringify({
18+
number: 42,
19+
title: "Add PR thread creation",
20+
url: "https://github.com/pingdotgg/codething-mvp/pull/42",
21+
baseRefName: "main",
22+
headRefName: "feature/pr-threads",
23+
state: "OPEN",
24+
mergedAt: null,
25+
}),
26+
stderr: "",
27+
code: 0,
28+
signal: null,
29+
timedOut: false,
30+
});
31+
32+
const result = await Effect.runPromise(
33+
Effect.gen(function* () {
34+
const gh = yield* GitHubCli;
35+
return yield* gh.getPullRequest({
36+
cwd: "/repo",
37+
reference: "#42",
38+
});
39+
}).pipe(Effect.provide(GitHubCliLive)),
40+
);
41+
42+
expect(result).toEqual({
43+
number: 42,
44+
title: "Add PR thread creation",
45+
url: "https://github.com/pingdotgg/codething-mvp/pull/42",
46+
baseRefName: "main",
47+
headRefName: "feature/pr-threads",
48+
state: "open",
49+
});
50+
expect(mockedRunProcess).toHaveBeenCalledWith(
51+
"gh",
52+
["pr", "view", "#42", "--json", "number,title,url,baseRefName,headRefName,state,mergedAt"],
53+
expect.objectContaining({ cwd: "/repo" }),
54+
);
55+
});
56+
57+
it("surfaces a friendly error when the pull request is not found", async () => {
58+
mockedRunProcess.mockRejectedValueOnce(
59+
new Error(
60+
"GraphQL: Could not resolve to a PullRequest with the number of 4888. (repository.pullRequest)",
61+
),
62+
);
63+
64+
const error = await Effect.runPromise(
65+
Effect.gen(function* () {
66+
const gh = yield* GitHubCli;
67+
return yield* gh.getPullRequest({
68+
cwd: "/repo",
69+
reference: "4888",
70+
});
71+
}).pipe(Effect.provide(GitHubCliLive), Effect.flip),
72+
);
73+
74+
expect(error.message).toContain("Pull request not found");
75+
});
76+
});

0 commit comments

Comments
 (0)