Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion apps/server/src/git/Layers/GitHubCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ function normalizeRepositoryCloneUrls(
function decodeGitHubJson<S extends Schema.Top>(
raw: string,
schema: S,
operation: "listOpenPullRequests" | "getPullRequest" | "getRepositoryCloneUrls",
operation:
| "listOpenPullRequests"
| "getPullRequest"
| "getRepositoryCloneUrls"
| "listAllPullRequests",
invalidDetail: string,
): Effect.Effect<S["Type"], GitHubCliError, S["DecodingServices"]> {
return Schema.decodeEffect(Schema.fromJsonString(schema))(raw).pipe(
Expand Down Expand Up @@ -203,6 +207,48 @@ const makeGitHubCli = Effect.sync(() => {
),
Effect.map((pullRequests) => pullRequests.map(normalizePullRequestSummary)),
),
listAllPullRequests: (input) =>
execute({
cwd: input.cwd,
args: [
"pr",
"list",
"--state",
input.state ?? "open",
"--limit",
String(input.limit ?? 50),
...(input.label ? ["--label", input.label] : []),
"--json",
"number,title,url,baseRefName,headRefName,state,labels,updatedAt,author",
],
}).pipe(
Effect.map((result) => result.stdout.trim()),
Effect.flatMap((raw) => {
if (raw.length === 0) return Effect.succeed([]);
return Effect.try({
try: () => {
const parsed = JSON.parse(raw) as Array<any>;
return parsed.map((pr: any) => ({
number: pr.number,
title: pr.title,
url: pr.url,
baseRefName: pr.baseRefName,
headRefName: pr.headRefName,
state: pr.state === "MERGED" ? "merged" : pr.state === "CLOSED" ? "closed" : "open",
labels: (pr.labels ?? []).map((l: any) => ({ name: l.name, color: l.color ?? "" })),
updatedAt: pr.updatedAt ?? "",
author: pr.author?.login ?? "",
}));
},
catch: (cause) =>
new GitHubCliError({
operation: "listAllPullRequests",
detail: "GitHub CLI returned invalid PR list JSON.",
cause,
}),
});
}),
),
getPullRequest: (input) =>
execute({
cwd: input.cwd,
Expand Down
29 changes: 29 additions & 0 deletions apps/server/src/git/Layers/GitManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,35 @@ function createGitHubCliWithFakeGh(scenario: FakeGhScenario = {}): {
(result) => JSON.parse(result.stdout) as ReadonlyArray<GitHubPullRequestSummary>,
),
),
listAllPullRequests: (input) =>
execute({
cwd: input.cwd,
args: [
"pr",
"list",
"--state",
input.state ?? "open",
"--limit",
String(input.limit ?? 50),
"--json",
"number,title,url,baseRefName,headRefName,state,labels,updatedAt,author",
],
}).pipe(
Effect.map((result) => {
const parsed = JSON.parse(result.stdout) as Array<any>;
return parsed.map((pr: any) => ({
number: pr.number,
title: pr.title,
url: pr.url,
baseRefName: pr.baseRefName,
headRefName: pr.headRefName,
state: pr.state === "MERGED" ? "merged" : pr.state === "CLOSED" ? "closed" : "open",
labels: (pr.labels ?? []).map((l: any) => ({ name: l.name, color: l.color ?? "" })),
updatedAt: pr.updatedAt ?? "",
author: pr.author?.login ?? "",
}));
}),
),
createPullRequest: (input) =>
execute({
cwd: input.cwd,
Expand Down
26 changes: 26 additions & 0 deletions apps/server/src/git/Layers/GitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1273,11 +1273,37 @@ export const makeGitManager = Effect.gen(function* () {
},
);

const listPullRequests: GitManagerShape["listPullRequests"] = Effect.fnUntraced(
function* (input) {
const results = yield* gitHubCli.listAllPullRequests({
cwd: input.cwd,
...(input.state !== undefined ? { state: input.state } : {}),
...(input.label !== undefined ? { label: input.label } : {}),
...(input.limit !== undefined ? { limit: input.limit } : {}),
});

return {
pullRequests: results.map((pr) => ({
number: pr.number,
title: pr.title,
url: pr.url,
baseBranch: pr.baseRefName,
headBranch: pr.headRefName,
state: pr.state as "open" | "closed" | "merged",
labels: pr.labels,
updatedAt: pr.updatedAt,
author: pr.author,
})),
};
},
);

return {
status,
resolvePullRequest,
preparePullRequestThread,
runStackedAction,
listPullRequests,
} satisfies GitManagerShape;
});

Expand Down
19 changes: 19 additions & 0 deletions apps/server/src/git/Services/GitHubCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ export interface GitHubCliShape {
readonly limit?: number;
}) => Effect.Effect<ReadonlyArray<GitHubPullRequestSummary>, GitHubCliError>;

/**
* List all pull requests for the repo, optionally filtered by state and label.
*/
readonly listAllPullRequests: (input: {
readonly cwd: string;
readonly state?: "open" | "closed" | "merged";
readonly label?: string;
readonly limit?: number;
}) => Effect.Effect<
ReadonlyArray<
GitHubPullRequestSummary & {
labels: Array<{ name: string; color: string }>;
updatedAt: string;
author: string;
}
>,
GitHubCliError
>;

/**
* Resolve a pull request by URL, number, or branch-ish identifier.
*/
Expand Down
9 changes: 9 additions & 0 deletions apps/server/src/git/Services/GitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
import {
GitActionProgressEvent,
GitListPullRequestsInput,
GitListPullRequestsResult,
GitPreparePullRequestThreadInput,
GitPreparePullRequestThreadResult,
GitPullRequestRefInput,
Expand Down Expand Up @@ -55,6 +57,13 @@ export interface GitManagerShape {
input: GitPreparePullRequestThreadInput,
) => Effect.Effect<GitPreparePullRequestThreadResult, GitManagerServiceError>;

/**
* List pull requests for the repository, optionally filtered by state/label.
*/
readonly listPullRequests: (
input: GitListPullRequestsInput,
) => Effect.Effect<GitListPullRequestsResult, GitManagerServiceError>;

/**
* Run a stacked Git action (`commit`, `commit_push`, `commit_push_pr`).
* When `featureBranch` is set, creates and checks out a feature branch first.
Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/wsServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,7 @@ describe("WebSocket Server", () => {
resolvePullRequest,
preparePullRequestThread,
runStackedAction,
listPullRequests: vi.fn(() => Effect.succeed({ pullRequests: [] })),
};

server = await createTestServer({ cwd: "/test", gitManager });
Expand Down Expand Up @@ -1796,6 +1797,7 @@ describe("WebSocket Server", () => {
resolvePullRequest: vi.fn(() => Effect.succeed(resolvePullRequestResult)),
preparePullRequestThread: vi.fn(() => Effect.succeed(preparePullRequestThreadResult)),
runStackedAction: vi.fn(() => Effect.void as any),
listPullRequests: vi.fn(() => Effect.succeed({ pullRequests: [] })),
};

server = await createTestServer({ cwd: "/test", gitManager });
Expand Down Expand Up @@ -1844,6 +1846,7 @@ describe("WebSocket Server", () => {
resolvePullRequest: vi.fn(() => Effect.void as any),
preparePullRequestThread: vi.fn(() => Effect.void as any),
runStackedAction,
listPullRequests: vi.fn(() => Effect.succeed({ pullRequests: [] })),
};

server = await createTestServer({ cwd: "/test", gitManager });
Expand Down Expand Up @@ -1906,6 +1909,7 @@ describe("WebSocket Server", () => {
resolvePullRequest: vi.fn(() => Effect.void as any),
preparePullRequestThread: vi.fn(() => Effect.void as any),
runStackedAction,
listPullRequests: vi.fn(() => Effect.succeed({ pullRequests: [] })),
};

server = await createTestServer({ cwd: "/test", gitManager });
Expand Down
5 changes: 5 additions & 0 deletions apps/server/src/wsServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,11 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
return yield* gitManager.preparePullRequestThread(body);
}

case WS_METHODS.gitListPullRequests: {
const body = stripRequestTag(request.body);
return yield* gitManager.listPullRequests(body);
}

case WS_METHODS.gitListBranches: {
const body = stripRequestTag(request.body);
return yield* git.listBranches(body);
Expand Down
32 changes: 32 additions & 0 deletions apps/web/src/lib/gitReactQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const gitQueryKeys = {
all: ["git"] as const,
status: (cwd: string | null) => ["git", "status", cwd] as const,
branches: (cwd: string | null) => ["git", "branches", cwd] as const,
pullRequests: (cwd: string | null) => ["git", "pull-requests", cwd] as const,
};

export const gitMutationKeys = {
Expand Down Expand Up @@ -78,6 +79,37 @@ export function gitResolvePullRequestQueryOptions(input: {
});
}

export function gitListPullRequestsQueryOptions(input: {
cwd: string | null;
state?: "open" | "closed" | "merged";
label?: string;
}) {
return queryOptions({
queryKey: [
"git",
"pull-requests",
input.cwd,
input.state ?? "open",
input.label ?? "",
] as const,
queryFn: async () => {
const api = ensureNativeApi();
if (!input.cwd) throw new Error("Pull request listing is unavailable.");
return api.git.listPullRequests({
cwd: input.cwd,
...(input.state ? { state: input.state } : {}),
...(input.label ? { label: input.label } : {}),
limit: 100,
});
},
enabled: input.cwd !== null,
staleTime: 15_000,
refetchOnWindowFocus: true,
refetchOnReconnect: true,
refetchInterval: 30_000,
});
}

export function gitInitMutationOptions(input: { cwd: string | null; queryClient: QueryClient }) {
return mutationOptions({
mutationKey: gitMutationKeys.init(input.cwd),
Expand Down
Loading
Loading