fix(server): link thread PRs without an open client (#10101)

Co-authored-by: Claude Fable 5.1 <[email protected]>
This commit is contained in:
Theo BrowneandClaude Fable 5.1 authored and GitHub committed 2026-09-06 04:06:15 -07:00
1 parent 5fa35d2116
commit 223ff4490f
50 files changed
+2308 -1485

No files matched your search

+228 -7
View File
@@ -13,6 +13,7 @@ import * as Logger from "effect/Logger";
import * as Option from "effect/Option";
import * as PlatformError from "effect/PlatformError";
import * as References from "effect/References";
import * as Schema from "effect/Schema";
import * as Scope from "effect/Scope";
import { ChildProcessSpawner } from "effect/unstable/process";
import { expect } from "vite-plus/test";
@@ -30,10 +31,13 @@ import {
TextGenerationError,
} from "@t3tools/contracts";
import * as GitHubCli from "../sourceControl/GitHubCli.ts";
import * as GitLabCli from "../sourceControl/GitLabCli.ts";
import * as TextGeneration from "../textGeneration/TextGeneration.ts";
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
import * as VcsProcess from "../vcs/VcsProcess.ts";
import * as GitHubSourceControlProvider from "../sourceControl/GitHubSourceControlProvider.ts";
import * as GitLabSourceControlProvider from "../sourceControl/GitLabSourceControlProvider.ts";
import type { SourceControlProvider } from "../sourceControl/SourceControlProvider.ts";
import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
import * as ServerConfig from "../config.ts";
import * as ProjectSetupScriptRunner from "../project/ProjectSetupScriptRunner.ts";
@@ -41,6 +45,8 @@ import * as ProviderRegistry from "../provider/Services/ProviderRegistry.ts";
import * as ServerSettings from "../serverSettings.ts";
import * as GitManager from "./GitManager.ts";
const encodeCliJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
interface FakeGhScenario {
prListSequence?: string[];
prListByHeadSelector?: Record<string, string>;
@@ -620,6 +626,7 @@ function preparePullRequestThread(
function makeManager(input?: {
ghScenario?: FakeGhScenario;
sourceControlProvider?: SourceControlProvider["Service"];
textGeneration?: Partial<FakeGitTextGeneration>;
serverSettings?: Parameters<typeof ServerSettings.layerTest>[0];
setupScriptRunner?: ProjectSetupScriptRunner.ProjectSetupScriptRunner["Service"];
@@ -659,7 +666,10 @@ function makeManager(input?: {
);
const sourceControlRegistryLayer = Layer.effect(
SourceControlProviderRegistry.SourceControlProviderRegistry,
GitHubSourceControlProvider.make.pipe(
(input?.sourceControlProvider === undefined
? GitHubSourceControlProvider.make
: Effect.succeed(input.sourceControlProvider)
).pipe(
Effect.map((provider) =>
SourceControlProviderRegistry.SourceControlProviderRegistry.of({
get: () => Effect.succeed(provider),
@@ -1146,7 +1156,12 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
branch: "feature/saved-branch",
});
expect(pullRequest).toEqual({
expect(pullRequest).toMatchObject({
number: 216,
title: "Saved branch PR",
url: "https://github.com/pingdotgg/t3code/pull/216",
baseRef: "main",
headRef: "feature/saved-branch",
state: "open",
closedAt: null,
mergedAt: null,
@@ -1191,7 +1206,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
const pullRequest = yield* manager.branchPullRequest({ cwd: repoDir, branch: "main" });
expect(pullRequest).toEqual({
expect(pullRequest).toMatchObject({
state: "merged",
closedAt: null,
mergedAt: "2026-04-07T15:00:00Z",
@@ -1244,7 +1259,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
branch: "feature/deleted-local-branch",
});
expect(pullRequest).toEqual({
expect(pullRequest).toMatchObject({
state: "merged",
closedAt: null,
mergedAt: null,
@@ -1310,7 +1325,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
branch: "feature/deleted-fork-branch",
});
expect(pullRequest).toEqual({
expect(pullRequest).toMatchObject({
state: "merged",
closedAt: null,
mergedAt: null,
@@ -1432,6 +1447,17 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
updatedAt: "2026-04-07T15:00:00Z",
},
]),
encodeCliJson([
{
number: 221,
title: "New PR on the same branch",
url: "https://github.com/pingdotgg/codething-mvp/pull/221",
baseRefName: "main",
headRefName: "feature/shared-pr-cache",
state: "OPEN",
updatedAt: "2026-04-08T15:00:00Z",
},
]),
],
},
});
@@ -1445,6 +1471,16 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
expect(status.pr?.state).toBe("merged");
expect(pullRequest?.state).toBe("merged");
expect(ghCalls.filter((call) => call.startsWith("pr list "))).toHaveLength(1);
const refreshed = yield* manager.branchPullRequest(
{ cwd: repoDir, branch: "feature/shared-pr-cache" },
{ refresh: true },
);
expect(refreshed).toMatchObject({
number: 221,
state: "open",
repositoryKey: "github.com/pingdotgg/codething-mvp",
});
expect(ghCalls.filter((call) => call.startsWith("pr list "))).toHaveLength(2);
}),
);
@@ -1459,7 +1495,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
yield* runGit(repoDir, ["push", "-u", "origin", "feature/lookup-failure"]);
yield* runGit(repoDir, ["checkout", "main"]);
const { manager } = yield* makeManager({
const { manager, ghCalls } = yield* makeManager({
ghScenario: {
failWith: new GitHubCli.GitHubCliUnavailableError({
command: "gh",
@@ -1474,6 +1510,11 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
.pipe(Effect.flip);
expect(error._tag).toBe("SourceControlProviderError");
const refreshError = yield* manager
.branchPullRequest({ cwd: repoDir, branch: "feature/lookup-failure" }, { refresh: true })
.pipe(Effect.flip);
expect(refreshError._tag).toBe("SourceControlProviderError");
expect(ghCalls.filter((call) => call.startsWith("pr list "))).toHaveLength(1);
}),
);
@@ -1594,6 +1635,186 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
expect(Duration.toMillis(GitManager.prLookupFailureTtl(20))).toBe(900_000);
});
it.each([
[
"https://github.example.com/team/repository/pull/42?tab=files",
"github.example.com/team/repository",
],
[
"https://gitlab.example.com/group/subgroup/repository/-/merge_requests/42",
"gitlab.example.com/group/subgroup/repository",
],
["https://bitbucket.org/team/repository/pull-requests/42", "bitbucket.org/team/repository"],
[
"https://dev.azure.com/org/project/_git/repository/pullrequest/42",
"dev.azure.com/org/project/_git/repository",
],
[
"https://org.visualstudio.com/project/_git/repository/pullrequest/42",
"org.visualstudio.com/project/_git/repository",
],
[
"https://gitlab.example/group/pull/123/repository/-/merge_requests/42",
"gitlab.example/group/pull/123/repository",
],
["https://github.example.com/team/repository/issues/42", null],
] as const)("reads the repository from the returned PR URL %s", (url, expected) => {
expect(GitManager.pullRequestRepositoryKey(url)).toBe(expected);
});
it.effect("distinguishes Enterprise forks with the same head branch", () =>
Effect.gen(function* () {
const repoDir = yield* makeTempDir("t3code-git-manager-");
yield* initRepo(repoDir);
const originDir = yield* createBareRemote();
const forkDir = yield* createBareRemote();
yield* runGit(repoDir, ["remote", "add", "origin", originDir]);
yield* runGit(repoDir, ["push", "-u", "origin", "main"]);
yield* runGit(repoDir, ["remote", "add", "fork", forkDir]);
yield* runGit(repoDir, ["checkout", "-b", "feature"]);
yield* runGit(repoDir, ["push", "-u", "fork", "feature"]);
yield* configureVisibleRemoteUrlWithLocalRewrite(
repoDir,
"origin",
"[email protected]:team/repository.git",
originDir,
);
yield* configureVisibleRemoteUrlWithLocalRewrite(
repoDir,
"fork",
"[email protected]:alice/repository.git",
forkDir,
);
const output = encodeCliJson([
{
number: 2,
title: "Another fork",
url: "https://github.example.com/team/repository/pull/2",
baseRefName: "main",
headRefName: "feature",
state: "OPEN",
updatedAt: "2026-04-08T15:00:00Z",
isCrossRepository: true,
headRepository: { nameWithOwner: "bob/repository" },
headRepositoryOwner: { login: "bob" },
},
{
number: 1,
title: "This fork",
url: "https://github.example.com/team/repository/pull/1",
baseRefName: "main",
headRefName: "feature",
state: "OPEN",
updatedAt: "2026-04-07T15:00:00Z",
isCrossRepository: true,
headRepository: { nameWithOwner: "alice/repository" },
headRepositoryOwner: { login: "alice" },
},
]);
const { manager } = yield* makeManager({
ghScenario: {
prListByHeadSelector: {
"alice:feature": output,
"fork:feature": output,
feature: output,
},
},
});
expect(yield* manager.branchPullRequest({ cwd: repoDir, branch: "feature" })).toMatchObject({
number: 1,
repositoryKey: "github.example.com/team/repository",
});
}),
);
it.effect.each([
"[email protected]:Group/Subgroup/Fork.git",
"https://gitlab.com/Group/Subgroup/Fork.git",
])("matches nested GitLab forks through the adapter for %s", (remoteUrl) =>
Effect.gen(function* () {
const repoDir = yield* makeTempDir("t3code-git-manager-");
yield* initRepo(repoDir);
const originDir = yield* createBareRemote();
const forkDir = yield* createBareRemote();
const branch = "feature/NestedGroups";
yield* runGit(repoDir, ["remote", "add", "origin", originDir]);
yield* runGit(repoDir, ["push", "-u", "origin", "main"]);
yield* runGit(repoDir, ["remote", "add", "fork", forkDir]);
yield* runGit(repoDir, ["checkout", "-b", branch]);
yield* runGit(repoDir, ["push", "-u", "fork", branch]);
yield* configureVisibleRemoteUrlWithLocalRewrite(
repoDir,
"origin",
"[email protected]:Group/Upstream/Repository.git",
originDir,
);
yield* configureVisibleRemoteUrlWithLocalRewrite(repoDir, "fork", remoteUrl, forkDir);
const output = encodeCliJson([
{
iid: 2,
title: "Another subgroup's fork",
web_url: "https://gitlab.com/Group/Upstream/Repository/-/merge_requests/2",
target_branch: "main",
source_branch: branch,
state: "opened",
updated_at: "2026-04-08T15:00:00Z",
source_project_id: 102,
target_project_id: 100,
source_project: { path_with_namespace: "Group/Other/Fork" },
},
{
iid: 1,
title: "This subgroup's fork",
web_url: "https://gitlab.com/Group/Upstream/Repository/-/merge_requests/1",
target_branch: "main",
source_branch: branch,
state: "opened",
updated_at: "2026-04-07T15:00:00Z",
source_project_id: 101,
target_project_id: 100,
source_project: { path_with_namespace: "Group/Subgroup/Fork" },
},
]);
const calls: VcsProcess.VcsProcessInput[] = [];
const provider = yield* GitLabSourceControlProvider.make.pipe(
Effect.provide(
GitLabCli.layer.pipe(
Layer.provide(
Layer.mock(VcsProcess.VcsProcess)({
run: (input) =>
Effect.sync(() => {
calls.push(input);
return fakeGhOutput(output);
}),
}),
),
),
),
);
const { manager } = yield* makeManager({ sourceControlProvider: provider });
expect(yield* manager.branchPullRequest({ cwd: repoDir, branch })).toMatchObject({
number: 1,
repositoryKey: "gitlab.com/group/upstream/repository",
});
expect(calls.length).toBeGreaterThan(0);
for (const call of calls) {
expect(call.command).toBe("glab");
expect(call.args).toEqual([
"mr",
"list",
"--source-branch",
branch,
"--all",
"--per-page",
"20",
"--output",
"json",
]);
}
}),
);
it.effect(
"status ignores unrelated fork PRs when the current branch tracks the same repository",
() =>
@@ -2149,7 +2370,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
branch: "feature/fork-settle",
});
expect(pullRequest).toEqual({
expect(pullRequest).toMatchObject({
state: "merged",
closedAt: null,
mergedAt: null,
+46 -20
View File
@@ -77,6 +77,13 @@ export interface GitRemoteStatusOptions extends GitVcsDriver.GitRemoteStatusOpti
readonly refreshMissingPullRequest?: boolean;
}
export type GitBranchPullRequest = NonNullable<VcsStatusResult["pr"]> & {
readonly repositoryKey: string | null;
readonly updatedAt: string | null;
readonly closedAt?: string | null;
readonly mergedAt?: string | null;
};
interface SourceControlTextGenerationSettings {
readonly modelSelection: ModelSelection;
readonly style: SourceControlWritingStyleSettings;
@@ -96,18 +103,10 @@ export class GitManager extends Context.Service<
options?: GitRemoteStatusOptions,
) => Effect.Effect<VcsStatusRemoteResult | null, GitManagerServiceError>;
/** Resolve the PR for a saved branch without changing the current checkout. */
readonly branchPullRequest: (input: {
readonly cwd: string;
readonly branch: string;
}) => Effect.Effect<
{
readonly state: "open" | "closed" | "merged";
readonly updatedAt: string | null;
readonly closedAt?: string | null;
readonly mergedAt?: string | null;
} | null,
GitManagerServiceError
>;
readonly branchPullRequest: (
input: { readonly cwd: string; readonly branch: string },
options?: { readonly refresh?: boolean },
) => Effect.Effect<GitBranchPullRequest | null, GitManagerServiceError>;
readonly invalidateLocalStatus: (cwd: string) => Effect.Effect<void, never>;
readonly invalidateRemoteStatus: (cwd: string) => Effect.Effect<void, never>;
readonly invalidateStatus: (cwd: string) => Effect.Effect<void, never>;
@@ -214,9 +213,26 @@ interface BranchHeadContext {
isCrossRepository: boolean;
}
export function pullRequestRepositoryKey(value: string): string | null {
try {
const url = new URL(value);
const match =
/^(.*)(?:\/pull\/|\/-\/merge_requests\/|\/pull-requests\/|\/pullrequest\/)\d+(?:\/.*)?$/iu.exec(
url.pathname,
);
if (match?.[1] === undefined) return null;
url.pathname = match[1];
url.search = "";
url.hash = "";
return normalizeGitRemoteUrl(url.toString());
} catch {
return null;
}
}
function parseRepositoryNameFromPullRequestUrl(url: string): string | null {
const trimmed = url.trim();
const match = /^https:\/\/github\.com\/[^/]+\/([^/]+)\/pull\/\d+(?:\/.*)?$/i.exec(trimmed);
const match = /^https?:\/\/[^/]+\/[^/]+\/([^/]+)\/pull\/\d+(?:\/.*)?$/i.exec(trimmed);
const repositoryName = match?.[1]?.trim() ?? "";
return repositoryName.length > 0 ? repositoryName : null;
}
@@ -254,14 +270,14 @@ function resolvePullRequestWorktreeLocalBranchName(
return `t3code/pr-${pullRequest.number}/${suffix}`;
}
function parseGitHubRepositoryNameWithOwnerFromRemoteUrl(url: string | null): string | null {
function parseRepositoryNameWithOwnerFromRemoteUrl(url: string | null): string | null {
const trimmed = url?.trim() ?? "";
if (trimmed.length === 0) {
return null;
}
const match =
/^(?:git@github\.com:|ssh:\/\/git@github\.com\/|https:\/\/github\.com\/|git:\/\/github\.com\/)([^/\s]+\/[^/\s]+?)(?:\.git)?\/?$/i.exec(
/^(?:[^@/\s]+@[^:/\s]+:|(?:ssh|https?|git):\/\/[^/]+\/)((?:[^/\s]+\/)+[^/\s]+?)(?:\.git)?\/?$/iu.exec(
trimmed,
);
const repositoryNameWithOwner = match?.[1]?.trim() ?? "";
@@ -273,6 +289,7 @@ function parseRepositoryOwnerLogin(nameWithOwner: string | null): string | null
if (trimmed.length === 0) {
return null;
}
// GitLab reports the top-level group as owner. The full path distinguishes subgroups.
const [ownerLogin] = trimmed.split("/");
const normalizedOwnerLogin = ownerLogin?.trim() ?? "";
return normalizedOwnerLogin.length > 0 ? normalizedOwnerLogin : null;
@@ -1261,7 +1278,7 @@ export const make = Effect.gen(function* () {
}
const remoteUrl = yield* readConfigValueNullable(cwd, `remote.${remoteName}.url`);
const repositoryNameWithOwner = parseGitHubRepositoryNameWithOwnerFromRemoteUrl(remoteUrl);
const repositoryNameWithOwner = parseRepositoryNameWithOwnerFromRemoteUrl(remoteUrl);
return {
remoteUrlKey: remoteUrl ? normalizeGitRemoteUrl(remoteUrl) : null,
repositoryNameWithOwner,
@@ -2031,7 +2048,7 @@ export const make = Effect.gen(function* () {
});
const branchPullRequest: GitManager["Service"]["branchPullRequest"] = Effect.fn(
"branchPullRequest",
)(function* ({ cwd, branch }) {
)(function* ({ cwd, branch }, options) {
const cacheCwd = yield* normalizeStatusCacheKey(cwd);
const remotes = yield* gitCore.execute({
operation: "GitManager.branchPullRequest.remotes",
@@ -2111,6 +2128,14 @@ export const make = Effect.gen(function* () {
localBranchExists,
...(localBranchExists ? {} : { remoteName }),
});
if (options?.refresh) {
// A completed turn can create a PR or reuse a merged PR's branch.
// Refresh successful answers, but keep failed lookups' retry backoff.
const cached = yield* Cache.getOption(prLookupCache, cacheKey).pipe(
Effect.orElseSucceed(() => Option.none()),
);
if (Option.isSome(cached)) yield* Cache.invalidate(prLookupCache, cacheKey);
}
let cached = yield* Cache.get(prLookupCache, cacheKey);
// The cached head context may have resolved on a different remote than
// the saved upstream: a branch tracking origin/main but pushed to a fork
@@ -2165,12 +2190,13 @@ export const make = Effect.gen(function* () {
) {
return null;
}
const statusPr = toStatusPr(latest);
return {
state: statusPr.state,
updatedAt: statusPr.updatedAt,
...toStatusPr(latest),
closedAt: latest.closedAt ?? null,
mergedAt: latest.mergedAt ?? null,
// Hosting CLIs can select an upstream repository instead of origin.
// The returned PR URL names the repository that actually owns it.
repositoryKey: pullRequestRepositoryKey(latest.url),
};
});
const invalidateLocalStatus: GitManager["Service"]["invalidateLocalStatus"] = Effect.fn(