Skip to content

Commit 6fd68f5

Browse files
authored
feat(source-control): support Forgejo and Gitea with fj and tea (#11436)
1 parent c299764 commit 6fd68f5

66 files changed

Lines changed: 4545 additions & 102 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/components/SourceControlIcon.tsx‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import Svg, { Defs, LinearGradient, Path, Stop } from "react-native-svg";
1+
import Svg, { Circle, Defs, G, LinearGradient, Path, Stop } from "react-native-svg";
22
import { withUniwind } from "uniwind";
33

44
const ThemedSvg = withUniwind(Svg);
55

6-
export type SourceControlIconKind = "github" | "gitlab" | "bitbucket" | "azure-devops";
6+
export type SourceControlIconKind = "github" | "gitlab" | "forgejo" | "bitbucket" | "azure-devops";
77

88
export function SourceControlIcon(props: {
99
readonly kind: SourceControlIconKind;
@@ -14,6 +14,19 @@ export function SourceControlIcon(props: {
1414
const size = props.size ?? 18;
1515

1616
switch (props.kind) {
17+
case "forgejo":
18+
// Official two-color mark from https://forgejo.org/favicon.svg.
19+
return (
20+
<Svg width={size} height={size} viewBox="0 0 212 212">
21+
<G transform="translate(6 6)" fill="none">
22+
<Path d="M58 168 v-98 a50 50 0 0 1 50-50 h20" stroke="#ff6600" strokeWidth={25} />
23+
<Path d="M58 168 v-30 a50 50 0 0 1 50-50 h20" stroke="#d40000" strokeWidth={25} />
24+
<Circle cx={142} cy={20} r={18} stroke="#ff6600" strokeWidth={15} />
25+
<Circle cx={142} cy={88} r={18} stroke="#d40000" strokeWidth={15} />
26+
<Circle cx={58} cy={180} r={18} stroke="#d40000" strokeWidth={15} />
27+
</G>
28+
</Svg>
29+
);
1730
case "github":
1831
return (
1932
<ThemedSvg

‎apps/mobile/src/features/projects/AddProjectRepositoryRoute.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function AddProjectRepositoryRoute({
1717
const title =
1818
source === "github" ||
1919
source === "gitlab" ||
20+
source === "forgejo" ||
2021
source === "bitbucket" ||
2122
source === "azure-devops"
2223
? addProjectRemoteSourceLabel(source)

‎apps/mobile/src/features/projects/AddProjectScreen.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ function sourceFromParam(value: string | string[] | undefined): AddProjectRemote
110110
source === "url" ||
111111
source === "github" ||
112112
source === "gitlab" ||
113+
source === "forgejo" ||
113114
source === "bitbucket" ||
114115
source === "azure-devops"
115116
) {

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
3737
import * as VcsProcess from "../vcs/VcsProcess.ts";
3838
import * as GitHubSourceControlProvider from "../sourceControl/GitHubSourceControlProvider.ts";
3939
import * as GitLabSourceControlProvider from "../sourceControl/GitLabSourceControlProvider.ts";
40+
import {
41+
ForgejoPullRequestSchema,
42+
toForgejoChangeRequest,
43+
} from "../sourceControl/forgejoPullRequests.ts";
4044
import type { SourceControlProvider } from "../sourceControl/SourceControlProvider.ts";
4145
import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
4246
import * as ServerConfig from "../config.ts";
@@ -46,6 +50,7 @@ import * as ServerSettings from "../serverSettings.ts";
4650
import * as GitManager from "./GitManager.ts";
4751

4852
const encodeCliJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
53+
const decodeForgejoPullRequest = Schema.decodeEffect(ForgejoPullRequestSchema);
4954

5055
interface FakeGhScenario {
5156
prListSequence?: string[];
@@ -3851,6 +3856,69 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
38513856
20_000,
38523857
);
38533858

3859+
it.effect("matches mounted Forgejo heads without confusing forks sharing a branch", () =>
3860+
Effect.gen(function* () {
3861+
for (const owner of ["maria", "reviewer"]) {
3862+
const mapped = toForgejoChangeRequest(
3863+
yield* decodeForgejoPullRequest({
3864+
number: 42,
3865+
title: "Greeting",
3866+
html_url: "https://forgejo.example/forgejo/maria/project/pulls/42",
3867+
state: "open",
3868+
merged: false,
3869+
base: {
3870+
ref: "main",
3871+
sha: "base",
3872+
repo: { full_name: "maria/project", owner: { login: "maria" } },
3873+
},
3874+
head: {
3875+
ref: "greeting",
3876+
sha: "head",
3877+
repo: { full_name: `${owner}/project`, owner: { login: owner } },
3878+
},
3879+
}),
3880+
);
3881+
const pr = {
3882+
...mapped,
3883+
isDraft: mapped.isDraft ?? false,
3884+
closedAt: mapped.closedAt ?? null,
3885+
mergedAt: mapped.mergedAt ?? null,
3886+
};
3887+
const repository = GitManager.parseRepositoryNameWithOwnerFromRemoteUrl(
3888+
`https://forgejo.example/forgejo/${owner}/project.git`,
3889+
"forgejo",
3890+
);
3891+
expect(repository).toBe(`${owner}/project`);
3892+
const context = {
3893+
headBranch: "greeting",
3894+
headRepositoryNameWithOwner: repository,
3895+
headRepositoryOwnerLogin: repository?.split("/")[0] ?? null,
3896+
isCrossRepository: owner !== "maria",
3897+
};
3898+
expect(GitManager.matchesBranchHeadContext(pr, context)).toBe(true);
3899+
expect(
3900+
GitManager.matchesBranchHeadContext(pr, {
3901+
...context,
3902+
headRepositoryNameWithOwner: "other/project",
3903+
headRepositoryOwnerLogin: "other",
3904+
}),
3905+
).toBe(false);
3906+
}
3907+
expect(
3908+
GitManager.parseRepositoryNameWithOwnerFromRemoteUrl(
3909+
"[email protected]:maria/project.git",
3910+
"forgejo",
3911+
),
3912+
).toBe("maria/project");
3913+
expect(
3914+
GitManager.parseRepositoryNameWithOwnerFromRemoteUrl(
3915+
"https://gitlab.example/group/maria/project.git",
3916+
"gitlab",
3917+
),
3918+
).toBe("group/maria/project");
3919+
}),
3920+
);
3921+
38543922
it.effect("rejects same-repo PR metadata when matching a cross-repo head context", () =>
38553923
Effect.sync(() => {
38563924
const headContext = {

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,10 @@ function resolvePullRequestWorktreeLocalBranchName(
277277
return `t3code/pr-${pullRequest.number}/${suffix}`;
278278
}
279279

280-
function parseRepositoryNameWithOwnerFromRemoteUrl(url: string | null): string | null {
280+
export function parseRepositoryNameWithOwnerFromRemoteUrl(
281+
url: string | null,
282+
providerKind?: ChangeRequest["provider"],
283+
): string | null {
281284
const trimmed = url?.trim() ?? "";
282285
if (trimmed.length === 0) {
283286
return null;
@@ -288,6 +291,12 @@ function parseRepositoryNameWithOwnerFromRemoteUrl(url: string | null): string |
288291
trimmed,
289292
);
290293
const repositoryNameWithOwner = match?.[1]?.trim() ?? "";
294+
// Forgejo HTTP paths can include an installation mount; its API always names owner/repo.
295+
if (providerKind === "forgejo" && /^https?:\/\//iu.test(trimmed)) {
296+
return repositoryNameWithOwner.length > 0
297+
? repositoryNameWithOwner.split("/").slice(-2).join("/")
298+
: null;
299+
}
291300
return repositoryNameWithOwner.length > 0 ? repositoryNameWithOwner : null;
292301
}
293302

@@ -1291,7 +1300,15 @@ export const make = Effect.gen(function* () {
12911300
(yield* readConfigValueNullable(cwd, `remote.${preferredRemoteName}.url`)) ??
12921301
(yield* readConfigValueNullable(cwd, "remote.origin.url"));
12931302

1294-
return remoteUrl ? detectSourceControlProviderFromGitRemoteUrl(remoteUrl) : null;
1303+
const provider = remoteUrl ? detectSourceControlProviderFromGitRemoteUrl(remoteUrl) : null;
1304+
if (!remoteUrl || provider?.kind !== "unknown") return provider;
1305+
const handle = yield* sourceControlProviders
1306+
.resolveHandle({
1307+
cwd,
1308+
context: { provider, remoteName: preferredRemoteName, remoteUrl },
1309+
})
1310+
.pipe(Effect.orElseSucceed(() => null));
1311+
return handle?.context?.provider ?? provider;
12951312
});
12961313

12971314
const resolveRemoteRepositoryContext = Effect.fn("resolveRemoteRepositoryContext")(function* (
@@ -1307,7 +1324,22 @@ export const make = Effect.gen(function* () {
13071324
}
13081325

13091326
const remoteUrl = yield* readConfigValueNullable(cwd, `remote.${remoteName}.url`);
1310-
const repositoryNameWithOwner = parseRepositoryNameWithOwnerFromRemoteUrl(remoteUrl);
1327+
let repositoryNameWithOwner = parseRepositoryNameWithOwnerFromRemoteUrl(remoteUrl);
1328+
if (
1329+
remoteUrl !== null &&
1330+
/^https?:\/\//iu.test(remoteUrl) &&
1331+
(repositoryNameWithOwner?.split("/").length ?? 0) > 2
1332+
) {
1333+
const detected = detectSourceControlProviderFromGitRemoteUrl(remoteUrl);
1334+
const kind =
1335+
detected?.kind === "unknown"
1336+
? yield* sourceControlProvider(cwd).pipe(
1337+
Effect.map((provider) => provider.kind),
1338+
Effect.orElseSucceed(() => undefined),
1339+
)
1340+
: detected?.kind;
1341+
repositoryNameWithOwner = parseRepositoryNameWithOwnerFromRemoteUrl(remoteUrl, kind);
1342+
}
13111343
return {
13121344
remoteUrlKey: remoteUrl ? normalizeGitRemoteUrl(remoteUrl) : null,
13131345
repositoryNameWithOwner,

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,40 @@ describe("pull request toolkit handlers", () => {
265265
}),
266266
);
267267

268+
it.effect("links a numeric Forgejo reference with its remote's web origin and mount path", () =>
269+
Effect.gen(function* () {
270+
const harness = yield* makeHarness({
271+
project: makeProject({
272+
canonicalKey: "forge.example/git/owner/repo",
273+
locator: {
274+
source: "git-remote",
275+
remoteName: "origin",
276+
remoteUrl: "http://forge.example:3000/git/owner/repo.git",
277+
},
278+
provider: "forgejo",
279+
displayName: "git/owner/repo",
280+
}),
281+
});
282+
const result = yield* harness.call("link_pull_request", {
283+
repository: "git/owner/repo",
284+
number: 42,
285+
});
286+
expect(result).toEqual({
287+
host: "forge.example:3000",
288+
repository: "git/owner/repo",
289+
number: 42,
290+
url: "http://forge.example:3000/git/owner/repo/pulls/42",
291+
alreadyLinked: false,
292+
});
293+
const other = yield* harness.call("link_pull_request", {
294+
host: "other.example",
295+
repository: "owner/repo",
296+
number: 42,
297+
});
298+
expect(other.url).toBe("https://other.example/owner/repo/pull/42");
299+
}),
300+
);
301+
268302
it.effect("rejects a target that names neither a URL nor repository and number", () =>
269303
Effect.gen(function* () {
270304
const harness = yield* makeHarness();
@@ -334,6 +368,18 @@ describe("pull request toolkit handlers", () => {
334368
}),
335369
);
336370

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+
337383
it.effect("fails cleanly when the token's thread no longer exists", () =>
338384
Effect.gen(function* () {
339385
const harness = yield* makeHarness({ thread: null });

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

Lines changed: 8 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({});
@@ -89,8 +90,12 @@ const resolveTarget = Effect.fn("PullRequestsToolkit.resolveTarget")(function* (
8990
host,
9091
repository,
9192
input.number,
93+
project?.repositoryIdentity?.locator.remoteUrl,
9294
) ?? `https://${host}/${repository}/pull/${input.number}`;
93-
return { host, repository, number: input.number, url } satisfies ResolvedTarget;
95+
return {
96+
...normalizeThreadPullRequestKey({ host, repository, number: input.number, url }),
97+
url,
98+
} satisfies ResolvedTarget;
9499
});
95100

96101
function entryOf(
@@ -108,7 +113,7 @@ function entryOf(
108113
}
109114
}
110115
return {
111-
host: link.host,
116+
host: normalizeThreadPullRequestKey(link).host,
112117
repository: link.repository,
113118
number: link.number,
114119
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* () {

0 commit comments

Comments
 (0)