The best way to control Claude Code, Codex and more from any computer or phone
fix(gitlab): create fork merge requests in the source project (#23439)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Aditya Garud committed
Nov 14, 2026 at 13:58 UTC
410177c7d6d2c9fd356c7d968ede0e126ea20b49
5 files changed
+220
-78
apps/server/src/git/GitManager.test.ts
+83
-65
@@ -56,7 +56,10 @@ import * as VcsProcess from "../vcs/VcsProcess.ts";
56
import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
57
import * as VcsProjectConfig from "../vcs/VcsProjectConfig.ts";
58
import * as GitWorkflowService from "./GitWorkflowService.ts";
59
-import type { SourceControlProvider } from "@t3tools/source-control-core/server/SourceControlProvider";
59
+import type {
60
+ SourceControlProvider,
61
+ SourceControlRefSelector,
62
+} from "@t3tools/source-control-core/server/SourceControlProvider";
63
import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
64
import * as ServerConfig from "../config.ts";
65
import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts";
@@ -111,6 +114,7 @@ type FakeProviderCall =
114
}
115
| {
116
readonly operation: "createChangeRequest";
117
+ readonly source: SourceControlRefSelector | undefined;
118
readonly baseRefName: string;
119
readonly headSelector: string;
120
readonly title: string;
@@ -500,6 +504,7 @@ function createFakeSourceControlProvider(scenario: FakeProviderScenario = {}): {
504
createChangeRequest: (input) =>
505
record(input.cwd, {
506
operation: "createChangeRequest",
507
+ source: input.source,
508
baseRefName: input.baseRefName,
509
headSelector: input.headSelector,
510
title: input.title,
@@ -4560,75 +4565,88 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
4565
}),
4566
);
4567
4563
- it.effect("creates cross-repo PRs with the fork owner selector and default base branch", () =>
4564
- Effect.gen(function* () {
4565
- const repoDir = yield* makeTempDir("t3code-git-manager-");
4566
- yield* initRepo(repoDir);
4567
- const forkDir = yield* createBareRemote();
4568
- yield* runGit(repoDir, ["remote", "add", "fork-seed", forkDir]);
4569
- // A lone remote would be treated as the base repository.
4570
- yield* runGit(repoDir, [
4571
- "remote",
4572
- "add",
4573
- "origin",
4574
- "git@github.com:pingdotgg/codething-mvp.git",
4575
- ]);
4576
- yield* runGit(repoDir, ["checkout", "-b", "statemachine"]);
4577
- NodeFS.writeFileSync(NodePath.join(repoDir, "changes.txt"), "change\n");
4578
- yield* runGit(repoDir, ["add", "changes.txt"]);
4579
- yield* runGit(repoDir, ["commit", "-m", "Feature commit"]);
4580
- yield* runGit(repoDir, ["push", "-u", "fork-seed", "statemachine"]);
4581
- yield* runGit(repoDir, ["checkout", "-b", "t3code/pr-91/statemachine"]);
4582
- yield* runGit(repoDir, ["branch", "--set-upstream-to", "fork-seed/statemachine"]);
4583
- yield* configureVisibleRemoteUrlWithLocalRewrite(
4584
- repoDir,
4585
- "fork-seed",
4586
- "git@github.com:octocat/codething-mvp.git",
4587
- forkDir,
4588
- );
4568
+ it.effect.each(["github", "gitlab", "bitbucket"] as const)(
4569
+ "creates fork requests with provider-specific source identity (%s)",
4570
+ (kind) =>
4571
+ Effect.gen(function* () {
4572
+ const sourceRepository =
4573
+ kind === "gitlab" ? "octocat/subgroup/renamed-fork" : "octocat/renamed-fork";
4574
+ const repoDir = yield* makeTempDir("t3code-git-manager-");
4575
+ yield* initRepo(repoDir);
4576
+ const forkDir = yield* createBareRemote();
4577
+ yield* runGit(repoDir, ["remote", "add", "fork-seed", forkDir]);
4578
+ // A lone remote would be treated as the base repository.
4579
+ yield* runGit(repoDir, [
4580
+ "remote",
4581
+ "add",
4582
+ "origin",
4583
+ `git@${kind}.com:pingdotgg/codething-mvp.git`,
4584
+ ]);
4585
+ yield* runGit(repoDir, ["checkout", "-b", "statemachine"]);
4586
+ NodeFS.writeFileSync(NodePath.join(repoDir, "changes.txt"), "change\n");
4587
+ yield* runGit(repoDir, ["add", "changes.txt"]);
4588
+ yield* runGit(repoDir, ["commit", "-m", "Feature commit"]);
4589
+ yield* runGit(repoDir, ["push", "-u", "fork-seed", "statemachine"]);
4590
+ yield* runGit(repoDir, ["checkout", "-b", "t3code/pr-91/statemachine"]);
4591
+ yield* runGit(repoDir, ["branch", "--set-upstream-to", "fork-seed/statemachine"]);
4592
+ yield* configureVisibleRemoteUrlWithLocalRewrite(
4593
+ repoDir,
4594
+ "fork-seed",
4595
+ `git@${kind}.com:${sourceRepository}.git`,
4596
+ forkDir,
4597
+ );
4598
4590
- const { manager, providerCalls } = yield* makeManager({
4591
- providerScenario: {
4592
- prListSequenceByHeadSelector: {
4593
- statemachine: [
4594
- [],
4595
- [
4596
- {
4597
- number: 188,
4598
- title: "Add stacked git actions",
4599
- url: "https://github.com/pingdotgg/codething-mvp/pull/188",
4600
- baseRefName: "main",
4601
- headRefName: "statemachine",
4602
- state: "open",
4603
- isCrossRepository: true,
4604
- headRepositoryNameWithOwner: "octocat/codething-mvp",
4605
- headRepositoryOwnerLogin: "octocat",
4606
- },
4599
+ const { manager, providerCalls } = yield* makeManager({
4600
+ providerScenario: {
4601
+ kind,
4602
+ prListSequenceByHeadSelector: {
4603
+ statemachine: [
4604
+ [],
4605
+ [
4606
+ {
4607
+ number: 188,
4608
+ title: "Add stacked git actions",
4609
+ url: "https://github.com/pingdotgg/codething-mvp/pull/188",
4610
+ baseRefName: "main",
4611
+ headRefName: "statemachine",
4612
+ state: "open",
4613
+ isCrossRepository: true,
4614
+ headRepositoryNameWithOwner: sourceRepository,
4615
+ headRepositoryOwnerLogin: "octocat",
4616
+ },
4617
+ ],
4618
],
4608
- ],
4619
+ },
4620
},
4610
- },
4611
- });
4621
+ });
4622
4613
- const result = yield* runStackedAction(manager, {
4614
- cwd: repoDir,
4615
- action: "commit_push_pr",
4616
- });
4623
+ const result = yield* runStackedAction(manager, {
4624
+ cwd: repoDir,
4625
+ action: "commit_push_pr",
4626
+ });
4627
4618
- expect(result.pr.status).toBe("created");
4619
- expect(result.pr.number).toBe(188);
4620
- expect(
4621
- created(providerCalls).some(
4622
- (call) => call.baseRefName === "main" && call.headSelector === "octocat:statemachine",
4623
- ),
4624
- ).toBe(true);
4625
- expect(
4626
- created(providerCalls).some(
4627
- (call) =>
4628
- call.baseRefName === "statemachine" && call.headSelector === "octocat:statemachine",
4629
- ),
4630
- ).toBe(false);
4631
- }),
4628
+ expect(result.pr.status).toBe("created");
4629
+ expect(result.pr.number).toBe(188);
4630
+ expect(
4631
+ providerCalls.find((call) => call.operation === "createChangeRequest"),
4632
+ ).toMatchObject({
4633
+ source:
4634
+ kind === "gitlab"
4635
+ ? { repository: sourceRepository, refName: "statemachine" }
4636
+ : undefined,
4637
+ });
4638
+ expect(
4639
+ created(providerCalls).some(
4640
+ (call) => call.baseRefName === "main" && call.headSelector === "octocat:statemachine",
4641
+ ),
4642
+ ).toBe(true);
4643
+ expect(
4644
+ created(providerCalls).some(
4645
+ (call) =>
4646
+ call.baseRefName === "statemachine" && call.headSelector === "octocat:statemachine",
4647
+ ),
4648
+ ).toBe(false);
4649
+ }),
4650
);
4651
4652
it.effect("creates a fork PR from origin into the upstream remote the provider targets", () =>
apps/server/src/git/GitManager.ts
+10
@@ -2474,6 +2474,16 @@ export const make = Effect.gen(function* () {
2474
cwd,
2475
baseRefName: baseBranch,
2476
headSelector: headContext.preferredHeadSelector,
2477
+ ...(provider.kind === "gitlab" &&
2478
+ headContext.isCrossRepository &&
2479
+ headContext.headRepositoryNameWithOwner
2480
+ ? {
2481
+ source: {
2482
+ repository: headContext.headRepositoryNameWithOwner,
2483
+ refName: headContext.headBranch,
2484
+ },
2485
+ }
2486
+ : {}),
2487
title: generated.title,
2488
bodyFile,
2489
})
packages/source-control-gitlab/src/server/GitLabCli.test.ts
+32
@@ -166,6 +166,38 @@ layer("GitLabCli.layer", (it) => {
166
}),
167
);
168
169
+ it.effect("creates same-project MRs without a repository lookup", () =>
170
+ Effect.gen(function* () {
171
+ mockedRun.mockReturnValueOnce(Effect.succeed(processOutput("{}")));
172
+ const glab = yield* GitLabCli.GitLabCli;
173
+ yield* glab.createMergeRequest({
174
+ cwd: "/repo",
175
+ baseBranch: "main",
176
+ headSelector: "feature/fix",
177
+ title: "Fix remote flow",
178
+ bodyFile: "/tmp/body.md",
179
+ });
180
+ expect(mockedRun).toHaveBeenCalledExactlyOnceWith(
181
+ expect.objectContaining({
182
+ args: [
183
+ "api",
184
+ "--method",
185
+ "POST",
186
+ "projects/:fullpath/merge_requests",
187
+ "--raw-field",
188
+ "source_branch=feature/fix",
189
+ "--raw-field",
190
+ "target_branch=main",
191
+ "--raw-field",
192
+ "title=Fix remote flow",
193
+ "--field",
194
+ "description=@/tmp/body.md",
195
+ ],
196
+ }),
197
+ );
198
+ }),
199
+ );
200
+
201
it.effect("surfaces a friendly error when the merge request is not found", () =>
202
Effect.gen(function* () {
203
const cause = new VcsProcessExitError({
packages/source-control-gitlab/src/server/GitLabCli.ts
+37
-13
@@ -196,7 +196,12 @@ export class GitLabRepositoryDecodeError extends Schema.TaggedError<GitLabReposi
196
"GitLabRepositoryDecodeError",
197
{
198
...gitLabCliDecodeErrorContext,
199
- operation: Schema.Literals(["getRepositoryCloneUrls", "createRepository", "getDefaultBranch"]),
199
+ operation: Schema.Literals([
200
+ "getRepositoryCloneUrls",
201
+ "createRepository",
202
+ "getDefaultBranch",
203
+ "createMergeRequest",
204
+ ]),
205
repository: Schema.optional(Schema.String),
206
},
207
) {
@@ -332,6 +337,10 @@ const RawGitLabDefaultBranchSchema = Schema.Struct({
337
default_branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
338
});
339
340
+const decodeGitLabMergeRequestProject = Schema.decodeEffect(
341
+ Schema.fromJsonString(Schema.Struct({ id: Schema.Number, path: TrimmedNonEmptyString })),
342
+);
343
+
344
const RawGitLabNamespaceSchema = Schema.Struct({
345
id: Schema.Number,
346
});
@@ -380,12 +389,6 @@ function sourceRefName(input: {
389
return input.source?.refName ?? normalizeHeadSelector(input.headSelector);
390
}
391
383
-function sourceProjectIdentifier(
384
- source: SourceControlProvider.SourceControlRefSelector | undefined,
385
-): string | null {
386
- return source?.repository ?? source?.owner ?? null;
387
-}
388
-
392
function toSummaryWithOptionalUpdatedAt(
393
record: GitLabMergeRequestSummary & {
394
readonly updatedAt: Option.Option<DateTime.Utc>;
@@ -631,26 +634,47 @@ export const make = Effect.gen(function* () {
634
Effect.map(normalizeRepositoryCloneUrls),
635
);
636
}),
634
- createMergeRequest: Effect.fn("GitLabCli.createMergeRequest")(function (input) {
635
- const sourceProject = sourceProjectIdentifier(input.source);
636
- return execute({
637
+ createMergeRequest: Effect.fn("GitLabCli.createMergeRequest")(function* (input) {
638
+ let project = ":fullpath";
639
+ let targetProjectId: number | undefined;
640
+ if (input.source?.repository || input.source?.owner) {
641
+ const result = yield* execute({ cwd: input.cwd, args: ["api", "projects/:fullpath"] });
642
+ const targetProject = yield* decodeGitLabMergeRequestProject(result.stdout).pipe(
643
+ Effect.mapError(
644
+ (cause) =>
645
+ new GitLabRepositoryDecodeError({
646
+ operation: "createMergeRequest",
647
+ command: "glab",
648
+ cwd: input.cwd,
649
+ cause,
650
+ }),
651
+ ),
652
+ );
653
+ project = encodeURIComponent(
654
+ input.source.repository ?? `${input.source.owner}/${targetProject.path}`,
655
+ );
656
+ targetProjectId = targetProject.id;
657
+ }
658
+ yield* execute({
659
cwd: input.cwd,
660
args: [
661
"api",
662
"--method",
663
"POST",
642
- "projects/:fullpath/merge_requests",
664
+ `projects/${project}/merge_requests`,
665
"--raw-field",
666
`source_branch=${sourceRefName(input)}`,
667
"--raw-field",
668
`target_branch=${input.target?.refName ?? input.baseBranch}`,
647
- ...(sourceProject ? ["--raw-field", `source_project_id=${sourceProject}`] : []),
669
+ ...(targetProjectId === undefined
670
+ ? []
671
+ : ["--raw-field", `target_project_id=${targetProjectId}`]),
672
"--raw-field",
673
`title=${input.title}`,
674
"--field",
675
`description=@${input.bodyFile}`,
676
],
653
- }).pipe(Effect.asVoid);
677
+ });
678
}),
679
getDefaultBranch: Effect.fn("GitLabCli.getDefaultBranch")(function (input) {
680
return execute({
packages/source-control-gitlab/src/server/GitLabSourceControlProvider.test.ts
+58
@@ -281,3 +281,61 @@ it.effect.each(["read", "decode"] as const)(
281
else assert.propertyVal(error.cause, "_tag", "SchemaError");
282
}),
283
);
284
+
285
+it.effect.each([
286
+ { headSelector: "octocat:feature/fix", source: undefined, sourceProject: "octocat/t3code" },
287
+ {
288
+ headSelector: "feature/fix",
289
+ source: { owner: "team/subgroup", refName: "feature/fix" },
290
+ sourceProject: "team/subgroup/t3code",
291
+ },
292
+ {
293
+ headSelector: "feature/fix",
294
+ source: { repository: "octocat/renamed-fork", refName: "feature/fix" },
295
+ sourceProject: "octocat/renamed-fork",
296
+ },
297
+])("creates a fork MR from $sourceProject into the current project", (testCase) =>
298
+ Effect.gen(function* () {
299
+ const requests: ReadonlyArray<string>[] = [];
300
+ const host = TestSourceControlHost.layer({
301
+ process: {
302
+ run: (input) => {
303
+ requests.push(input.args);
304
+ if (input.args[1] === "projects/:fullpath") {
305
+ return Effect.succeed(processOutput(JSON.stringify({ id: 100, path: "t3code" })));
306
+ }
307
+ assert.deepStrictEqual(input.args, [
308
+ "api",
309
+ "--method",
310
+ "POST",
311
+ `projects/${encodeURIComponent(testCase.sourceProject)}/merge_requests`,
312
+ "--raw-field",
313
+ "source_branch=feature/fix",
314
+ "--raw-field",
315
+ "target_branch=main",
316
+ "--raw-field",
317
+ "target_project_id=100",
318
+ "--raw-field",
319
+ "title=Fix remote flow",
320
+ "--field",
321
+ "description=@/tmp/body.md",
322
+ ]);
323
+ return Effect.succeed(processOutput("{}"));
324
+ },
325
+ },
326
+ });
327
+ const provider = yield* GitLabSourceControlProvider.make.pipe(
328
+ Effect.provide(Layer.merge(host, GitLabCli.layer.pipe(Layer.provide(host)))),
329
+ );
330
+ yield* provider.createChangeRequest({
331
+ cwd: "/repo",
332
+ baseRefName: "main",
333
+ headSelector: testCase.headSelector,
334
+ ...(testCase.source ? { source: testCase.source } : {}),
335
+ title: "Fix remote flow",
336
+ bodyFile: "/tmp/body.md",
337
+ });
338
+ assert.strictEqual(requests.length, 2);
339
+ assert.deepStrictEqual(requests[0], ["api", "projects/:fullpath"]);
340
+ }),
341
+);