Show merged PR status when no open PR exists
- extend `GitStatusResult` with nullable `mergedPr` metadata - in server git status, query merged PR only when no open PR is found - in sidebar, display a `Merged` status pill for threads on branches with merged PRs - update server/web/contracts tests for the new status shape
This commit is contained in:
1 parent
7fc53dadc7
commit
46347e2bd2
8 files changed
+152
-8
No files matched your search
@@ -31,7 +31,7 @@ export interface TerminalCommandInput {
|
||||
|
||||
export type TerminalCommandResult = ProcessRunResult;
|
||||
|
||||
export interface GitStatusDetails extends Omit<GitStatusResult, "openPr"> {
|
||||
export interface GitStatusDetails extends Omit<GitStatusResult, "openPr" | "mergedPr"> {
|
||||
upstreamRef: string | null;
|
||||
}
|
||||
|
||||
@@ -227,6 +227,7 @@ export class GitCoreService {
|
||||
aheadCount: details.aheadCount,
|
||||
behindCount: details.behindCount,
|
||||
openPr: null,
|
||||
mergedPr: null,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -169,7 +169,7 @@ describe("GitManager", () => {
|
||||
await runGit(repoDir, ["remote", "add", "origin", remoteDir]);
|
||||
await runGit(repoDir, ["push", "-u", "origin", "feature/status-open-pr"]);
|
||||
|
||||
const { runner } = createRunnerWithFakeGh({
|
||||
const { runner, ghCalls } = createRunnerWithFakeGh({
|
||||
prListSequence: [
|
||||
JSON.stringify([
|
||||
{
|
||||
@@ -197,9 +197,52 @@ describe("GitManager", () => {
|
||||
baseBranch: "main",
|
||||
headBranch: "feature/status-open-pr",
|
||||
});
|
||||
expect(status.mergedPr).toBeNull();
|
||||
expect(ghCalls.some((call) => call.includes("--state merged"))).toBe(false);
|
||||
});
|
||||
|
||||
it("status is resilient to gh lookup failures and returns openPr null", async () => {
|
||||
it("status includes merged PR metadata when there is no open PR", async () => {
|
||||
const repoDir = makeTempDir("t3code-git-manager-");
|
||||
await initRepo(repoDir);
|
||||
await runGit(repoDir, ["checkout", "-b", "feature/status-merged-pr"]);
|
||||
const remoteDir = await createBareRemote();
|
||||
await runGit(repoDir, ["remote", "add", "origin", remoteDir]);
|
||||
await runGit(repoDir, ["push", "-u", "origin", "feature/status-merged-pr"]);
|
||||
|
||||
const { runner, ghCalls } = createRunnerWithFakeGh({
|
||||
prListSequence: [
|
||||
"[]",
|
||||
JSON.stringify([
|
||||
{
|
||||
number: 26,
|
||||
title: "Merged PR",
|
||||
url: "https://github.com/pingdotgg/codething-mvp/pull/26",
|
||||
baseRefName: "main",
|
||||
headRefName: "feature/status-merged-pr",
|
||||
},
|
||||
]),
|
||||
],
|
||||
});
|
||||
|
||||
const manager = new GitManager({
|
||||
runProcess: runner,
|
||||
textGenerator: createTextGenerator(),
|
||||
});
|
||||
|
||||
const status = await manager.status({ cwd: repoDir });
|
||||
expect(status.branch).toBe("feature/status-merged-pr");
|
||||
expect(status.openPr).toBeNull();
|
||||
expect(status.mergedPr).toEqual({
|
||||
number: 26,
|
||||
title: "Merged PR",
|
||||
url: "https://github.com/pingdotgg/codething-mvp/pull/26",
|
||||
baseBranch: "main",
|
||||
headBranch: "feature/status-merged-pr",
|
||||
});
|
||||
expect(ghCalls.some((call) => call.includes("--state merged"))).toBe(true);
|
||||
});
|
||||
|
||||
it("status is resilient to gh lookup failures and returns openPr/mergedPr null", async () => {
|
||||
const repoDir = makeTempDir("t3code-git-manager-");
|
||||
await initRepo(repoDir);
|
||||
await runGit(repoDir, ["checkout", "-b", "feature/status-no-gh"]);
|
||||
@@ -218,6 +261,7 @@ describe("GitManager", () => {
|
||||
const status = await manager.status({ cwd: repoDir });
|
||||
expect(status.branch).toBe("feature/status-no-gh");
|
||||
expect(status.openPr).toBeNull();
|
||||
expect(status.mergedPr).toBeNull();
|
||||
});
|
||||
|
||||
it("creates a commit when working tree is dirty", async () => {
|
||||
|
||||
@@ -192,11 +192,17 @@ export class GitManager {
|
||||
const details = await this.gitCore.statusDetails(input.cwd);
|
||||
|
||||
let openPr: ReturnType<typeof toStatusOpenPr> | null = null;
|
||||
if (details.branch && details.hasUpstream) {
|
||||
let mergedPr: ReturnType<typeof toStatusOpenPr> | null = null;
|
||||
if (details.branch) {
|
||||
try {
|
||||
const existing = await this.findOpenPr(input.cwd, details.branch);
|
||||
if (existing) {
|
||||
openPr = toStatusOpenPr(existing);
|
||||
} else {
|
||||
const merged = await this.findMergedPr(input.cwd, details.branch);
|
||||
if (merged) {
|
||||
mergedPr = toStatusOpenPr(merged);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// PR lookup is best-effort for status rendering.
|
||||
@@ -211,6 +217,7 @@ export class GitManager {
|
||||
aheadCount: details.aheadCount,
|
||||
behindCount: details.behindCount,
|
||||
openPr,
|
||||
mergedPr,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -408,6 +415,34 @@ export class GitManager {
|
||||
return parsed[0] ?? null;
|
||||
}
|
||||
|
||||
private async findMergedPr(cwd: string, branch: string): Promise<OpenPrInfo | null> {
|
||||
const stdout = await this.runGhStdout(cwd, [
|
||||
"pr",
|
||||
"list",
|
||||
"--head",
|
||||
branch,
|
||||
"--state",
|
||||
"merged",
|
||||
"--limit",
|
||||
"1",
|
||||
"--json",
|
||||
"number,title,url,baseRefName,headRefName",
|
||||
]);
|
||||
|
||||
const raw = trimStdout(stdout);
|
||||
if (raw.length === 0) return null;
|
||||
|
||||
let parsedJson: unknown;
|
||||
try {
|
||||
parsedJson = JSON.parse(raw);
|
||||
} catch {
|
||||
throw new Error("GitHub CLI returned invalid PR list JSON.");
|
||||
}
|
||||
|
||||
const parsed = parseOpenPrList(parsedJson);
|
||||
return parsed[0] ?? null;
|
||||
}
|
||||
|
||||
private async resolveBaseBranch(
|
||||
cwd: string,
|
||||
branch: string,
|
||||
|
||||
@@ -564,6 +564,7 @@ describe("WebSocket Server", () => {
|
||||
aheadCount: 0,
|
||||
behindCount: 0,
|
||||
openPr: null,
|
||||
mergedPr: null,
|
||||
}),
|
||||
runStackedAction: vi.fn(),
|
||||
};
|
||||
@@ -593,6 +594,7 @@ describe("WebSocket Server", () => {
|
||||
aheadCount: 0,
|
||||
behindCount: 0,
|
||||
openPr: null,
|
||||
mergedPr: null,
|
||||
});
|
||||
expect(gitManager.status).toHaveBeenCalledWith({ cwd: "/test" });
|
||||
});
|
||||
|
||||
@@ -21,6 +21,7 @@ function status(overrides: Partial<GitStatusResult> = {}): GitStatusResult {
|
||||
aheadCount: 0,
|
||||
behindCount: 0,
|
||||
openPr: null,
|
||||
mergedPr: null,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { MonitorIcon, MoonIcon, SunIcon, TerminalIcon } from "lucide-react";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { useMutation, useQueries, useQueryClient } from "@tanstack/react-query";
|
||||
import type { GitStatusResult } from "@t3tools/contracts";
|
||||
import { isElectron } from "../env";
|
||||
import { useTheme } from "../hooks/useTheme";
|
||||
import { DEFAULT_MODEL } from "../model-logic";
|
||||
@@ -13,7 +14,7 @@ import {
|
||||
type Thread,
|
||||
} from "../types";
|
||||
import { useNativeApi } from "../hooks/useNativeApi";
|
||||
import { gitRemoveWorktreeMutationOptions } from "../lib/gitReactQuery";
|
||||
import { gitRemoveWorktreeMutationOptions, gitStatusQueryOptions } from "../lib/gitReactQuery";
|
||||
import { formatWorktreePathForDisplay, getOrphanedWorktreePathForThread } from "../worktreeCleanup";
|
||||
|
||||
const THEME_CYCLE = { system: "light", light: "dark", dark: "system" } as const;
|
||||
@@ -35,7 +36,7 @@ function inferProjectName(cwd: string): string {
|
||||
}
|
||||
|
||||
interface ThreadStatusPill {
|
||||
label: "Working" | "Connecting" | "Completed" | "Awaiting response";
|
||||
label: "Working" | "Connecting" | "Completed" | "Awaiting response" | "Merged";
|
||||
colorClass: string;
|
||||
dotClass: string;
|
||||
pulse: boolean;
|
||||
@@ -58,7 +59,11 @@ function hasUnseenCompletion(thread: Thread): boolean {
|
||||
return completedAt > lastVisitedAt;
|
||||
}
|
||||
|
||||
function threadStatusPill(thread: Thread, hasPendingApprovals: boolean): ThreadStatusPill | null {
|
||||
function threadStatusPill(
|
||||
thread: Thread,
|
||||
hasPendingApprovals: boolean,
|
||||
hasMergedPr: boolean,
|
||||
): ThreadStatusPill | null {
|
||||
if (hasPendingApprovals) {
|
||||
return {
|
||||
label: "Awaiting response",
|
||||
@@ -86,6 +91,15 @@ function threadStatusPill(thread: Thread, hasPendingApprovals: boolean): ThreadS
|
||||
};
|
||||
}
|
||||
|
||||
if (hasMergedPr) {
|
||||
return {
|
||||
label: "Merged",
|
||||
colorClass: "text-violet-600 dark:text-violet-300/90",
|
||||
dotClass: "bg-violet-500 dark:bg-violet-300/90",
|
||||
pulse: false,
|
||||
};
|
||||
}
|
||||
|
||||
if (hasUnseenCompletion(thread)) {
|
||||
return {
|
||||
label: "Completed",
|
||||
@@ -126,6 +140,42 @@ export default function Sidebar() {
|
||||
}
|
||||
return map;
|
||||
}, [state.threads]);
|
||||
const projectCwdById = useMemo(
|
||||
() => new Map(state.projects.map((project) => [project.id, project.cwd] as const)),
|
||||
[state.projects],
|
||||
);
|
||||
const threadGitTargets = useMemo(
|
||||
() =>
|
||||
state.threads.map((thread) => ({
|
||||
threadId: thread.id,
|
||||
branch: thread.branch,
|
||||
cwd: thread.worktreePath ?? projectCwdById.get(thread.projectId) ?? null,
|
||||
})),
|
||||
[projectCwdById, state.threads],
|
||||
);
|
||||
const threadGitStatusQueries = useQueries({
|
||||
queries: threadGitTargets.map((target) => {
|
||||
const base = gitStatusQueryOptions(api, target.cwd);
|
||||
return {
|
||||
...base,
|
||||
enabled: Boolean(base.enabled && target.branch !== null),
|
||||
staleTime: 30_000,
|
||||
refetchInterval: 60_000,
|
||||
};
|
||||
}),
|
||||
});
|
||||
const mergedPrByThreadId = useMemo(() => {
|
||||
const map = new Map<string, boolean>();
|
||||
for (let index = 0; index < threadGitTargets.length; index += 1) {
|
||||
const target = threadGitTargets[index];
|
||||
if (!target) continue;
|
||||
const status = threadGitStatusQueries[index]?.data as GitStatusResult | undefined;
|
||||
const branchMatches =
|
||||
target.branch !== null && status?.branch !== null && status?.branch === target.branch;
|
||||
map.set(target.threadId, branchMatches && status?.openPr === null && status?.mergedPr !== null);
|
||||
}
|
||||
return map;
|
||||
}, [threadGitStatusQueries, threadGitTargets]);
|
||||
|
||||
const handleNewThread = useCallback(
|
||||
(projectId: string) => {
|
||||
@@ -426,6 +476,7 @@ export default function Sidebar() {
|
||||
const threadStatus = threadStatusPill(
|
||||
thread,
|
||||
pendingApprovalByThreadId.get(thread.id) === true,
|
||||
mergedPrByThreadId.get(thread.id) === true,
|
||||
);
|
||||
const terminalStatus = terminalStatusIndicator(thread);
|
||||
return (
|
||||
|
||||
@@ -37,6 +37,7 @@ describe("git contracts", () => {
|
||||
aheadCount: 0,
|
||||
behindCount: 0,
|
||||
openPr: null,
|
||||
mergedPr: null,
|
||||
});
|
||||
expect(parsed.branch).toBe("feature/git-actions");
|
||||
expect(parsed.hasWorkingTreeChanges).toBe(true);
|
||||
|
||||
@@ -98,6 +98,15 @@ export const gitStatusResultSchema = z.object({
|
||||
headBranch: z.string().min(1),
|
||||
})
|
||||
.nullable(),
|
||||
mergedPr: z
|
||||
.object({
|
||||
number: z.number().int().positive(),
|
||||
title: z.string().min(1),
|
||||
url: z.string().url(),
|
||||
baseBranch: z.string().min(1),
|
||||
headBranch: z.string().min(1),
|
||||
})
|
||||
.nullable(),
|
||||
});
|
||||
|
||||
export const gitStackedActionSchema = z.enum(["commit", "commit_push", "commit_push_pr"]);
|
||||
|
||||
Reference in new issue
Block a user