Skip to content

Commit c593683

Browse files
committed
fix: read GitHub Enterprise pull request URLs everywhere they are reused
URLs a provider returned or a thread already accepted are read back with their own host, so the details panel, stack links, branch lookups and the legacy linked check work on Enterprise hosts. The link dialog and the MCP tools read pasted URLs with every project's GitHub hosts, and Forgejo identities stay out of GitHub's credential lookup.
1 parent 553276f commit c593683

9 files changed

Lines changed: 71 additions & 20 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import {
6060
isSshRemoteUrl,
6161
type ChangeRequestTerminology,
6262
} from "@t3tools/shared/sourceControl";
63-
import { parseChangeRequestUrl } from "@t3tools/shared/changeRequestUrl";
63+
import { parseChangeRequestUrl, providerUrlHosts } from "@t3tools/shared/changeRequestUrl";
6464
import {
6565
normalizeThreadPullRequestKey,
6666
threadPullRequestKeyOf,
@@ -1171,7 +1171,7 @@ export const make = Effect.gen(function* () {
11711171
const lookupStates = new Map<string, PullRequestInfo["state"]>();
11721172
const noteLookupState = (pr: PullRequestInfo) =>
11731173
Effect.suspend(() => {
1174-
const parsed = parseChangeRequestUrl(pr.url);
1174+
const parsed = parseChangeRequestUrl(pr.url, providerUrlHosts(pr.url));
11751175
if (parsed === null || parsed.number !== pr.number) return Effect.void;
11761176
const key = normalizeThreadPullRequestKey(parsed);
11771177
const id = threadPullRequestKeyOf(key);

‎apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ const makeHarness = Effect.fn("makePullRequestsToolkitHarness")(function* (
158158
const layerDependencies = Layer.mergeAll(
159159
Layer.mock(ProjectService.ProjectService)({
160160
getShell: () => Effect.succeed(Option.fromNullishOr(project)),
161+
listShells: () => Effect.succeed(project ? [project] : []),
161162
}),
162163
Layer.mock(Orchestrator.OrchestratorV2)({
163164
getThreadShell: (id) =>

‎apps/server/src/mcp/toolkits/pullRequests/handlers.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ function projectHostAndProvider(project: OrchestrationProjectShell | undefined):
7979
const resolveTarget = Effect.fn("PullRequestsToolkit.resolveTarget")(function* (
8080
input: PullRequestTargetInput,
8181
project: OrchestrationProjectShell | undefined,
82+
gitHubHosts: ReadonlyArray<string>,
8283
) {
8384
if (input.url !== undefined) {
84-
const parsed = parseChangeRequestUrl(input.url, gitHubHostsOf(project ? [project] : []));
85+
const parsed = parseChangeRequestUrl(input.url, gitHubHosts);
8586
if (parsed === null) {
8687
return yield* new PullRequestUrlInvalidError({});
8788
}
@@ -192,6 +193,13 @@ const make = Effect.gen(function* () {
192193
return thread.value;
193194
});
194195

196+
/** Every project's GitHub hosts, so an agent's URL is read the way the web client reads it. */
197+
const knownGitHubHosts = (project: OrchestrationProjectShell | undefined) =>
198+
projects.listShells().pipe(
199+
Effect.map(gitHubHostsOf),
200+
Effect.orElseSucceed(() => gitHubHostsOf(project ? [project] : [])),
201+
);
202+
195203
const projectOf = (
196204
thread: OrchestrationV2ThreadShell,
197205
Failure:
@@ -231,7 +239,7 @@ const make = Effect.gen(function* () {
231239
) {
232240
const thread = yield* requireThread(PullRequestWatchFailedError, input.threadId);
233241
const project = yield* projectOf(thread, PullRequestWatchFailedError);
234-
const target = yield* resolveTarget(input, project);
242+
const target = yield* resolveTarget(input, project, yield* knownGitHubHosts(project));
235243
const watchedLink = (shell: OrchestrationV2ThreadShell) =>
236244
threadPullRequestsOf(shell).find(
237245
(link) => link.source !== "stack-dismissed" && threadPullRequestKeysEqual(link, target),
@@ -278,7 +286,7 @@ const make = Effect.gen(function* () {
278286
Effect.gen(function* () {
279287
const thread = yield* requireThread(PullRequestLinkFailedError, input.threadId);
280288
const project = yield* projectOf(thread, PullRequestLinkFailedError);
281-
const target = yield* resolveTarget(input, project);
289+
const target = yield* resolveTarget(input, project, yield* knownGitHubHosts(project));
282290
const existing = threadPullRequestsOf(thread).find((link) =>
283291
threadPullRequestKeysEqual(link, target),
284292
);
@@ -309,7 +317,7 @@ const make = Effect.gen(function* () {
309317
Effect.gen(function* () {
310318
const thread = yield* requireThread(PullRequestUnlinkFailedError, input.threadId);
311319
const project = yield* projectOf(thread, PullRequestUnlinkFailedError);
312-
const target = yield* resolveTarget(input, project);
320+
const target = yield* resolveTarget(input, project, yield* knownGitHubHosts(project));
313321
if (!threadPullRequestsOf(thread).some((link) => threadPullRequestKeysEqual(link, target)))
314322
return {
315323
host: target.host,

‎apps/web/src/components/ThreadStatusIndicators.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { useRender } from "@base-ui/react/use-render";
3030
import { type ReactNode, type AnimationEvent, type MouseEvent, type ReactElement } from "react";
3131
import { cn } from "../lib/utils";
3232

33+
import { providerUrlHosts } from "@t3tools/shared/changeRequestUrl";
3334
import { parseChangeRequestUrl } from "../lib/openPullRequestLink";
3435
import { useEnvironmentQuery } from "../state/query";
3536
import { linkedPullRequestDetailAtom, useSharedPullRequestSummary } from "../state/pullRequests";
@@ -98,7 +99,7 @@ export function useLinkedThreadPullRequest(
9899
// touch the cache on every render.
99100
const reference = useMemo(() => {
100101
if (fallback == null) return null;
101-
const host = parseChangeRequestUrl(fallback.url)?.host;
102+
const host = parseChangeRequestUrl(fallback.url, providerUrlHosts(fallback.url))?.host;
102103
return { ...fallback, ...(host === undefined ? {} : { host }) };
103104
}, [fallback]);
104105
const queried = useEnvironmentQuery(

‎apps/web/src/components/pullRequest/LinkPullRequestDialog.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,16 @@ export function resolveLinkPullRequestInput(input: {
8080
readonly webUrl: (number: number) => string | null;
8181
} | null;
8282
readonly hasProject: (reference: ResolvedLink) => boolean;
83+
/** Reads a change request URL with the environment's GitHub hosts. */
84+
readonly parse?: (url: string) => ReturnType<typeof parseChangeRequestUrl>;
8385
}): { link: ResolvedLink } | { error: string } | null {
86+
const parse = input.parse ?? ((url: string) => parseChangeRequestUrl(url));
8487
const parsed =
85-
parseChangeRequestUrl(input.reference.trim()) !== null
88+
parse(input.reference.trim()) !== null
8689
? input.reference.trim()
8790
: parsePullRequestReference(input.reference);
8891
if (parsed === null) return null;
89-
const url = parseChangeRequestUrl(parsed);
92+
const url = parse(parsed);
9093
if (url !== null) {
9194
if (!input.hasProject({ ...url, url: parsed })) {
9295
return { error: `No project in this environment can read ${url.host}/${url.repository}.` };
@@ -101,7 +104,7 @@ export function resolveLinkPullRequestInput(input: {
101104
return { error: "Paste a full URL to link a pull request from another repository." };
102105
}
103106
const webUrl = input.project.webUrl(number);
104-
const webReference = webUrl === null ? null : parseChangeRequestUrl(webUrl);
107+
const webReference = webUrl === null ? null : parse(webUrl);
105108
if (webUrl === null || webReference === null) {
106109
return { error: "Paste a full URL; this project's host has no known pull request URL." };
107110
}
@@ -166,6 +169,7 @@ function LinkPullRequestDialog({
166169
reference,
167170
project: ownProject,
168171
hasProject: (reference) => linking.canLink(reference.url),
172+
parse: linking.parse,
169173
}),
170174
[linking, ownProject, reference],
171175
);

‎apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parseChangeRequestUrl } from "@t3tools/shared/changeRequestUrl";
1+
import { parseChangeRequestUrl, providerUrlHosts } from "@t3tools/shared/changeRequestUrl";
22
import { useAtomValue } from "@effect/atom-react";
33
import { usePullRequestStack } from "~/state/usePullRequestStack";
44
import { RefreshIcon } from "~/components/ui/refresh-icon";
@@ -732,7 +732,8 @@ export function PullRequestDetailPanel({
732732
useEffect(() => {
733733
if (detail?.autoMergeMethod !== undefined) setMergeMethod(detail.autoMergeMethod);
734734
}, [detail?.autoMergeMethod, pullRequestKey]);
735-
const repositoryUrl = detail === null ? null : changeRequestRepositoryUrl(detail.url);
735+
const repositoryUrl =
736+
detail === null ? null : changeRequestRepositoryUrl(detail.url, providerUrlHosts(detail.url));
736737
const hostClient = sourceControlClients.get(detail?.provider);
737738
const markdownContext = useMemo(
738739
() => ({
@@ -752,7 +753,7 @@ export function PullRequestDetailPanel({
752753
handoffSummary.number,
753754
handoffSummary.headBranch,
754755
detail?.headRepositoryNameWithOwner,
755-
changeRequestRepositoryUrl(handoffSummary.url),
756+
changeRequestRepositoryUrl(handoffSummary.url, providerUrlHosts(handoffSummary.url)),
756757
)
757758
: loadingPullRequestCheckoutCommand(reference, repositoryIdentity);
758759
const onCheckoutCommandError = useCallback((error: Error) => {
@@ -784,7 +785,12 @@ export function PullRequestDetailPanel({
784785
() =>
785786
detail === null || detail.capabilities.stacks !== true || !supportsThreadPullRequests
786787
? null
787-
: { ...reference, host: reference.host ?? parseChangeRequestUrl(detail.url)?.host },
788+
: {
789+
...reference,
790+
host:
791+
reference.host ??
792+
parseChangeRequestUrl(detail.url, providerUrlHosts(detail.url))?.host,
793+
},
788794
[detail, reference, supportsThreadPullRequests],
789795
);
790796
const nativeStackQuery = usePullRequestStack(environmentId, stackReference);

‎apps/web/src/hooks/usePullRequestLinking.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function usePullRequestLinking(environmentId: EnvironmentId | null | unde
7979
if (mode !== "multiple")
8080
return (
8181
thread.linkedPullRequest != null &&
82-
matchesLinkedPullRequestUrl(thread.linkedPullRequest, url)
82+
matchesLinkedPullRequestUrl(thread.linkedPullRequest, url, gitHubHosts)
8383
);
8484
const parsed = parse(url);
8585
return (

‎packages/shared/src/changeRequestUrl.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { describe, expect, it } from "vite-plus/test";
22

33
import {
4+
changeRequestRepositoryUrl,
45
changeRequestUrlFor,
56
gitHubHostsOf,
7+
providerUrlHosts,
68
parseChangeRequestUrl,
79
pullRequestCandidateUrlFromReferenceAutolink,
810
siblingPullRequestUrl,
@@ -236,3 +238,15 @@ describe("changeRequestUrlFor", () => {
236238
});
237239
});
238240
});
241+
242+
describe("URLs a provider returned", () => {
243+
const url = "https://git.corp.example/team/workspace/pull/7/files";
244+
245+
it("reads a stored GitHub Enterprise link back on a host named nothing like GitHub", () => {
246+
expect(siblingPullRequestUrl(url, 8)).toBe("https://git.corp.example/team/workspace/pull/8");
247+
expect(changeRequestRepositoryUrl(url, providerUrlHosts(url))).toBe(
248+
"https://git.corp.example/team/workspace",
249+
);
250+
expect(changeRequestRepositoryUrl(url)).toBeNull();
251+
});
252+
});

‎packages/shared/src/changeRequestUrl.ts‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ export function gitHubHostsOf(
113113
);
114114
}
115115

116+
/**
117+
* The host of a change request URL a provider returned itself or a thread already accepted, as
118+
* `parseChangeRequestUrl`'s `gitHubHosts`: reading such a URL back needs no lookalike guard.
119+
*/
120+
export function providerUrlHosts(url: string): string[] {
121+
const host = URL.parse(url)?.hostname.toLowerCase();
122+
return host ? [host] : [];
123+
}
124+
116125
function claim(host: string, match: RegExpExecArray | null): ChangeRequestLink | null {
117126
const repository = match?.[1];
118127
const number = Number(match?.[2]);
@@ -228,9 +237,13 @@ export function pullRequestCandidateUrlFromReferenceAutolink(targetUrl: string):
228237
export function matchesLinkedPullRequestUrl(
229238
linkedPullRequest: ThreadLinkedPullRequest,
230239
targetUrl: string,
240+
gitHubHosts: ReadonlyArray<string> = [],
231241
): boolean {
232-
const linked = parseChangeRequestUrl(linkedPullRequest.url);
233-
const target = parseChangeRequestUrl(targetUrl);
242+
const linked = parseChangeRequestUrl(
243+
linkedPullRequest.url,
244+
providerUrlHosts(linkedPullRequest.url),
245+
);
246+
const target = parseChangeRequestUrl(targetUrl, gitHubHosts);
234247
return (
235248
linked !== null &&
236249
target !== null &&
@@ -242,8 +255,11 @@ export function matchesLinkedPullRequestUrl(
242255
}
243256

244257
/** The repository root behind a recognised change-request URL, without PR-specific state. */
245-
export function changeRequestRepositoryUrl(targetUrl: string): string | null {
246-
const changeRequest = parseChangeRequestUrl(targetUrl);
258+
export function changeRequestRepositoryUrl(
259+
targetUrl: string,
260+
gitHubHosts: ReadonlyArray<string> = [],
261+
): string | null {
262+
const changeRequest = parseChangeRequestUrl(targetUrl, gitHubHosts);
247263
if (changeRequest === null) return null;
248264
const url = new URL(targetUrl);
249265
const repositoryPath =
@@ -258,8 +274,9 @@ export function changeRequestRepositoryUrl(targetUrl: string): string | null {
258274
return url.toString();
259275
}
260276

277+
/** The URL of another change request in a linked one's repository; `url` is a stored link. */
261278
export function siblingPullRequestUrl(url: string, number: number): string | null {
262-
const reference = parseChangeRequestUrl(url);
279+
const reference = parseChangeRequestUrl(url, providerUrlHosts(url));
263280
if (reference === null || !Number.isSafeInteger(number) || number < 1) return null;
264281
const sibling = new URL(url);
265282
const route = /^\/(-\/merge_requests|pulls?|pull-requests|pullrequest)\/\d+(?:\/|$)/u.exec(

0 commit comments

Comments
 (0)