Skip to content

Commit 41b1795

Browse files
committed
fix(source-control): normalize mounted pull request repositories
1 parent 7d82310 commit 41b1795

2 files changed

Lines changed: 159 additions & 16 deletions

File tree

‎apps/server/src/pullRequest/PullRequestService.test.ts‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type {
2525
PullRequestReviewerCapabilities,
2626
} from "@t3tools/contracts";
2727
import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts/settings";
28+
import { parseChangeRequestUrl } from "@t3tools/shared/changeRequestUrl";
2829
import type { ServerSettings } from "@t3tools/contracts";
2930
import * as ServerSettingsService from "../serverSettings.ts";
3031
import { makeSettingsChanges } from "../testUtils/settingsChanges.ts";
@@ -658,6 +659,113 @@ it.effect.each(["forgejo", "azure-devops"] as const)(
658659
}),
659660
);
660661

662+
it.effect.each([
663+
{ mount: "/forge", repository: "forge/owner/repo", expected: "owner/repo" },
664+
{ mount: "/forge", repository: "forge/forge/repo", expected: "forge/repo" },
665+
{ mount: "/forge", repository: "forgeX/owner/repo", expected: "forgeX/owner/repo" },
666+
{ mount: "/Forge", repository: "Forge/owner/repo", expected: "owner/repo" },
667+
{ mount: "/forge/nested/", repository: "forge/nested/owner/repo", expected: "owner/repo" },
668+
{ mount: "/", repository: "owner/repo", expected: "owner/repo" },
669+
{ mount: undefined, repository: "owner/repo", expected: "owner/repo" },
670+
])(
671+
"routes linked $repository with mount '$mount' through normalized provider and cursor identities",
672+
({ mount, repository, expected }) =>
673+
Effect.gen(function* () {
674+
const host = "code.example.test";
675+
const baseUrl = mount === undefined ? undefined : `https://${host}${mount}`;
676+
const projects = [project({ id: "inbox", title: "Inbox", workspaceRoot: "/inbox" })];
677+
const seen: string[] = [];
678+
const viewed = environmentViewedProvider(new Map([["same.ts", "blob"]]), []);
679+
const service = yield* makeService({
680+
settings: {
681+
...DEFAULT_SERVER_SETTINGS,
682+
sourceControlHostOverrides: {
683+
[host]: { kind: "forgejo", ...(baseUrl === undefined ? {} : { baseUrl }) },
684+
},
685+
},
686+
projects,
687+
providers: [
688+
fakeProvider(SourceControlProviderKind.make("forgejo"), {
689+
capabilities: { ...viewed.capabilities, search: false },
690+
getFileRevisions: viewed.getFileRevisions!,
691+
getChangeRequestSummary: (input) =>
692+
Effect.gen(function* () {
693+
seen.push(input.repository);
694+
assert.strictEqual(yield* PullRequestBaseUrl, baseUrl?.replace(/\/+$/, ""));
695+
return changeRequest(input.number, "2026-07-02T00:00:00Z");
696+
}),
697+
listChangeRequests: (input) => {
698+
seen.push(input.repository);
699+
return Effect.succeed({
700+
items: [changeRequest(42, "2026-07-02T00:00:00Z")],
701+
truncated: true,
702+
continues: true,
703+
});
704+
},
705+
}),
706+
],
707+
});
708+
const link = parseChangeRequestUrl(`https://${host}/${repository}/pulls/42`)!;
709+
const ref = { ...link, projectId: "inbox" as ProjectId, repository };
710+
const summary = yield* service.summary(ref, { recoverTransientFailure: false });
711+
assert.strictEqual(summary.repository, expected);
712+
assert.deepStrictEqual(seen, [expected]);
713+
// A link's viewed-file identity must survive the later arrival of its checkout.
714+
yield* service.setFilesViewed({ ...ref, files: [{ path: "same.ts", viewed: true }] });
715+
assert.deepStrictEqual((yield* service.filesViewed(ref)).files, [
716+
{ path: "same.ts", state: "viewed" },
717+
]);
718+
projects.push(
719+
project({
720+
id: "other",
721+
title: "Another repository on the host",
722+
workspaceRoot: "/other",
723+
host,
724+
provider: "forgejo",
725+
repository: "another/repo",
726+
}),
727+
);
728+
const crossRepository = { ...ref, number: 43 };
729+
assert.strictEqual(
730+
(yield* service.summary(crossRepository, { recoverTransientFailure: false })).repository,
731+
expected,
732+
);
733+
assert.deepStrictEqual(seen, [expected, expected]);
734+
yield* service.setFilesViewed({
735+
...crossRepository,
736+
files: [{ path: "same.ts", viewed: true }],
737+
});
738+
projects.push(
739+
project({
740+
id: "checkout",
741+
title: "Mounted repository",
742+
workspaceRoot: "/checkout",
743+
host,
744+
provider: "forgejo",
745+
repository,
746+
}),
747+
);
748+
for (const number of [42, 43]) {
749+
assert.deepStrictEqual(
750+
(yield* service.filesViewed({ ...ref, projectId: "checkout" as ProjectId, number }))
751+
.files,
752+
[{ path: "same.ts", state: "viewed" }],
753+
);
754+
}
755+
const listed = yield* service.list({
756+
projectId: "checkout" as ProjectId,
757+
state: "open",
758+
involvement: "all",
759+
limit: 1,
760+
});
761+
assert.deepStrictEqual(listed.errors, []);
762+
assert.strictEqual(listed.entries[0]?.repository, expected);
763+
assert.deepStrictEqual(Object.keys(listed.nextCursors), [
764+
`${host} ${expected.toLowerCase()}`,
765+
]);
766+
}),
767+
);
768+
661769
it.effect("uses the configured provider's repository selector for a mapped Azure checkout", () =>
662770
Effect.gen(function* () {
663771
const service = yield* makeService({

‎apps/server/src/pullRequest/PullRequestService.ts‎

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ const routingCredential = Context.Reference<{
164164
readonly viewer: string;
165165
} | null>("t3/PullRequestService/routingCredential", { defaultValue: () => null });
166166
const credentialNamespace = Symbol("pullRequestCredentialNamespace");
167-
type CredentialRef = PullRequestRef & { readonly [credentialNamespace]?: string };
167+
const mountedRepository = Symbol("pullRequestMountedRepository");
168+
type CredentialRef = PullRequestRef & {
169+
readonly [credentialNamespace]?: string;
170+
readonly [mountedRepository]?: string;
171+
};
168172

169173
export class PullRequestService extends Context.Service<
170174
PullRequestService,
@@ -357,6 +361,13 @@ function listCursorKey(host: string, repository: string): string {
357361
return `${host} ${repository.toLowerCase()}`;
358362
}
359363

364+
function repositoryWithoutMount(repository: string, baseUrl: string | undefined): string {
365+
const mount = baseUrl === undefined ? "" : new URL(baseUrl).pathname.replace(/^\/+|\/+$/g, "");
366+
return mount && repository.startsWith(`${mount}/`)
367+
? repository.slice(mount.length + 1)
368+
: repository;
369+
}
370+
360371
/**
361372
* A slice that ends on the same instant it began on has to keep the earlier rows excluded as
362373
* well as its own, or the read after it would hand them over again.
@@ -765,12 +776,10 @@ export const make = Effect.gen(function* () {
765776
if (kind === undefined) continue;
766777
let repository = sourceControlRepositorySelector({ ...identity, provider: kind });
767778
if (repository === null) continue;
779+
const remoteRepository = repository;
768780
const baseUrl = override?.baseUrl === undefined ? undefined : refinedProvider?.baseUrl;
769781
if (baseUrl && /^https?:/i.test(identity.locator.remoteUrl)) {
770-
const mount = new URL(baseUrl).pathname.replace(/^\/+|\/+$/g, "");
771-
if (mount && repository.startsWith(`${mount}/`)) {
772-
repository = repository.slice(mount.length + 1);
773-
}
782+
repository = repositoryWithoutMount(repository, baseUrl);
774783
}
775784
const host =
776785
refinedProvider && (overridden || refinedProvider.kind === "forgejo")
@@ -808,7 +817,7 @@ export const make = Effect.gen(function* () {
808817
...(baseUrl === undefined ? {} : { baseUrl }),
809818
repository,
810819
host,
811-
remote: repositoryKey ?? normalizeGitRemoteUrl(`https://${host}/${repository}`),
820+
remote: repositoryKey ?? normalizeGitRemoteUrl(`https://${host}/${remoteRepository}`),
812821
repositoryKey:
813822
repositoryKey === null ? null : canonicalRepositoryKey(repositoryKey.toLowerCase()),
814823
});
@@ -825,9 +834,14 @@ export const make = Effect.gen(function* () {
825834
listWorkspaceProjects({ projectId: ref.projectId }).pipe(
826835
Effect.flatMap(({ supported }): Effect.Effect<SupportedProject, PullRequestError> => {
827836
const own = supported[0];
828-
const repository = ref.repository.trim();
837+
const repository = ((ref as CredentialRef)[mountedRepository] ?? ref.repository).trim();
829838
const host = ref.host?.trim().toLowerCase();
830-
if (own !== undefined && own.repository.toLowerCase() === repository.toLowerCase()) {
839+
if (
840+
own !== undefined &&
841+
(own.repository.toLowerCase() === repository.toLowerCase() ||
842+
own.repository.toLowerCase() ===
843+
repositoryWithoutMount(repository, own.baseUrl).toLowerCase())
844+
) {
831845
if (host === undefined || host === own.host) return Effect.succeed(own);
832846
}
833847
if (host === undefined) {
@@ -857,7 +871,9 @@ export const make = Effect.gen(function* () {
857871
onHost.find(
858872
(candidate) =>
859873
candidate.repositoryKey === null &&
860-
candidate.repository.toLowerCase() === repository.toLowerCase(),
874+
(candidate.repository.toLowerCase() === repository.toLowerCase() ||
875+
candidate.repository.toLowerCase() ===
876+
repositoryWithoutMount(repository, candidate.baseUrl).toLowerCase()),
861877
) ??
862878
onHost.find((candidate) => candidate.repositoryKey === null);
863879
if (route === undefined) {
@@ -891,6 +907,7 @@ export const make = Effect.gen(function* () {
891907
)?.baseUrl === undefined
892908
? undefined
893909
: provider?.baseUrl;
910+
const normalizedRepository = repositoryWithoutMount(repository, baseUrl);
894911
const api = provider === null ? null : registry.get(provider.kind);
895912
if (api !== null && provider?.kind !== "azure-devops") {
896913
const selected = yield* projects.getShell(ref.projectId).pipe(
@@ -905,7 +922,7 @@ export const make = Effect.gen(function* () {
905922
);
906923
if (Option.isSome(selected)) {
907924
return {
908-
cursorKey: listCursorKey(host, repository),
925+
cursorKey: listCursorKey(host, normalizedRepository),
909926
project: selected.value,
910927
api: withRateLimitBackoff(
911928
api,
@@ -914,7 +931,7 @@ export const make = Effect.gen(function* () {
914931
baseUrl === undefined ? undefined : { baseUrl },
915932
),
916933
...(baseUrl === undefined ? {} : { baseUrl }),
917-
repository,
934+
repository: normalizedRepository,
918935
host,
919936
remote: normalizeGitRemoteUrl(`https://${host}/${repository}`),
920937
repositoryKey: null,
@@ -924,13 +941,15 @@ export const make = Effect.gen(function* () {
924941
return yield* new PullRequestUnavailableError({ reason: "provider-unsupported" });
925942
});
926943
}
944+
const normalizedRepository = repositoryWithoutMount(repository, route.baseUrl);
927945
return Effect.succeed(
928946
route.repositoryKey !== null ||
929-
route.repository.toLowerCase() === repository.toLowerCase()
947+
route.repository.toLowerCase() === normalizedRepository.toLowerCase()
930948
? route
931949
: {
932950
...route,
933-
repository,
951+
cursorKey: listCursorKey(host, normalizedRepository),
952+
repository: normalizedRepository,
934953
remote: normalizeGitRemoteUrl(`https://${host}/${repository}`),
935954
},
936955
);
@@ -945,6 +964,12 @@ export const make = Effect.gen(function* () {
945964
const project = yield* requireProject(input);
946965
return {
947966
...input,
967+
// Cache loaders resolve the project again; retain the mounted path for its remote identity.
968+
...((input as CredentialRef)[mountedRepository] !== undefined ||
969+
(project.repository !== input.repository &&
970+
repositoryWithoutMount(input.repository, project.baseUrl) !== input.repository)
971+
? { [mountedRepository]: (input as CredentialRef)[mountedRepository] ?? input.repository }
972+
: {}),
948973
projectId: project.project.id,
949974
host: project.host,
950975
repository: project.repository,
@@ -2666,16 +2691,26 @@ export const make = Effect.gen(function* () {
26662691
ref.number,
26672692
ref.expectedAccountId ?? null,
26682693
ref[credentialNamespace] ?? null,
2694+
ref[mountedRepository] ?? null,
26692695
]);
26702696
const refOfCacheKey = (key: string): PullRequestRef => {
2671-
const [, projectId, host, repository, number, expectedAccountId, fingerprint] = JSON.parse(
2672-
key,
2673-
) as [number, string, string | null, string, number, string | null, string | null];
2697+
const [, projectId, host, repository, number, expectedAccountId, fingerprint, mounted] =
2698+
JSON.parse(key) as [
2699+
number,
2700+
string,
2701+
string | null,
2702+
string,
2703+
number,
2704+
string | null,
2705+
string | null,
2706+
string | null,
2707+
];
26742708
return {
26752709
projectId,
26762710
...(host === null ? {} : { host }),
26772711
...(expectedAccountId === null ? {} : { expectedAccountId }),
26782712
...(fingerprint === null ? {} : { [credentialNamespace]: fingerprint }),
2713+
...(mounted === null ? {} : { [mountedRepository]: mounted }),
26792714
repository,
26802715
number,
26812716
} as PullRequestRef;

0 commit comments

Comments
 (0)