Skip to content

Commit 8f7a445

Browse files
committed
fix(source-control): preserve Forgejo ports in saved PR links
1 parent be7d20e commit 8f7a445

21 files changed

Lines changed: 432 additions & 41 deletions

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ describe("pull request toolkit handlers", () => {
284284
number: 42,
285285
});
286286
expect(result).toEqual({
287-
host: "forge.example",
287+
host: "forge.example:3000",
288288
repository: "git/owner/repo",
289289
number: 42,
290290
url: "http://forge.example:3000/git/owner/repo/pulls/42",
@@ -368,6 +368,18 @@ describe("pull request toolkit handlers", () => {
368368
}),
369369
);
370370

371+
it("reports an older Forgejo link's HTTP port when listing thread links", () => {
372+
const result = listThreadPullRequests(
373+
makeThread([
374+
makeLink(42, {
375+
host: "forge.example",
376+
url: "http://forge.example:3000/t3tools/t3code/pulls/42",
377+
}),
378+
]),
379+
);
380+
expect(result.pullRequests[0]?.host).toBe("forge.example:3000");
381+
});
382+
371383
it.effect("fails cleanly when the token's thread no longer exists", () =>
372384
Effect.gen(function* () {
373385
const harness = yield* makeHarness({ thread: null });

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "@t3tools/contracts";
1010
import { changeRequestUrlFor, parseChangeRequestUrl } from "@t3tools/shared/changeRequestUrl";
1111
import {
12+
normalizeThreadPullRequestKey,
1213
resolveThreadPullRequestChains,
1314
threadPullRequestKeyOf,
1415
visibleThreadPullRequests,
@@ -71,7 +72,7 @@ const resolveTarget = Effect.fn("PullRequestsToolkit.resolveTarget")(function* (
7172
if (parsed === null) {
7273
return yield* new PullRequestUrlInvalidError({});
7374
}
74-
return { ...parsed, url: input.url } satisfies ResolvedTarget;
75+
return { ...normalizeThreadPullRequestKey(parsed), url: input.url } satisfies ResolvedTarget;
7576
}
7677
if (input.repository === undefined || input.number === undefined) {
7778
return yield* new PullRequestTargetIncompleteError({});
@@ -91,7 +92,10 @@ const resolveTarget = Effect.fn("PullRequestsToolkit.resolveTarget")(function* (
9192
input.number,
9293
project?.repositoryIdentity?.locator.remoteUrl,
9394
) ?? `https://${host}/${repository}/pull/${input.number}`;
94-
return { host, repository, number: input.number, url } satisfies ResolvedTarget;
95+
return {
96+
...normalizeThreadPullRequestKey({ host, repository, number: input.number, url }),
97+
url,
98+
} satisfies ResolvedTarget;
9599
});
96100

97101
function entryOf(
@@ -109,7 +113,7 @@ function entryOf(
109113
}
110114
}
111115
return {
112-
host: link.host,
116+
host: normalizeThreadPullRequestKey(link).host,
113117
repository: link.repository,
114118
number: link.number,
115119
url: link.url,

‎apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,39 @@ it.layer(Layer.fresh(makeProjectionPipelinePrefixedTestLayer("t3-projection-pull
889889
assert.deepEqual(yield* readLinks(), []);
890890
assert.deepEqual(yield* readThreadUpdatedAt(), [{ updatedAt: "2026-01-01T00:00:05.000Z" }]);
891891

892+
// Older Forgejo rows stored a portless host; unlink by their URL's authority.
893+
yield* eventStore.append({
894+
...base("2026-01-01T00:00:05.100Z"),
895+
type: "thread.pull-request-linked",
896+
payload: {
897+
threadId,
898+
link: {
899+
host: "forge.example",
900+
repository: "team/repo",
901+
number: 42,
902+
url: "http://forge.example:3000/team/repo/pulls/42",
903+
source: "agent",
904+
linkedAt: "2026-01-01T00:00:05.100Z",
905+
snapshot: null,
906+
stack: null,
907+
},
908+
updatedAt: "2026-01-01T00:00:05.100Z",
909+
},
910+
});
911+
yield* eventStore.append({
912+
...base("2026-01-01T00:00:05.200Z"),
913+
type: "thread.pull-request-unlinked",
914+
payload: {
915+
threadId,
916+
host: "forge.example:3000",
917+
repository: "team/repo",
918+
number: 42,
919+
updatedAt: "2026-01-01T00:00:05.200Z",
920+
},
921+
});
922+
yield* projectionPipeline.bootstrap;
923+
assert.deepEqual(yield* readLinks(), []);
924+
892925
// Deleting the thread clears whatever links it still had.
893926
yield* eventStore.append({
894927
...base("2026-01-01T00:00:06.000Z"),

‎apps/server/src/orchestration/Layers/ProjectionPipeline.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -880,12 +880,20 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
880880
if (Option.isNone(existingRow)) {
881881
return;
882882
}
883-
yield* projectionThreadPullRequestRepository.delete({
883+
const links = yield* projectionThreadPullRequestRepository.listByThreadId({
884884
threadId: event.payload.threadId,
885-
host: event.payload.host.toLowerCase(),
886-
repository: event.payload.repository.toLowerCase(),
887-
number: event.payload.number,
888885
});
886+
const link = links.find((candidate) =>
887+
threadPullRequestKeysEqual(candidate, event.payload),
888+
);
889+
if (link !== undefined) {
890+
yield* projectionThreadPullRequestRepository.delete({
891+
threadId: event.payload.threadId,
892+
host: link.host,
893+
repository: link.repository,
894+
number: link.number,
895+
});
896+
}
889897
yield* projectionThreadRepository.upsert({
890898
...existingRow.value,
891899
updatedAt: event.payload.updatedAt,

‎apps/server/src/orchestration/PullRequestSyncReactor.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,31 @@ describe("PullRequestSyncReactor", () => {
420420
),
421421
);
422422

423+
it.effect("recovers the HTTP port when syncing an older Forgejo link", () =>
424+
Effect.scoped(
425+
Effect.gen(function* () {
426+
const fixture = yield* makeHarness({
427+
snapshot: makeSnapshot([
428+
makeThread("one", {
429+
pullRequests: [
430+
makeLink(42, null, {
431+
host: "forge.example",
432+
url: "http://forge.example:3000/owner/repository/pulls/42",
433+
}),
434+
],
435+
}),
436+
]),
437+
summary: (input) => Effect.succeed(makeSummary(input)),
438+
});
439+
yield* Effect.gen(function* () {
440+
yield* startAndSweep(fixture);
441+
assert.strictEqual((yield* Ref.get(fixture.summaryCalls))[0]?.host, "forge.example:3000");
442+
assert.strictEqual((yield* Ref.get(fixture.syncCommands))[0]?.host, "forge.example:3000");
443+
}).pipe(Effect.provide(fixture.layer));
444+
}),
445+
),
446+
);
447+
423448
it.effect("dispatches nothing when the host snapshot is unchanged", () =>
424449
Effect.scoped(
425450
Effect.gen(function* () {

‎apps/server/src/orchestration/PullRequestSyncReactor.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
1212
import {
1313
threadPullRequestKeyOf,
14+
normalizeThreadPullRequestKey,
1415
threadPullRequestKeysEqual,
1516
visibleThreadPullRequests,
1617
} from "@t3tools/shared/threadPullRequests";
@@ -191,7 +192,7 @@ export const make = Effect.gen(function* () {
191192
type: "thread.pull-request-link.sync",
192193
commandId: CommandId.make(`server:pr-sync:${thread.id}:${uuid}`),
193194
threadId: thread.id,
194-
host: link.host,
195+
host: normalizeThreadPullRequestKey(link).host,
195196
repository: link.repository,
196197
number: link.number,
197198
snapshot: { ...fields, syncedAt: nowIso },
@@ -200,7 +201,11 @@ export const make = Effect.gen(function* () {
200201
}
201202
if (fetchedStack === null || fetchedStack.stack === null) return;
202203
for (const layer of fetchedStack.stack.layers) {
203-
const layerKey = { host: link.host, repository: link.repository, number: layer.number };
204+
const layerKey = {
205+
host: normalizeThreadPullRequestKey(link).host,
206+
repository: link.repository,
207+
number: layer.number,
208+
};
204209
const dedupeKey = `${thread.id}:${threadPullRequestKeyOf(layerKey)}`;
205210
if (linkedThisSweep.has(dedupeKey)) continue;
206211
// Tombstones count as present: a dismissed layer is never re-added.
@@ -240,7 +245,7 @@ export const make = Effect.gen(function* () {
240245
const first = entries[0]!;
241246
const ref = {
242247
projectId: first.thread.projectId,
243-
host: first.link.host,
248+
host: normalizeThreadPullRequestKey(first.link).host,
244249
repository: first.link.repository,
245250
number: first.link.number,
246251
};

‎apps/server/src/orchestration/decider.pullRequests.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,49 @@ const snapshot: ThreadPullRequestSnapshot = {
115115
};
116116

117117
it.layer(NodeServices.layer)("pull request link decider", (it) => {
118+
it.effect("links the same Forgejo number on two ports and unlinks an older portless record", () =>
119+
Effect.gen(function* () {
120+
const existing = makeLink({
121+
host: "forge.example",
122+
url: "http://forge.example:3000/t3tools/t3code/pulls/42",
123+
});
124+
let model = makeReadModel([existing]);
125+
const command = yield* decodeCommand({
126+
type: "thread.pull-request.link",
127+
commandId: "link-other-port",
128+
threadId: THREAD_ID,
129+
host: "forge.example",
130+
repository: "t3tools/t3code",
131+
number: 42,
132+
url: "http://forge.example:4000/t3tools/t3code/pulls/42",
133+
source: "manual",
134+
});
135+
const linked = expectSingleEvent(
136+
yield* decideOrchestrationCommand({ readModel: model, command }),
137+
"thread.pull-request-linked",
138+
);
139+
expect(linked.payload.link.host).toBe("forge.example:4000");
140+
model = yield* projectEvent(model, { ...linked, sequence: 1 });
141+
expect(model.threads[0]!.pullRequests).toHaveLength(2);
142+
const unlink = yield* decodeCommand({
143+
type: "thread.pull-request.unlink",
144+
commandId: "unlink-old-port",
145+
threadId: THREAD_ID,
146+
host: "forge.example:3000",
147+
repository: "t3tools/t3code",
148+
number: 42,
149+
});
150+
const unlinked = expectSingleEvent(
151+
yield* decideOrchestrationCommand({ readModel: model, command: unlink }),
152+
"thread.pull-request-unlinked",
153+
);
154+
model = yield* projectEvent(model, { ...unlinked, sequence: 2 });
155+
expect(model.threads[0]!.pullRequests.map((link) => link.url)).toEqual([
156+
linked.payload.link.url,
157+
]);
158+
}),
159+
);
160+
118161
it.effect("legacy unlink cannot remove a newer cross-host link", () =>
119162
Effect.gen(function* () {
120163
const own = makeLink();

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,82 @@ it.effect("reads a host-native stack through the provider and null where it has
16601660
}),
16611661
);
16621662

1663+
it.effect("routes explicit Forgejo HTTP authorities through SSH checkouts after refinement", () =>
1664+
Effect.gen(function* () {
1665+
for (const provider of ["forgejo", "unknown"] as const) {
1666+
const seen: string[] = [];
1667+
const service = yield* makeService({
1668+
projects: [
1669+
project({
1670+
id: "ssh",
1671+
title: "ssh",
1672+
workspaceRoot: "/ssh",
1673+
repository: "team/repo",
1674+
provider,
1675+
host: "code.example",
1676+
remoteUrl: "[email protected]:team/repo.git",
1677+
}),
1678+
],
1679+
providers: [
1680+
fakeProvider("forgejo", {
1681+
getChangeRequestSummary: (input) =>
1682+
Effect.sync(() => {
1683+
seen.push(input.host);
1684+
return changeRequest(42, "2026-07-02T00:00:00Z");
1685+
}),
1686+
}),
1687+
],
1688+
resolveHandle: ({ context }) =>
1689+
Effect.succeed({
1690+
context: { ...context!, provider: { ...context!.provider, kind: "forgejo" } },
1691+
provider: undefined as never,
1692+
}),
1693+
});
1694+
yield* service.summary(
1695+
{
1696+
projectId: "ssh" as ProjectId,
1697+
host: "code.example:3000",
1698+
repository: "team/repo",
1699+
number: 42,
1700+
},
1701+
{ recoverTransientFailure: false },
1702+
);
1703+
assert.deepStrictEqual(seen, ["code.example:3000"]);
1704+
}
1705+
}),
1706+
);
1707+
1708+
it.effect("rejects a different Forgejo HTTP port for an HTTP checkout", () =>
1709+
Effect.gen(function* () {
1710+
const service = yield* makeService({
1711+
projects: [
1712+
project({
1713+
id: "http",
1714+
title: "http",
1715+
workspaceRoot: "/http",
1716+
repository: "team/repo",
1717+
provider: "forgejo",
1718+
host: "code.example",
1719+
remoteUrl: "http://code.example:4000/team/repo.git",
1720+
}),
1721+
],
1722+
providers: [fakeProvider("forgejo")],
1723+
});
1724+
const failure = yield* service
1725+
.summary(
1726+
{
1727+
projectId: "http" as ProjectId,
1728+
host: "code.example:3000",
1729+
repository: "team/repo",
1730+
number: 42,
1731+
},
1732+
{ recoverTransientFailure: false },
1733+
)
1734+
.pipe(Effect.flip);
1735+
assert.strictEqual(failure._tag, "PullRequestUnavailableError");
1736+
}),
1737+
);
1738+
16631739
it.effect("routes a hosted reference to another repository through a project on that host", () =>
16641740
Effect.gen(function* () {
16651741
const seen: Array<{ cwd: string; repository: string; host: string }> = [];

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
canonicalRepositoryKey,
3+
isSshRemoteUrl,
34
sourceControlRepositorySelector,
45
} from "@t3tools/shared/sourceControl";
56
import * as Cache from "effect/Cache";
@@ -560,7 +561,16 @@ export const make = Effect.gen(function* () {
560561
const host = pullRequestHostOf(identity, "unknown");
561562
// A legacy identity has no canonical host until its provider is refined, so it must reach
562563
// the refinement before a host filter can decide whether it belongs in the result.
563-
if (filter.host !== undefined && host !== "unknown" && host !== filter.host.toLowerCase()) {
564+
if (
565+
filter.host !== undefined &&
566+
host !== "unknown" &&
567+
host !== filter.host.toLowerCase() &&
568+
pullRequestHostOf(identity, "forgejo") !== filter.host.toLowerCase() &&
569+
!(
570+
isSshRemoteUrl(identity.locator.remoteUrl) &&
571+
host === filter.host.toLowerCase().replace(/:\d+$/u, "")
572+
)
573+
) {
564574
continue;
565575
}
566576
const { remoteName, remoteUrl } = identity.locator;
@@ -639,8 +649,18 @@ export const make = Effect.gen(function* () {
639649
const provider = detectSourceControlProviderFromRemoteUrl(identity.locator.remoteUrl);
640650
kind = provider === null ? kind : (refinedKinds.get(provider.baseUrl) ?? kind);
641651
}
642-
const host = pullRequestHostOf(identity, kind);
643-
if (filter.host !== undefined && host !== filter.host.toLowerCase()) continue;
652+
let host = pullRequestHostOf(identity, kind);
653+
if (filter.host !== undefined && host !== filter.host.toLowerCase()) {
654+
if (
655+
kind !== "forgejo" ||
656+
!isSshRemoteUrl(identity.locator.remoteUrl) ||
657+
host !== filter.host.toLowerCase().replace(/:\d+$/u, "")
658+
) {
659+
continue;
660+
}
661+
// SSH clone ports do not identify the web server; tea validates this HTTP authority.
662+
host = filter.host.toLowerCase();
663+
}
644664
const api = registry.get(kind);
645665
// Recorded before the de-duplication below, so the viewer lookup keeps the alternates
646666
// the listing is about to drop.

0 commit comments

Comments
 (0)