Skip to content

Commit eba2c49

Browse files
refactor(source-control): forges parse their own change request URLs
packages/shared/src/changeRequestUrl.ts re-implemented every forge's change request URL shapes behind a host and kind switch, duplicating (and drifting from) the hooks in each forge's client definition, and source-control-core imported its link type from shared. Each forge's client definition now owns parseChangeRequestUrl and changeRequestUrlFromReferenceAutolink, and changeRequestUrl builds below a repository web URL the same way everywhere. The registry picks the first definition that claims a URL, with GitCafe claiming before GitHub and Forgejo. isChangeRequestUrl and changeRequestUrlHost are gone. Server drivers carry their definition, so the server builds one registry from them, and so do the host secret fields. Thread pull request identity moves to source-control-core and is bound to a registry once per side. Migrations 050 and 064 keep a frozen copy of the parsing they shipped with. Behaviour changes: GitHub claims /<owner>/<repo>/pull/<n> on any host, so GitHub Enterprise links on custom hostnames work. A link that matches no project still opens in the browser. On mobile, a pull request link opens in the app only under a project whose repository holds it; before, it opened under whichever project was current. The MCP link tools no longer invent a GitHub URL for a project on an unknown host. The "open on host" fallback in the pull request panel now covers every forge, not only GitHub. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bb7783e commit eba2c49

95 files changed

Lines changed: 2036 additions & 1481 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/mobile/src/features/keyboard/HardwareKeyboardCommandProvider.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { StackActions, useNavigation } from "@react-navigation/native";
2-
import { resolveThreadReferenceCopyTarget } from "@t3tools/shared/threadReference";
2+
import { resolveThreadReferenceCopyTarget } from "@t3tools/client-runtime/thread-reference";
33
import {
44
createContext,
55
use,
Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,61 @@
1+
import { EnvironmentId, ProjectId } from "@t3tools/contracts";
12
import { describe, expect, it } from "vite-plus/test";
23
import { nativePullRequestDestination } from "../../lib/pullRequestNavigation";
4+
35
const context = { environmentId: "remote", projectId: "project" };
6+
const project = (id: string, canonicalKey: string, provider: string, remoteUrl: string) => ({
7+
id: ProjectId.make(id),
8+
environmentId: EnvironmentId.make("remote"),
9+
repositoryIdentity: {
10+
canonicalKey,
11+
locator: { source: "git-remote" as const, remoteName: "origin", remoteUrl },
12+
displayName: canonicalKey.split("/").slice(1).join("/"),
13+
provider,
14+
},
15+
});
16+
const projects = [
17+
project("project", "github.com/org/repo", "github", "[email protected]:org/repo.git"),
18+
project("gitlab", "git.example/group/sub/repo", "gitlab", "[email protected]:group/sub/repo.git"),
19+
project("forgejo", "forgejo.test/org/repo", "forgejo", "http://forgejo.test:3000/org/repo.git"),
20+
];
21+
422
describe("native PR entry points", () => {
523
it("keeps a thread link in its own environment and handles renamed host path shapes", () => {
624
expect(
7-
nativePullRequestDestination("https://github.com/org/repo/pull/12/files#diff", context),
25+
nativePullRequestDestination(
26+
"https://github.com/org/repo/pull/12/files#diff",
27+
context,
28+
projects,
29+
),
830
).toEqual({ ...context, host: "github.com", repository: "org/repo", number: 12 });
931
expect(
10-
nativePullRequestDestination("https://git.example/group/sub/repo/-/merge_requests/9", context)
11-
?.repository,
12-
).toBe("group/sub/repo");
32+
nativePullRequestDestination(
33+
"https://git.example/group/sub/repo/-/merge_requests/9",
34+
context,
35+
projects,
36+
),
37+
).toMatchObject({ projectId: "gitlab", repository: "group/sub/repo" });
1338
expect(
14-
nativePullRequestDestination("http://forgejo.test:3000/org/repo/pulls/3", context)?.host,
39+
nativePullRequestDestination("http://forgejo.test:3000/org/repo/pulls/3", context, projects)
40+
?.host,
1541
).toBe("forgejo.test:3000");
1642
});
43+
44+
it("leaves pull requests from repositories without a project to the external opener", () => {
45+
expect(
46+
nativePullRequestDestination("https://github.com/someone/else/pull/12", context, projects),
47+
).toBeNull();
48+
});
49+
1750
it("leaves issues, unknown sites, and unscoped links to the external opener", () => {
1851
expect(
19-
nativePullRequestDestination("https://github.com/org/repo/issues/12", context),
52+
nativePullRequestDestination("https://github.com/org/repo/issues/12", context, projects),
53+
).toBeNull();
54+
expect(
55+
nativePullRequestDestination("https://example.com/org/repo/pull/12", context, projects),
2056
).toBeNull();
2157
expect(
22-
nativePullRequestDestination("https://example.com/org/repo/pull/12", context),
58+
nativePullRequestDestination("https://github.com/org/repo/pull/12", null, projects),
2359
).toBeNull();
24-
expect(nativePullRequestDestination("https://github.com/org/repo/pull/12", null)).toBeNull();
2560
});
2661
});

‎apps/mobile/src/features/pull-requests/usePullRequestStack.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { threadPullRequestLinkMode } from "@t3tools/client-runtime/thread-pull-request-compatibility";
22
import type { PullRequestDetail } from "@t3tools/contracts";
3-
import { parseChangeRequestUrl } from "@t3tools/shared/changeRequestUrl";
3+
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
44
import { useMemo } from "react";
55
import { useEnvironmentServerConfig, useNavigationThreadShells } from "../../state/entities";
66
import { pullRequestStackAtom } from "../../state/pull-requests";
@@ -18,7 +18,9 @@ export function usePullRequestStack(params: PullRequestParams, pr: PullRequestDe
1818
const reference = useMemo(
1919
() => ({
2020
...pullRequestTarget(params).input,
21-
host: params.host ?? (pr ? parseChangeRequestUrl(pr.url)?.host : undefined),
21+
host:
22+
params.host ??
23+
(pr ? sourceControlClients.parseChangeRequestUrl(pr.url)?.link.host : undefined),
2224
}),
2325
[params, pr],
2426
);

‎apps/mobile/src/features/pull-requests/usePullRequestThreads.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111
type PullRequestLinkedThreadsResult,
1212
} from "@t3tools/contracts";
1313
import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences";
14-
import { parseChangeRequestUrl } from "@t3tools/shared/changeRequestUrl";
15-
import { normalizeThreadPullRequestKey } from "@t3tools/shared/threadPullRequests";
14+
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
15+
import { normalizeThreadPullRequestKey } from "@t3tools/client-runtime/thread-pull-requests";
1616
import { useRef, useState } from "react";
1717
import { Platform } from "react-native";
1818
import { useAndroidWorkspaceSelection } from "../workspace/useAndroidWorkspaceSelection";
@@ -49,7 +49,7 @@ export function usePullRequestThreads(
4949
const target = pullRequestTarget(params);
5050
const config = useEnvironmentServerConfig(target.environmentId);
5151
const supported = threadPullRequestLinkMode(config?.environment.capabilities) === "multiple";
52-
const parsed = pr ? parseChangeRequestUrl(pr.url) : null;
52+
const parsed = pr ? sourceControlClients.parseChangeRequestUrl(pr.url)?.link : undefined;
5353
const reference = {
5454
...target,
5555
input: { ...target.input, ...(parsed ? normalizeThreadPullRequestKey(parsed) : {}) },

‎apps/mobile/src/features/threads/LinkedPullRequestList.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
resolveThreadPullRequestChains,
55
threadPullRequestKeyOf,
66
type PullRequestListLine,
7-
} from "@t3tools/shared/threadPullRequests";
7+
} from "@t3tools/client-runtime/thread-pull-requests";
88
import { memo, useMemo } from "react";
99
import { Pressable, useWindowDimensions, View } from "react-native";
1010

‎apps/mobile/src/features/threads/new-task-context-presentation.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { sanitizeNewRefName } from "@t3tools/shared/git";
2-
import { parseChangeRequestUrl } from "@t3tools/shared/changeRequestUrl";
2+
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
33
import { resolveSendEnvMode } from "@t3tools/shared/projectSettings";
44
import type { ComposerDraftWorkspaceSelection } from "../../state/use-composer-drafts";
55

@@ -79,7 +79,7 @@ export function resolveNewTaskBranchLabel(input: {
7979
readonly pullRequestReference?: string;
8080
}): string {
8181
if (input.workspaceMode === "worktree" && input.pullRequestReference) {
82-
const pr = parseChangeRequestUrl(input.pullRequestReference);
82+
const pr = sourceControlClients.parseChangeRequestUrl(input.pullRequestReference)?.link;
8383
if (pr) return `From PR #${pr.number}`;
8484
}
8585
if (!input.branchName) {

‎apps/mobile/src/lib/pullRequestNavigation.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
import { parseChangeRequestUrl } from "@t3tools/shared/changeRequestUrl";
1+
import { findProjectForChangeRequest } from "@t3tools/client-runtime/change-request-links";
2+
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
3+
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
24

5+
/**
6+
* Opens a change request in the app only under a project of the link's environment whose own
7+
* repository holds it, preferring the one the link came from; anything else is the browser's.
8+
*/
39
export function nativePullRequestDestination(
410
url: string,
511
context: { environmentId: string; projectId: string } | null,
12+
projects: ReadonlyArray<Pick<EnvironmentProject, "id" | "environmentId" | "repositoryIdentity">>,
613
) {
7-
const parsed = parseChangeRequestUrl(url);
14+
const parsed = sourceControlClients.parseChangeRequestUrl(url)?.link;
815
if (!parsed || !context) return null;
16+
const candidates = projects.filter((project) => project.environmentId === context.environmentId);
17+
const project = findProjectForChangeRequest(
18+
[
19+
...candidates.filter((candidate) => candidate.id === context.projectId),
20+
...candidates.filter((candidate) => candidate.id !== context.projectId),
21+
],
22+
parsed,
23+
);
24+
if (project === undefined) return null;
925
return {
10-
...context,
26+
environmentId: context.environmentId,
27+
projectId: String(project.id),
1128
repository: parsed.repository,
1229
number: parsed.number,
1330
host: parsed.authority ?? parsed.host,

‎apps/mobile/src/native/useOpenPullRequest.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useCallback } from "react";
44
import { Platform } from "react-native";
55
import { resetAndroidWorkspace } from "../lib/android-workspace-navigation";
66
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
7-
import { useThreadShell } from "../state/entities";
7+
import { useProjects, useThreadShell } from "../state/entities";
88
import { tryOpenExternalUrl } from "../lib/openExternalUrl";
99

1010
type PullRequestContext = { readonly environmentId: string; readonly projectId: string };
@@ -13,18 +13,19 @@ type PullRequestContext = { readonly environmentId: string; readonly projectId:
1313
// route context for useRoute, can open pull requests for any thread.
1414
export function useNavigateToPullRequest(options?: { readonly replace?: boolean }) {
1515
const navigation = useNavigation();
16+
const projects = useProjects();
1617
const replace = options?.replace;
1718
return useCallback(
1819
async (url: string, context: PullRequestContext | null) => {
19-
const params = nativePullRequestDestination(url, context);
20+
const params = nativePullRequestDestination(url, context, projects);
2021
if (!params) return tryOpenExternalUrl(url, "pull-request");
2122
if (Platform.OS === "android")
2223
resetAndroidWorkspace(navigation, { name: "PullRequest", params });
2324
else if (replace) navigation.dispatch(StackActions.replace("PullRequest", params));
2425
else navigation.navigate("PullRequest", params);
2526
return true;
2627
},
27-
[navigation, replace],
28+
[navigation, projects, replace],
2829
);
2930
}
3031

‎apps/mobile/src/state/thread-pr-presentation.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { sourceControlClients } from "@t3tools/client-runtime/source-control-cli
99
import {
1010
resolveThreadCurrentPullRequestLink,
1111
resolveThreadPullRequestBadge,
12-
} from "@t3tools/shared/threadPullRequests";
12+
} from "@t3tools/client-runtime/thread-pull-requests";
1313

1414
export type ThreadPr = NonNullable<VcsStatusResult["pr"]>;
1515

‎apps/server/src/git/GitManager.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ import {
6262
isSshRemoteUrl,
6363
type ChangeRequestTerminology,
6464
} from "@t3tools/shared/sourceControl";
65-
import { parseChangeRequestUrl } from "@t3tools/shared/changeRequestUrl";
6665
import * as HostProcess from "@t3tools/shared/HostProcess";
6766
import {
6867
parseGitHubRepositorySelector,
@@ -71,7 +70,8 @@ import {
7170
import {
7271
normalizeThreadPullRequestKey,
7372
threadPullRequestKeyOf,
74-
} from "@t3tools/shared/threadPullRequests";
73+
} from "../sourceControl/threadPullRequests.ts";
74+
import { sourceControlClients } from "../sourceControl/sourceControlClients.ts";
7575

7676
import { GitManagerError, GitPullRequestMaterializationError } from "@t3tools/contracts";
7777
import {
@@ -1202,8 +1202,8 @@ export const make = Effect.gen(function* () {
12021202
>();
12031203
const noteLookupState = (pr: PullRequestInfo) =>
12041204
Effect.suspend(() => {
1205-
const parsed = parseChangeRequestUrl(pr.url);
1206-
if (parsed === null || parsed.number !== pr.number) return Effect.void;
1205+
const parsed = sourceControlClients.parseChangeRequestUrl(pr.url)?.link;
1206+
if (parsed === undefined || parsed.number !== pr.number) return Effect.void;
12071207
const key = normalizeThreadPullRequestKey(parsed);
12081208
const id = threadPullRequestKeyOf(key);
12091209
const previous = lookupStates.get(id);

0 commit comments

Comments
 (0)