Skip to content

Commit d8c7e90

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 568b455 commit d8c7e90

8 files changed

Lines changed: 72 additions & 21 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,
@@ -1172,7 +1172,7 @@ export const make = Effect.gen(function* () {
11721172
const lookupStates = new Map<string, PullRequestInfo["state"]>();
11731173
const noteLookupState = (pr: PullRequestInfo) =>
11741174
Effect.suspend(() => {
1175-
const parsed = parseChangeRequestUrl(pr.url);
1175+
const parsed = parseChangeRequestUrl(pr.url, providerUrlHosts(pr.url));
11761176
if (parsed === null || parsed.number !== pr.number) return Effect.void;
11771177
const key = normalizeThreadPullRequestKey(parsed);
11781178
const id = threadPullRequestKeyOf(key);

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ function projectHostAndProvider(project: OrchestrationProjectShell | undefined):
8484
const resolveTarget = Effect.fn("PullRequestsToolkit.resolveTarget")(function* (
8585
input: PullRequestTargetInput,
8686
project: OrchestrationProjectShell | undefined,
87+
gitHubHosts: ReadonlyArray<string>,
8788
) {
8889
if (input.url !== undefined) {
89-
const parsed = parseChangeRequestUrl(input.url, gitHubHostsOf(project ? [project] : []));
90+
const parsed = parseChangeRequestUrl(input.url, gitHubHosts);
9091
if (parsed === null) {
9192
return yield* new PullRequestUrlInvalidError({});
9293
}
@@ -237,6 +238,13 @@ const make = Effect.gen(function* () {
237238
return thread.value;
238239
});
239240

241+
/** Every project's GitHub hosts, so an agent's URL is read the way the web client reads it. */
242+
const knownGitHubHosts = (project: OrchestrationProjectShell | undefined) =>
243+
projects.listShells().pipe(
244+
Effect.map(gitHubHostsOf),
245+
Effect.orElseSucceed(() => gitHubHostsOf(project ? [project] : [])),
246+
);
247+
240248
const projectOf = (
241249
thread: OrchestrationV2ThreadShell,
242250
Failure:
@@ -276,7 +284,7 @@ const make = Effect.gen(function* () {
276284
) {
277285
const thread = yield* requireThread(PullRequestWatchFailedError, input.threadId);
278286
const project = yield* projectOf(thread, PullRequestWatchFailedError);
279-
const target = yield* resolveTarget(input, project);
287+
const target = yield* resolveTarget(input, project, yield* knownGitHubHosts(project));
280288
const watchedLink = (shell: OrchestrationV2ThreadShell) =>
281289
threadPullRequestsOf(shell).find(
282290
(link) => link.source !== "stack-dismissed" && threadPullRequestKeysEqual(link, target),
@@ -332,9 +340,10 @@ const make = Effect.gen(function* () {
332340
.listShells()
333341
.pipe(Effect.mapError((cause) => new PullRequestListFailedError({ cause })));
334342
const callerProject = known.find((project) => project.id === callerThread?.projectId);
343+
const gitHubHosts = gitHubHostsOf(known);
335344
// Each target resolves on its own, so one bad URL in a batch is that entry's failure only.
336345
return yield* Effect.forEach(targets, (input) =>
337-
resolveTarget(input, callerProject).pipe(
346+
resolveTarget(input, callerProject, gitHubHosts).pipe(
338347
Effect.map((target) => ({
339348
input,
340349
target,
@@ -373,7 +382,7 @@ const make = Effect.gen(function* () {
373382
Effect.gen(function* () {
374383
const thread = yield* requireThread(PullRequestLinkFailedError, input.threadId);
375384
const project = yield* projectOf(thread, PullRequestLinkFailedError);
376-
const target = yield* resolveTarget(input, project);
385+
const target = yield* resolveTarget(input, project, yield* knownGitHubHosts(project));
377386
const existing = threadPullRequestsOf(thread).find((link) =>
378387
threadPullRequestKeysEqual(link, target),
379388
);
@@ -404,7 +413,7 @@ const make = Effect.gen(function* () {
404413
Effect.gen(function* () {
405414
const thread = yield* requireThread(PullRequestUnlinkFailedError, input.threadId);
406415
const project = yield* projectOf(thread, PullRequestUnlinkFailedError);
407-
const target = yield* resolveTarget(input, project);
416+
const target = yield* resolveTarget(input, project, yield* knownGitHubHosts(project));
408417
if (!threadPullRequestsOf(thread).some((link) => threadPullRequestKeysEqual(link, target)))
409418
return {
410419
host: target.host,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
} from "react";
3737
import { cn } from "../lib/utils";
3838

39+
import { providerUrlHosts } from "@t3tools/shared/changeRequestUrl";
3940
import { parseChangeRequestUrl } from "../lib/openPullRequestLink";
4041
import { useEnvironmentQuery } from "../state/query";
4142
import { linkedPullRequestDetailAtom, useSharedPullRequestSummary } from "../state/pullRequests";
@@ -105,7 +106,7 @@ export function useLinkedThreadPullRequest(
105106
// touch the cache on every render.
106107
const reference = useMemo(() => {
107108
if (fallback == null) return null;
108-
const host = parseChangeRequestUrl(fallback.url)?.host;
109+
const host = parseChangeRequestUrl(fallback.url, providerUrlHosts(fallback.url))?.host;
109110
return { ...fallback, ...(host === undefined ? {} : { host }) };
110111
}, [fallback]);
111112
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";
@@ -795,7 +795,8 @@ export function PullRequestDetailPanel({
795795
useEffect(() => {
796796
if (detail?.autoMergeMethod !== undefined) setMergeMethod(detail.autoMergeMethod);
797797
}, [detail?.autoMergeMethod, pullRequestKey]);
798-
const repositoryUrl = detail === null ? null : changeRequestRepositoryUrl(detail.url);
798+
const repositoryUrl =
799+
detail === null ? null : changeRequestRepositoryUrl(detail.url, providerUrlHosts(detail.url));
799800
const hostClient = sourceControlClients.get(detail?.provider);
800801
const markdownContext = useMemo(
801802
() => ({
@@ -815,7 +816,7 @@ export function PullRequestDetailPanel({
815816
handoffSummary.number,
816817
handoffSummary.headBranch,
817818
detail?.headRepositoryNameWithOwner,
818-
changeRequestRepositoryUrl(handoffSummary.url),
819+
changeRequestRepositoryUrl(handoffSummary.url, providerUrlHosts(handoffSummary.url)),
819820
)
820821
: loadingPullRequestCheckoutCommand(reference, repositoryIdentity);
821822
const onCheckoutCommandError = useCallback((error: Error) => {
@@ -847,7 +848,12 @@ export function PullRequestDetailPanel({
847848
() =>
848849
detail === null || detail.capabilities.stacks !== true || !supportsThreadPullRequests
849850
? null
850-
: { ...reference, host: reference.host ?? parseChangeRequestUrl(detail.url)?.host },
851+
: {
852+
...reference,
853+
host:
854+
reference.host ??
855+
parseChangeRequestUrl(detail.url, providerUrlHosts(detail.url))?.host,
856+
},
851857
[detail, reference, supportsThreadPullRequests],
852858
);
853859
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)