Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,164 @@ layer("GitLabPullRequestCli.layer", (it) => {
}),
);

it.effect.each([
{
mergeMethod: "squash",
squashOnMerge: false,
squash: "Add thing\n\nCo-authored-by: Codex <noreply@openai.com>",
flags: [
"--squash",
"--sha",
"head",
"--message",
"Merge branch 'feature' into 'main'\n\nAdd thing\n\nCo-authored-by: Ada <ada@example.com>",
"--squash-message",
"Add thing",
],
},
{
mergeMethod: "merge",
squashOnMerge: false,
squash: "Add thing\n\nCo-authored-by: Codex <noreply@openai.com>",
flags: [
"--sha",
"head",
"--message",
"Merge branch 'feature' into 'main'\n\nAdd thing\n\nCo-authored-by: Ada <ada@example.com>",
],
},
{
mergeMethod: "merge",
squashOnMerge: true,
squash: "Add thing\n\nCo-authored-by: Codex <noreply@openai.com>",
flags: [
"--sha",
"head",
"--message",
"Merge branch 'feature' into 'main'\n\nAdd thing\n\nCo-authored-by: Ada <ada@example.com>",
"--squash",
"--squash-message",
"Add thing",
],
},
{
mergeMethod: "squash",
squashOnMerge: false,
squash: "Co-authored-by: Codex <noreply@openai.com>",
flags: [
"--squash",
"--sha",
"head",
"--message",
"Merge branch 'feature' into 'main'\n\nAdd thing\n\nCo-authored-by: Ada <ada@example.com>",
"--squash-message",
"Add thing",
],
},
] as const)(
"removes agent credits from GitLab's default messages for a $mergeMethod (squash on merge: $squashOnMerge)",
({ mergeMethod, squashOnMerge, squash, flags }) =>
Effect.gen(function* () {
const credits =
"Co-authored-by: Ada <ada@example.com>\nCo-authored-by: Claude <noreply@anthropic.com>";
mockedExecute.mockImplementation(({ args }) =>
Effect.succeed(
output(
args[1] === "graphql"
? JSON.stringify({
data: {
project: {
mergeRequest: {
title: "Add thing",
squashOnMerge,
defaultMergeCommitMessage: `Merge branch 'feature' into 'main'\n\nAdd thing\n\n${credits}`,
defaultSquashCommitMessage: squash,
},
},
},
})
: args[0] === "api"
? mergeRequestJson({
diff_refs: { base_sha: "base", head_sha: "head", start_sha: "start" },
})
: "",
),
),
);
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;

yield* cli.runMergeRequestAction({
cwd: "/w",
repository: "acme/web",
number: 7,
action: "merge",
mergeMethod,
removeAgentCreditsOnMerge: true,
});

expect(mockedExecute.mock.calls.at(-1)?.[0].args).toEqual([
"mr",
"merge",
"7",
"--repo",
"acme/web",
"--auto-merge=false",
"--yes",
...flags,
]);
}),
);

it.effect("keeps GitLab's merge messages when they carry no agent credits", () =>
Effect.gen(function* () {
mockedExecute.mockImplementation(({ args }) =>
Effect.succeed(
output(
args[1] === "graphql"
? JSON.stringify({
data: {
project: {
mergeRequest: {
defaultMergeCommitMessage: "Merge branch 'feature' into 'main'",
defaultSquashCommitMessage: "Add thing",
},
},
},
})
: args[0] === "api"
? mergeRequestJson({
diff_refs: { base_sha: "base", head_sha: "head", start_sha: "start" },
})
: "",
),
),
);
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;

yield* cli.runMergeRequestAction({
cwd: "/w",
repository: "acme/web",
number: 7,
action: "enable-auto-merge",
mergeMethod: "squash",
removeAgentCreditsOnMerge: true,
});

expect(mockedExecute.mock.calls.at(-1)?.[0].args).toEqual([
"mr",
"merge",
"7",
"--repo",
"acme/web",
"--auto-merge=true",
"--yes",
"--squash",
"--sha",
"head",
]);
}),
);

it.effect.each(["merge", "enable-auto-merge"] as const)(
"does not run %s when GitLab has no diff revisions",
(action) =>
Expand Down
30 changes: 30 additions & 0 deletions packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { removeAgentCredits } from "@t3tools/source-control-core/server/mergeMessage";
import { isCommitSha } from "@t3tools/source-control-core/server/pullRequestUtils";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
Expand Down Expand Up @@ -35,6 +36,7 @@ import {
decodeCommitsJson,
decodeDiffRefsJson,
decodeDiscussionsJson,
decodeMergeMessagesJson,
decodeMergeRequestDetailJson,
decodeMergeRequestDiffsJson,
decodeMergeRequestChangesJson,
Expand All @@ -51,6 +53,7 @@ import {
decodeTriggerJobChecksJson,
decodeViewerJson,
gitLabAwardName,
MERGE_MESSAGES_GRAPHQL_QUERY,
MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY,
MERGE_REQUEST_STATS_GRAPHQL_QUERY,
REPOSITORY_BLOBS_GRAPHQL_QUERY,
Expand Down Expand Up @@ -386,6 +389,7 @@ export class GitLabPullRequestCli extends Context.Service<
readonly number: number;
readonly action: PullRequestAction;
readonly mergeMethod?: PullRequestMergeMethod;
readonly removeAgentCreditsOnMerge?: boolean;
}) => Effect.Effect<void, GitLabPullRequestCliError>;

readonly updateMergeRequest: (input: {
Expand Down Expand Up @@ -1672,6 +1676,32 @@ export const make = Effect.gen(function* () {
}
const refs = yield* getDiffRefs(input);
flags.push("--sha", refs.headSha);
if (input.removeAgentCreditsOnMerge === true) {
// Read after the SHA guard, so a push since then fails the merge instead of
// merging commits whose credits these messages don't cover.
const messages = yield* graphqlRead({
cwd: input.cwd,
host: input.host,
operation: "getMergeMessages",
query: MERGE_MESSAGES_GRAPHQL_QUERY,
variables: { fullPath: input.repository, iid: String(input.number) },
decode: decodeMergeMessagesJson,
});
// glab drops an empty message, which would bring the credits back.
const clean = (message: string) => {
const cleaned = removeAgentCredits(message);
return cleaned.trim() === "" ? (messages.title ?? message) : cleaned;
Comment thread
TonybynMp4 marked this conversation as resolved.
Resolved
};
const merge = messages.merge === null ? null : clean(messages.merge);
if (merge !== null && merge !== messages.merge) flags.push("--message", merge);
const squashes = input.mergeMethod === "squash" || messages.squashOnMerge;
const squash = messages.squash === null || !squashes ? null : clean(messages.squash);
if (squash !== null && squash !== messages.squash) {
// glab takes a squash message only alongside --squash, which changes nothing here.
if (!flags.includes("--squash")) flags.push("--squash");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
flags.push("--squash-message", squash);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
}
}
yield* executeMr(subcommand!, ...flags);
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { removeAgentCredits } from "@t3tools/source-control-core/server/mergeMessage";
import { SourceControlProviderKind } from "@t3tools/contracts";
import * as Cache from "effect/Cache";
import * as Duration from "effect/Duration";
Expand Down Expand Up @@ -221,6 +222,7 @@ export const make = Effect.gen(function* () {
const provider: PullRequestProviderApi = {
kind: SourceControlProviderKind.make("gitlab"),
capabilities: CAPABILITIES,
mergeMessageRewrite: removeAgentCredits,
unavailableRequirement: UNAVAILABLE_REQUIREMENT,

getViewer: (input) => cli.getViewerUsername(input).pipe(Effect.mapError(fail("getViewer"))),
Expand Down Expand Up @@ -441,6 +443,7 @@ export const make = Effect.gen(function* () {
number: input.number,
action: input.action,
...(input.mergeMethod === undefined ? {} : { mergeMethod: input.mergeMethod }),
...(input.removeAgentCreditsOnMerge === true ? { removeAgentCreditsOnMerge: true } : {}),
})
.pipe(Effect.mapError(fail("runAction"))),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
decodeAwardEmojiJson,
decodeRepositoryBlobsJson,
decodeCommitsJson,
decodeMergeMessagesJson,
decodeMergeRequestDetailJson,
decodeMergeRequestDiffsJson,
decodeMergeRequestListJson,
Expand Down Expand Up @@ -790,3 +791,18 @@ describe("decodeMergeRequestStatsJson", () => {
expect(read(2).truncated).toBe(true);
});
});

describe("decodeMergeMessagesJson", () => {
it("fails a partial response instead of reading errored fields as absent templates", () => {
const decoded = decodeMergeMessagesJson(
JSON.stringify({
errors: [{ message: "Internal server error", path: ["project", "mergeRequest"] }],
data: {
project: { mergeRequest: { title: "Add thing", defaultMergeCommitMessage: null } },
},
}),
);

expect(Result.isFailure(decoded)).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,56 @@ export function decodeRepositoryBlobsJson(
return Result.succeed(blobs);
}

export const MERGE_MESSAGES_GRAPHQL_QUERY = `query($fullPath: ID!, $iid: String!) {
project(fullPath: $fullPath) {
mergeRequest(iid: $iid) { title squashOnMerge defaultMergeCommitMessage defaultSquashCommitMessage }
}
}`;

const RawMergeMessagesSchema = Schema.Struct({
// A field that errored reads as null, which must not pass for an absent template.
errors: Schema.optionalKey(Schema.Never),
data: Schema.Struct({
project: Schema.NullOr(
Schema.Struct({
mergeRequest: Schema.NullOr(
Schema.Struct({
title: Schema.optional(Schema.NullOr(Schema.String)),
squashOnMerge: Schema.optional(Schema.NullOr(Schema.Boolean)),
defaultMergeCommitMessage: Schema.optional(Schema.NullOr(Schema.String)),
defaultSquashCommitMessage: Schema.optional(Schema.NullOr(Schema.String)),
}),
),
}),
),
}),
});

const decodeMergeMessages = decodeJsonResult(RawMergeMessagesSchema);

export interface GitLabMergeMessages {
readonly title: string | null;
readonly merge: string | null;
readonly squash: string | null;
/** GitLab squashes when the project always does or the merge request asks to. */
readonly squashOnMerge: boolean;
}

/** The messages GitLab would write from the project's merge and squash templates. */
export function decodeMergeMessagesJson(
raw: string,
): Result.Result<GitLabMergeMessages, DecodeFailure> {
return Result.map(decodeMergeMessages(raw), (decoded) => {
const mergeRequest = decoded.data.project?.mergeRequest;
return {
title: mergeRequest?.title ?? null,
merge: mergeRequest?.defaultMergeCommitMessage ?? null,
squash: mergeRequest?.defaultSquashCommitMessage ?? null,
squashOnMerge: mergeRequest?.squashOnMerge === true,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
};
});
}

export const REVIEW_DECISIONS_GRAPHQL_QUERY = `query($fullPath: ID!, $iids: [String!]) {
metadata { enterprise }
project(fullPath: $fullPath) {
Expand Down
Loading