The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 03:15 UTC
fix(gitlab): merge messages drop agent credits (#30441)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Antony committed
Nov 5, 2026 at 22:15 UTC
938b4d45255fa92c11a03e0a5e946b2e438aabb3
5 files changed
+257
packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts
+158
@@ -437,6 +437,164 @@ layer("GitLabPullRequestCli.layer", (it) => {
437
}),
438
);
439
440
+ it.effect.each([
441
+ {
442
+ mergeMethod: "squash",
443
+ squashOnMerge: false,
444
+ squash: "Add thing\n\nCo-authored-by: Codex <noreply@openai.com>",
445
+ flags: [
446
+ "--squash",
447
+ "--sha",
448
+ "head",
449
+ "--message",
450
+ "Merge branch 'feature' into 'main'\n\nAdd thing\n\nCo-authored-by: Ada <ada@example.com>",
451
+ "--squash-message",
452
+ "Add thing",
453
+ ],
454
+ },
455
+ {
456
+ mergeMethod: "merge",
457
+ squashOnMerge: false,
458
+ squash: "Add thing\n\nCo-authored-by: Codex <noreply@openai.com>",
459
+ flags: [
460
+ "--sha",
461
+ "head",
462
+ "--message",
463
+ "Merge branch 'feature' into 'main'\n\nAdd thing\n\nCo-authored-by: Ada <ada@example.com>",
464
+ ],
465
+ },
466
+ {
467
+ mergeMethod: "merge",
468
+ squashOnMerge: true,
469
+ squash: "Add thing\n\nCo-authored-by: Codex <noreply@openai.com>",
470
+ flags: [
471
+ "--sha",
472
+ "head",
473
+ "--message",
474
+ "Merge branch 'feature' into 'main'\n\nAdd thing\n\nCo-authored-by: Ada <ada@example.com>",
475
+ "--squash",
476
+ "--squash-message",
477
+ "Add thing",
478
+ ],
479
+ },
480
+ {
481
+ mergeMethod: "squash",
482
+ squashOnMerge: false,
483
+ squash: "Co-authored-by: Codex <noreply@openai.com>",
484
+ flags: [
485
+ "--squash",
486
+ "--sha",
487
+ "head",
488
+ "--message",
489
+ "Merge branch 'feature' into 'main'\n\nAdd thing\n\nCo-authored-by: Ada <ada@example.com>",
490
+ "--squash-message",
491
+ "Add thing",
492
+ ],
493
+ },
494
+ ] as const)(
495
+ "removes agent credits from GitLab's default messages for a $mergeMethod (squash on merge: $squashOnMerge)",
496
+ ({ mergeMethod, squashOnMerge, squash, flags }) =>
497
+ Effect.gen(function* () {
498
+ const credits =
499
+ "Co-authored-by: Ada <ada@example.com>\nCo-authored-by: Claude <noreply@anthropic.com>";
500
+ mockedExecute.mockImplementation(({ args }) =>
501
+ Effect.succeed(
502
+ output(
503
+ args[1] === "graphql"
504
+ ? JSON.stringify({
505
+ data: {
506
+ project: {
507
+ mergeRequest: {
508
+ title: "Add thing",
509
+ squashOnMerge,
510
+ defaultMergeCommitMessage: `Merge branch 'feature' into 'main'\n\nAdd thing\n\n${credits}`,
511
+ defaultSquashCommitMessage: squash,
512
+ },
513
+ },
514
+ },
515
+ })
516
+ : args[0] === "api"
517
+ ? mergeRequestJson({
518
+ diff_refs: { base_sha: "base", head_sha: "head", start_sha: "start" },
519
+ })
520
+ : "",
521
+ ),
522
+ ),
523
+ );
524
+ const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
525
+
526
+ yield* cli.runMergeRequestAction({
527
+ cwd: "/w",
528
+ repository: "acme/web",
529
+ number: 7,
530
+ action: "merge",
531
+ mergeMethod,
532
+ removeAgentCreditsOnMerge: true,
533
+ });
534
+
535
+ expect(mockedExecute.mock.calls.at(-1)?.[0].args).toEqual([
536
+ "mr",
537
+ "merge",
538
+ "7",
539
+ "--repo",
540
+ "acme/web",
541
+ "--auto-merge=false",
542
+ "--yes",
543
+ ...flags,
544
+ ]);
545
+ }),
546
+ );
547
+
548
+ it.effect("keeps GitLab's merge messages when they carry no agent credits", () =>
549
+ Effect.gen(function* () {
550
+ mockedExecute.mockImplementation(({ args }) =>
551
+ Effect.succeed(
552
+ output(
553
+ args[1] === "graphql"
554
+ ? JSON.stringify({
555
+ data: {
556
+ project: {
557
+ mergeRequest: {
558
+ defaultMergeCommitMessage: "Merge branch 'feature' into 'main'",
559
+ defaultSquashCommitMessage: "Add thing",
560
+ },
561
+ },
562
+ },
563
+ })
564
+ : args[0] === "api"
565
+ ? mergeRequestJson({
566
+ diff_refs: { base_sha: "base", head_sha: "head", start_sha: "start" },
567
+ })
568
+ : "",
569
+ ),
570
+ ),
571
+ );
572
+ const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
573
+
574
+ yield* cli.runMergeRequestAction({
575
+ cwd: "/w",
576
+ repository: "acme/web",
577
+ number: 7,
578
+ action: "enable-auto-merge",
579
+ mergeMethod: "squash",
580
+ removeAgentCreditsOnMerge: true,
581
+ });
582
+
583
+ expect(mockedExecute.mock.calls.at(-1)?.[0].args).toEqual([
584
+ "mr",
585
+ "merge",
586
+ "7",
587
+ "--repo",
588
+ "acme/web",
589
+ "--auto-merge=true",
590
+ "--yes",
591
+ "--squash",
592
+ "--sha",
593
+ "head",
594
+ ]);
595
+ }),
596
+ );
597
+
598
it.effect.each(["merge", "enable-auto-merge"] as const)(
599
"does not run %s when GitLab has no diff revisions",
600
(action) =>
packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
+30
@@ -1,3 +1,4 @@
1
+import { removeAgentCredits } from "@t3tools/source-control-core/server/mergeMessage";
2
import { isCommitSha } from "@t3tools/source-control-core/server/pullRequestUtils";
3
import * as Context from "effect/Context";
4
import * as Effect from "effect/Effect";
@@ -35,6 +36,7 @@ import {
36
decodeCommitsJson,
37
decodeDiffRefsJson,
38
decodeDiscussionsJson,
39
+ decodeMergeMessagesJson,
40
decodeMergeRequestDetailJson,
41
decodeMergeRequestDiffsJson,
42
decodeMergeRequestChangesJson,
@@ -51,6 +53,7 @@ import {
53
decodeTriggerJobChecksJson,
54
decodeViewerJson,
55
gitLabAwardName,
56
+ MERGE_MESSAGES_GRAPHQL_QUERY,
57
MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY,
58
MERGE_REQUEST_STATS_GRAPHQL_QUERY,
59
REPOSITORY_BLOBS_GRAPHQL_QUERY,
@@ -386,6 +389,7 @@ export class GitLabPullRequestCli extends Context.Service<
389
readonly number: number;
390
readonly action: PullRequestAction;
391
readonly mergeMethod?: PullRequestMergeMethod;
392
+ readonly removeAgentCreditsOnMerge?: boolean;
393
}) => Effect.Effect<void, GitLabPullRequestCliError>;
394
395
readonly updateMergeRequest: (input: {
@@ -1672,6 +1676,32 @@ export const make = Effect.gen(function* () {
1676
}
1677
const refs = yield* getDiffRefs(input);
1678
flags.push("--sha", refs.headSha);
1679
+ if (input.removeAgentCreditsOnMerge === true) {
1680
+ // Read after the SHA guard, so a push since then fails the merge instead of
1681
+ // merging commits whose credits these messages don't cover.
1682
+ const messages = yield* graphqlRead({
1683
+ cwd: input.cwd,
1684
+ host: input.host,
1685
+ operation: "getMergeMessages",
1686
+ query: MERGE_MESSAGES_GRAPHQL_QUERY,
1687
+ variables: { fullPath: input.repository, iid: String(input.number) },
1688
+ decode: decodeMergeMessagesJson,
1689
+ });
1690
+ // glab drops an empty message, which would bring the credits back.
1691
+ const clean = (message: string) => {
1692
+ const cleaned = removeAgentCredits(message);
1693
+ return cleaned.trim() === "" ? (messages.title ?? message) : cleaned;
1694
+ };
1695
+ const merge = messages.merge === null ? null : clean(messages.merge);
1696
+ if (merge !== null && merge !== messages.merge) flags.push("--message", merge);
1697
+ const squashes = input.mergeMethod === "squash" || messages.squashOnMerge;
1698
+ const squash = messages.squash === null || !squashes ? null : clean(messages.squash);
1699
+ if (squash !== null && squash !== messages.squash) {
1700
+ // glab takes a squash message only alongside --squash, which changes nothing here.
1701
+ if (!flags.includes("--squash")) flags.push("--squash");
1702
+ flags.push("--squash-message", squash);
1703
+ }
1704
+ }
1705
}
1706
yield* executeMr(subcommand!, ...flags);
1707
},
packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts
+3
@@ -1,3 +1,4 @@
1
+import { removeAgentCredits } from "@t3tools/source-control-core/server/mergeMessage";
2
import { SourceControlProviderKind } from "@t3tools/contracts";
3
import * as Cache from "effect/Cache";
4
import * as Duration from "effect/Duration";
@@ -221,6 +222,7 @@ export const make = Effect.gen(function* () {
222
const provider: PullRequestProviderApi = {
223
kind: SourceControlProviderKind.make("gitlab"),
224
capabilities: CAPABILITIES,
225
+ mergeMessageRewrite: removeAgentCredits,
226
unavailableRequirement: UNAVAILABLE_REQUIREMENT,
227
228
getViewer: (input) => cli.getViewerUsername(input).pipe(Effect.mapError(fail("getViewer"))),
@@ -441,6 +443,7 @@ export const make = Effect.gen(function* () {
443
number: input.number,
444
action: input.action,
445
...(input.mergeMethod === undefined ? {} : { mergeMethod: input.mergeMethod }),
446
+ ...(input.removeAgentCreditsOnMerge === true ? { removeAgentCreditsOnMerge: true } : {}),
447
})
448
.pipe(Effect.mapError(fail("runAction"))),
449
),
packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts
+16
@@ -5,6 +5,7 @@ import {
5
decodeAwardEmojiJson,
6
decodeRepositoryBlobsJson,
7
decodeCommitsJson,
8
+ decodeMergeMessagesJson,
9
decodeMergeRequestDetailJson,
10
decodeMergeRequestDiffsJson,
11
decodeMergeRequestListJson,
@@ -791,3 +792,18 @@ describe("decodeMergeRequestStatsJson", () => {
792
expect(read(2).truncated).toBe(true);
793
});
794
});
795
+
796
+describe("decodeMergeMessagesJson", () => {
797
+ it("fails a partial response instead of reading errored fields as absent templates", () => {
798
+ const decoded = decodeMergeMessagesJson(
799
+ JSON.stringify({
800
+ errors: [{ message: "Internal server error", path: ["project", "mergeRequest"] }],
801
+ data: {
802
+ project: { mergeRequest: { title: "Add thing", defaultMergeCommitMessage: null } },
803
+ },
804
+ }),
805
+ );
806
+
807
+ expect(Result.isFailure(decoded)).toBe(true);
808
+ });
809
+});
packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts
+50
@@ -999,6 +999,56 @@ export function decodeRepositoryBlobsJson(
999
return Result.succeed(blobs);
1000
}
1001
1002
+export const MERGE_MESSAGES_GRAPHQL_QUERY = `query($fullPath: ID!, $iid: String!) {
1003
+ project(fullPath: $fullPath) {
1004
+ mergeRequest(iid: $iid) { title squashOnMerge defaultMergeCommitMessage defaultSquashCommitMessage }
1005
+ }
1006
+}`;
1007
+
1008
+const RawMergeMessagesSchema = Schema.Struct({
1009
+ // A field that errored reads as null, which must not pass for an absent template.
1010
+ errors: Schema.optionalKey(Schema.Never),
1011
+ data: Schema.Struct({
1012
+ project: Schema.NullOr(
1013
+ Schema.Struct({
1014
+ mergeRequest: Schema.NullOr(
1015
+ Schema.Struct({
1016
+ title: Schema.optional(Schema.NullOr(Schema.String)),
1017
+ squashOnMerge: Schema.optional(Schema.NullOr(Schema.Boolean)),
1018
+ defaultMergeCommitMessage: Schema.optional(Schema.NullOr(Schema.String)),
1019
+ defaultSquashCommitMessage: Schema.optional(Schema.NullOr(Schema.String)),
1020
+ }),
1021
+ ),
1022
+ }),
1023
+ ),
1024
+ }),
1025
+});
1026
+
1027
+const decodeMergeMessages = decodeJsonResult(RawMergeMessagesSchema);
1028
+
1029
+export interface GitLabMergeMessages {
1030
+ readonly title: string | null;
1031
+ readonly merge: string | null;
1032
+ readonly squash: string | null;
1033
+ /** GitLab squashes when the project always does or the merge request asks to. */
1034
+ readonly squashOnMerge: boolean;
1035
+}
1036
+
1037
+/** The messages GitLab would write from the project's merge and squash templates. */
1038
+export function decodeMergeMessagesJson(
1039
+ raw: string,
1040
+): Result.Result<GitLabMergeMessages, DecodeFailure> {
1041
+ return Result.map(decodeMergeMessages(raw), (decoded) => {
1042
+ const mergeRequest = decoded.data.project?.mergeRequest;
1043
+ return {
1044
+ title: mergeRequest?.title ?? null,
1045
+ merge: mergeRequest?.defaultMergeCommitMessage ?? null,
1046
+ squash: mergeRequest?.defaultSquashCommitMessage ?? null,
1047
+ squashOnMerge: mergeRequest?.squashOnMerge === true,
1048
+ };
1049
+ });
1050
+}
1051
+
1052
export const REVIEW_DECISIONS_GRAPHQL_QUERY = `query($fullPath: ID!, $iids: [String!]) {
1053
metadata { enterprise }
1054
project(fullPath: $fullPath) {