Skip to content

Commit 9e4e45b

Browse files
TonybynMp4claude
andcommitted
feat(gitlab): merge requests from forks can toggle maintainer edits
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 66e6217 commit 9e4e45b

5 files changed

Lines changed: 156 additions & 3 deletions

File tree

‎packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1105,14 +1105,17 @@ layer("GitLabPullRequestCli.layer", (it) => {
11051105
}),
11061106
);
11071107

1108-
it.effect("reads merge permissions without reading the trigger jobs", () =>
1108+
it.effect("reads merge permissions without reading the trigger jobs or fork settings", () =>
11091109
Effect.gen(function* () {
11101110
mockedExecute.mockReturnValueOnce(
11111111
Effect.succeed(
11121112
output(
11131113
mergeRequestJson({
11141114
head_pipeline: { id: 31, project_id: 12, status: "success" },
11151115
user: { can_merge: false },
1116+
source_project_id: 12,
1117+
target_project_id: 12,
1118+
allow_collaboration: null,
11161119
}),
11171120
),
11181121
),
@@ -1127,10 +1130,74 @@ layer("GitLabPullRequestCli.layer", (it) => {
11271130
});
11281131

11291132
expect(detail.viewerCanMerge).toBe(false);
1133+
expect(detail.maintainerCanModify).toBeUndefined();
11301134
expect(mockedExecute).toHaveBeenCalledTimes(1);
11311135
}),
11321136
);
11331137

1138+
it.effect.each([
1139+
["the author who can push to the fork", "bilal", true, true],
1140+
["a target maintainer the fork lets push", "octocat", true, false],
1141+
["the author without push access", "bilal", false, false],
1142+
] as const)(
1143+
"reads a fork's maintainer edits and lets %s change them",
1144+
([, viewer, pushToSourceBranch, allowed]) =>
1145+
Effect.gen(function* () {
1146+
mockedExecute.mockImplementation(({ args }) =>
1147+
Effect.succeed(
1148+
output(
1149+
args[1] === "graphql"
1150+
? JSON.stringify({
1151+
data: {
1152+
currentUser: { username: viewer },
1153+
project: {
1154+
mergeRequest: {
1155+
author: { username: "bilal" },
1156+
userPermissions: { pushToSourceBranch },
1157+
},
1158+
},
1159+
},
1160+
})
1161+
: mergeRequestJson({
1162+
author,
1163+
source_project_id: 40,
1164+
target_project_id: 12,
1165+
allow_collaboration: true,
1166+
}),
1167+
),
1168+
),
1169+
);
1170+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1171+
1172+
const detail = yield* cli.getMergeRequestDetail({
1173+
cwd: "/w",
1174+
repository: "acme/web",
1175+
number: 7,
1176+
downstreamChecks: false,
1177+
});
1178+
1179+
expect(detail.maintainerCanModify).toBe(true);
1180+
expect(detail.viewerCanEditMaintainerEdits).toBe(allowed);
1181+
}),
1182+
);
1183+
1184+
it.effect("sends maintainer edits as allow_collaboration", () =>
1185+
Effect.gen(function* () {
1186+
mockedExecute.mockReturnValueOnce(Effect.succeed(output("{}")));
1187+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1188+
1189+
yield* cli.updateMergeRequest({
1190+
cwd: "/w",
1191+
repository: "acme/web",
1192+
number: 7,
1193+
allowCollaboration: false,
1194+
});
1195+
1196+
expect(argsOfCall(0)).toContain("PUT");
1197+
expect(JSON.parse(callAt(0).stdin ?? "")).toEqual({ allow_collaboration: false });
1198+
}),
1199+
);
1200+
11341201
it.effect("reads who has access to the project and who is already on the merge request", () =>
11351202
Effect.gen(function* () {
11361203
mockedExecute

‎packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
decodeCommitsJson,
3636
decodeDiffRefsJson,
3737
decodeDiscussionsJson,
38+
decodeMaintainerEditsPermissionJson,
3839
decodeMergeRequestDetailJson,
3940
decodeMergeRequestDiffsJson,
4041
decodeMergeRequestChangesJson,
@@ -51,6 +52,7 @@ import {
5152
decodeTriggerJobChecksJson,
5253
decodeViewerJson,
5354
gitLabAwardName,
55+
MAINTAINER_EDITS_PERMISSION_GRAPHQL_QUERY,
5456
MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY,
5557
MERGE_REQUEST_STATS_GRAPHQL_QUERY,
5658
REPOSITORY_BLOBS_GRAPHQL_QUERY,
@@ -395,6 +397,7 @@ export class GitLabPullRequestCli extends Context.Service<
395397
readonly number: number;
396398
readonly title?: string | undefined;
397399
readonly description?: string | undefined;
400+
readonly allowCollaboration?: boolean | undefined;
398401
}) => Effect.Effect<void, GitLabPullRequestCliError>;
399402

400403
readonly commentOnMergeRequest: (input: {
@@ -1357,7 +1360,22 @@ export const make = Effect.gen(function* () {
13571360

13581361
getMergeRequestDetail: Effect.fn("GitLabPullRequestCli.getMergeRequestDetail")(
13591362
function* (input) {
1360-
const detail = yield* mergeRequestDetail(input);
1363+
const read = yield* mergeRequestDetail(input);
1364+
// Only forks have the setting, so same-project merge requests skip the extra read.
1365+
const detail =
1366+
read.maintainerCanModify === undefined || read.state !== "open"
1367+
? read
1368+
: {
1369+
...read,
1370+
viewerCanEditMaintainerEdits: yield* graphqlRead({
1371+
cwd: input.cwd,
1372+
host: input.host,
1373+
operation: "getMaintainerEditsPermission",
1374+
query: MAINTAINER_EDITS_PERMISSION_GRAPHQL_QUERY,
1375+
variables: { fullPath: input.repository, iid: String(input.number) },
1376+
decode: decodeMaintainerEditsPermissionJson,
1377+
}),
1378+
};
13611379
if (input.downstreamChecks === false || detail.headPipeline === undefined) return detail;
13621380
const downstream = yield* triggerJobChecks({
13631381
cwd: input.cwd,
@@ -1687,6 +1705,9 @@ export const make = Effect.gen(function* () {
16871705
stdin: JSON.stringify({
16881706
...(input.title === undefined ? {} : { title: input.title }),
16891707
...(input.description === undefined ? {} : { description: input.description }),
1708+
...(input.allowCollaboration === undefined
1709+
? {}
1710+
: { allow_collaboration: input.allowCollaboration }),
16901711
}),
16911712
}).pipe(Effect.asVoid);
16921713
}),

‎packages/source-control-gitlab/src/server/GitLabPullRequestProvider.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ describe("gitLabViewerPermissions", () => {
137137
resolve: true,
138138
verdicts: ["comment", "approve"],
139139
requestReviewers: true,
140+
maintainerEdits: false,
140141
updateMethods: ["rebase"],
141142
});
142143
});
@@ -148,6 +149,7 @@ describe("gitLabViewerPermissions", () => {
148149
resolve: true,
149150
verdicts: ["comment", "approve"],
150151
requestReviewers: true,
152+
maintainerEdits: false,
151153
});
152154
});
153155
});

‎packages/source-control-gitlab/src/server/GitLabPullRequestProvider.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const CAPABILITIES: PullRequestCapabilities = {
3939
verdicts: ["comment", "approve"],
4040
},
4141
reviewers: { request: true, listCandidates: true },
42-
edit: { changeRequest: true, comment: true },
42+
edit: { changeRequest: true, comment: true, maintainerEdits: true },
4343
};
4444

4545
const UNAVAILABLE_REQUIREMENT = {
@@ -63,6 +63,7 @@ const MERGE_ACTIONS: ReadonlySet<string> = new Set([
6363
/** Authors may close/reopen/change draft state regardless of role; REST omits authorship. */
6464
export function gitLabViewerPermissions(input: {
6565
readonly viewerCanMerge: boolean;
66+
readonly viewerCanEditMaintainerEdits?: boolean | undefined;
6667
}): PullRequestViewerPermissions {
6768
return {
6869
actions: CAPABILITIES.actions.filter(
@@ -72,6 +73,7 @@ export function gitLabViewerPermissions(input: {
7273
resolve: true,
7374
verdicts: CAPABILITIES.review.verdicts,
7475
requestReviewers: true,
76+
maintainerEdits: input.viewerCanEditMaintainerEdits === true,
7577
...(input.viewerCanMerge ? { updateMethods: CAPABILITIES.updateMethods } : {}),
7678
};
7779
}
@@ -454,6 +456,9 @@ export const make = Effect.gen(function* () {
454456
number: input.number,
455457
...(input.title === undefined ? {} : { title: input.title }),
456458
...(input.body === undefined ? {} : { description: input.body }),
459+
...(input.maintainerCanModify === undefined
460+
? {}
461+
: { allowCollaboration: input.maintainerCanModify }),
457462
})
458463
.pipe(Effect.mapError(fail("updateChangeRequest"))),
459464
),

‎packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ const RawMergeRequestSchema = Schema.Struct({
7878
squash: Schema.optional(Schema.NullOr(Schema.Boolean)),
7979
/** Only detail reads with include_diverged_commits_count return this expensive count. */
8080
diverged_commits_count: Schema.optional(Schema.NullOr(Schema.Int)),
81+
source_project_id: Schema.optional(Schema.NullOr(Schema.Int)),
82+
target_project_id: Schema.optional(Schema.NullOr(Schema.Int)),
83+
/** Null on merge requests from the same project, where it means nothing. */
84+
allow_collaboration: Schema.optional(Schema.NullOr(Schema.Boolean)),
8185
});
8286

8387
const RawNoteSchema = Schema.Struct({
@@ -209,6 +213,9 @@ export interface GitLabMergeRequestDetail extends GitLabMergeRequestListItem {
209213
readonly headSha?: string;
210214
/** On a fork the pipeline often belongs to the source project, not the target. */
211215
readonly headPipeline?: { readonly id: number; readonly projectId: number };
216+
/** Whether target maintainers may push to the source branch. Only set on merge requests from forks. */
217+
readonly maintainerCanModify?: boolean;
218+
readonly viewerCanEditMaintainerEdits?: boolean;
212219
}
213220

214221
function toActor(raw: Schema.Schema.Type<typeof RawUserSchema> | null | undefined) {
@@ -345,6 +352,11 @@ function toDetail(raw: Schema.Schema.Type<typeof RawMergeRequestSchema>): GitLab
345352
...(raw.head_pipeline?.id == null || raw.head_pipeline.project_id == null
346353
? {}
347354
: { headPipeline: { id: raw.head_pipeline.id, projectId: raw.head_pipeline.project_id } }),
355+
...(raw.source_project_id != null &&
356+
raw.target_project_id != null &&
357+
raw.source_project_id !== raw.target_project_id
358+
? { maintainerCanModify: raw.allow_collaboration === true }
359+
: {}),
348360
};
349361
}
350362

@@ -1141,6 +1153,52 @@ export function decodeMergeRequestStatsJson(
11411153
return Result.succeed(stats);
11421154
}
11431155

1156+
/**
1157+
* GitLab lets whoever can push to a fork's branch allow maintainer pushes, but once allowed the
1158+
* target's maintainers can push too, so only the author may change it, as on GitHub.
1159+
*/
1160+
export const MAINTAINER_EDITS_PERMISSION_GRAPHQL_QUERY = `query($fullPath: ID!, $iid: String!) {
1161+
currentUser { username }
1162+
project(fullPath: $fullPath) {
1163+
mergeRequest(iid: $iid) {
1164+
author { username }
1165+
userPermissions { pushToSourceBranch }
1166+
}
1167+
}
1168+
}`;
1169+
1170+
const decodeMaintainerEditsPermission = decodeJsonResult(
1171+
Schema.Struct({
1172+
data: Schema.Struct({
1173+
currentUser: Schema.NullOr(Schema.Struct({ username: Schema.String })),
1174+
project: Schema.NullOr(
1175+
Schema.Struct({
1176+
mergeRequest: Schema.NullOr(
1177+
Schema.Struct({
1178+
author: Schema.NullOr(Schema.Struct({ username: Schema.String })),
1179+
userPermissions: Schema.Struct({ pushToSourceBranch: Schema.Boolean }),
1180+
}),
1181+
),
1182+
}),
1183+
),
1184+
}),
1185+
}),
1186+
);
1187+
1188+
export function decodeMaintainerEditsPermissionJson(
1189+
raw: string,
1190+
): Result.Result<boolean, DecodeFailure> {
1191+
const decoded = decodeMaintainerEditsPermission(raw);
1192+
if (Result.isFailure(decoded)) return Result.fail(decoded.failure);
1193+
const { currentUser, project } = decoded.success.data;
1194+
const mergeRequest = project?.mergeRequest;
1195+
return Result.succeed(
1196+
currentUser !== null &&
1197+
mergeRequest?.author?.username === currentUser.username &&
1198+
mergeRequest.userPermissions.pushToSourceBranch,
1199+
);
1200+
}
1201+
11441202
/** Per-file line counts for one merge request, which the code/tests/generated breakdown sorts. */
11451203
export const MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY = `query($fullPath: ID!, $iid: String!) {
11461204
project(fullPath: $fullPath) {

0 commit comments

Comments
 (0)