Skip to content

Commit 45c3922

Browse files
TonybynMp4claude
andcommitted
feat(gitlab): reviews can request changes
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a185dd9 commit 45c3922

6 files changed

Lines changed: 258 additions & 21 deletions

File tree

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,48 @@ layer("GitLabPullRequestCli.layer", (it) => {
148148
}),
149149
);
150150

151+
it.effect("keeps approvals from a server that has no reviewer review states", () =>
152+
Effect.gen(function* () {
153+
mockedExecute.mockImplementation(({ cwd, stdin }) =>
154+
(stdin ?? "").includes("reviewState")
155+
? Effect.fail(
156+
new GitLabCli.GitLabCliCommandError({
157+
operation: "execute",
158+
command: "glab",
159+
cwd,
160+
cause: new Error("Field 'reviewState' doesn't exist"),
161+
}),
162+
)
163+
: Effect.succeed(
164+
output(
165+
JSON.stringify({
166+
data: {
167+
metadata: { enterprise: false },
168+
project: {
169+
mergeRequests: {
170+
nodes: [
171+
{ iid: "7", approved: true, approvedBy: { nodes: [{ username: "r" }] } },
172+
],
173+
},
174+
},
175+
},
176+
}),
177+
),
178+
),
179+
);
180+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
181+
182+
const decisions = yield* cli.getReviewDecisions({
183+
cwd: "/w",
184+
repository: "acme/web",
185+
numbers: [7],
186+
});
187+
188+
expect([...decisions]).toEqual([[7, "approved"]]);
189+
expect(mockedExecute).toHaveBeenCalledTimes(2);
190+
}),
191+
);
192+
151193
it.effect("asks GitLab for one row more than the page, to probe for a next page", () =>
152194
Effect.gen(function* () {
153195
mockedExecute.mockReturnValueOnce(Effect.succeed(output(mergeRequests(3, 1))));
@@ -944,6 +986,49 @@ layer("GitLabPullRequestCli.layer", (it) => {
944986
}),
945987
);
946988

989+
it.effect("requests changes as a reviewer and reports what GitLab refused", () =>
990+
Effect.gen(function* () {
991+
mockedExecute
992+
.mockReturnValueOnce(Effect.succeed(output(JSON.stringify({ username: "bilal" }))))
993+
.mockReturnValueOnce(
994+
Effect.succeed(
995+
output(JSON.stringify({ data: { mergeRequestSetReviewers: { errors: [] } } })),
996+
),
997+
)
998+
.mockReturnValueOnce(
999+
Effect.succeed(
1000+
output(
1001+
JSON.stringify({
1002+
data: { mergeRequestRequestChanges: { errors: ["Merge request is locked"] } },
1003+
}),
1004+
),
1005+
),
1006+
);
1007+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1008+
1009+
const error = yield* Effect.flip(
1010+
cli.submitReview({
1011+
cwd: "/w",
1012+
repository: "acme/web",
1013+
number: 7,
1014+
verdict: "request-changes",
1015+
body: "",
1016+
comments: [],
1017+
}),
1018+
);
1019+
1020+
const target = { projectPath: "acme/web", iid: "7" };
1021+
const setReviewers = JSON.parse(callAt(1).stdin ?? "");
1022+
expect(setReviewers.query).toContain("operationMode: APPEND");
1023+
expect(setReviewers.variables).toEqual({ ...target, reviewers: ["bilal"] });
1024+
const requestChanges = JSON.parse(callAt(2).stdin ?? "");
1025+
expect(requestChanges.query).toContain("mergeRequestRequestChanges");
1026+
expect(requestChanges.variables).toEqual(target);
1027+
assert.strictEqual(error._tag, "GitLabMutationRefusedError");
1028+
expect(error.detail).toBe("GitLab refused it: Merge request is locked");
1029+
}),
1030+
);
1031+
9471032
it.effect("removes an award by listing them and deleting the reader's own id", () =>
9481033
Effect.gen(function* () {
9491034
mockedExecute.mockReturnValueOnce(

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

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
decodeMergeRequestListJson,
4242
decodeMergeRequestFileStatsJson,
4343
decodeMergeRequestStatsJson,
44+
decodeMutationErrorsJson,
4445
decodeNotesJson,
4546
decodeOwnAwardIdJson,
4647
decodeProjectMergeCapabilitiesJson,
@@ -54,6 +55,7 @@ import {
5455
MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY,
5556
MERGE_REQUEST_STATS_GRAPHQL_QUERY,
5657
REPOSITORY_BLOBS_GRAPHQL_QUERY,
58+
APPROVALS_GRAPHQL_QUERY,
5759
REVIEW_DECISIONS_GRAPHQL_QUERY,
5860
type GitLabDiffRefs,
5961
type GitLabMergeRequestDetail,
@@ -185,6 +187,34 @@ export class GitLabDiffFileContentsUnavailableError extends Schema.TaggedError<G
185187
}
186188
}
187189

190+
export class GitLabMutationRefusedError extends Schema.TaggedError<GitLabMutationRefusedError>()(
191+
"GitLabMutationRefusedError",
192+
{
193+
command: Schema.Literal("glab"),
194+
cwd: Schema.String,
195+
operation: Schema.String,
196+
reasons: Schema.Array(Schema.String),
197+
},
198+
) {
199+
get detail(): string {
200+
return `GitLab refused it: ${this.reasons.join("; ")}`;
201+
}
202+
203+
override get message(): string {
204+
return `GitLab CLI failed in ${this.operation}: ${this.detail}`;
205+
}
206+
}
207+
208+
const SET_SELF_AS_REVIEWER_MUTATION = `mutation($projectPath: ID!, $iid: String!, $reviewers: [String!]!) {
209+
mergeRequestSetReviewers(
210+
input: { projectPath: $projectPath, iid: $iid, reviewerUsernames: $reviewers, operationMode: APPEND }
211+
) { errors }
212+
}`;
213+
214+
const REQUEST_CHANGES_MUTATION = `mutation($projectPath: ID!, $iid: String!) {
215+
mergeRequestRequestChanges(input: { projectPath: $projectPath, iid: $iid }) { errors }
216+
}`;
217+
188218
export type GitLabPullRequestCliError =
189219
| GitLabCli.GitLabCliError
190220
| GitLabMergeRequestReadError
@@ -193,7 +223,8 @@ export type GitLabPullRequestCliError =
193223
| GitLabDiffCommitParentUnavailableError
194224
| GitLabDiffFileContentsUnavailableError
195225
| GitLabDiffRefsUnavailableError
196-
| GitLabViewerUnavailableError;
226+
| GitLabViewerUnavailableError
227+
| GitLabMutationRefusedError;
197228

198229
/** GitLab's `per_page` limit. */
199230
const MAX_PAGE_SIZE = 100;
@@ -1170,6 +1201,28 @@ export const make = Effect.gen(function* () {
11701201
}),
11711202
);
11721203

1204+
const graphqlMutation = (input: {
1205+
readonly cwd: string;
1206+
readonly host?: string | undefined;
1207+
readonly operation: string;
1208+
readonly query: string;
1209+
readonly variables: Record<string, unknown>;
1210+
}) =>
1211+
graphqlRead({ ...input, decode: decodeMutationErrorsJson }).pipe(
1212+
Effect.flatMap((reasons) =>
1213+
reasons.length === 0
1214+
? Effect.void
1215+
: Effect.fail(
1216+
new GitLabMutationRefusedError({
1217+
command: "glab",
1218+
cwd: input.cwd,
1219+
operation: input.operation,
1220+
reasons,
1221+
}),
1222+
),
1223+
),
1224+
);
1225+
11731226
const mergeRequestStats = (input: {
11741227
readonly cwd: string;
11751228
readonly host?: string | undefined;
@@ -1316,15 +1369,20 @@ export const make = Effect.gen(function* () {
13161369
}): Effect.Effect<
13171370
ReadonlyMap<number, PullRequestReviewDecision | null>,
13181371
GitLabPullRequestCliError
1319-
> =>
1320-
graphqlRead({
1321-
cwd: input.cwd,
1322-
host: input.host,
1323-
operation: "getReviewDecisions",
1324-
query: REVIEW_DECISIONS_GRAPHQL_QUERY,
1325-
variables: { fullPath: input.repository, iids: input.numbers.map(String) },
1326-
decode: decodeReviewDecisionsJson,
1327-
});
1372+
> => {
1373+
const read = (query: string) =>
1374+
graphqlRead({
1375+
cwd: input.cwd,
1376+
host: input.host,
1377+
operation: "getReviewDecisions",
1378+
query,
1379+
variables: { fullPath: input.repository, iids: input.numbers.map(String) },
1380+
decode: decodeReviewDecisionsJson,
1381+
});
1382+
return read(REVIEW_DECISIONS_GRAPHQL_QUERY).pipe(
1383+
Effect.catchTags({ GitLabCliCommandError: () => read(APPROVALS_GRAPHQL_QUERY) }),
1384+
);
1385+
};
13281386

13291387
const viewerUsername = (input: { readonly cwd: string; readonly host?: string | undefined }) =>
13301388
api({ cwd: input.cwd, host: input.host, path: "user" }).pipe(
@@ -1774,6 +1832,26 @@ export const make = Effect.gen(function* () {
17741832
method: "POST",
17751833
});
17761834
}
1835+
if (input.verdict === "request-changes") {
1836+
// GitLab keeps the verdict on the reviewer, so whoever requests changes joins the
1837+
// reviewers first, as on GitHub. Appending someone already there changes nothing.
1838+
const viewer = yield* viewerUsername(input);
1839+
const target = { projectPath: input.repository, iid: String(input.number) };
1840+
yield* graphqlMutation({
1841+
cwd: input.cwd,
1842+
host: input.host,
1843+
operation: "submitReview",
1844+
query: SET_SELF_AS_REVIEWER_MUTATION,
1845+
variables: { ...target, reviewers: [viewer] },
1846+
});
1847+
yield* graphqlMutation({
1848+
cwd: input.cwd,
1849+
host: input.host,
1850+
operation: "submitReview",
1851+
query: REQUEST_CHANGES_MUTATION,
1852+
variables: target,
1853+
});
1854+
}
17771855
}),
17781856

17791857
replyToDiscussion: Effect.fn("GitLabPullRequestCli.replyToDiscussion")(function (input) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ describe("gitLabViewerPermissions", () => {
135135
],
136136
comment: true,
137137
resolve: true,
138-
verdicts: ["comment", "approve"],
138+
verdicts: ["comment", "approve", "request-changes"],
139139
requestReviewers: true,
140140
updateMethods: ["rebase"],
141141
});
@@ -146,7 +146,7 @@ describe("gitLabViewerPermissions", () => {
146146
actions: ["ready", "draft", "close", "reopen"],
147147
comment: true,
148148
resolve: true,
149-
verdicts: ["comment", "approve"],
149+
verdicts: ["comment", "approve", "request-changes"],
150150
requestReviewers: true,
151151
});
152152
});

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ const CAPABILITIES: PullRequestCapabilities = {
3535
inlineComment: true,
3636
reply: true,
3737
resolve: true,
38-
// GitLab has no requested-changes verdict.
39-
verdicts: ["comment", "approve"],
38+
verdicts: ["comment", "approve", "request-changes"],
4039
},
4140
reviewers: { request: true, listCandidates: true },
4241
edit: { changeRequest: true, comment: true },

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,21 @@ describe("decodeReviewDecisionsJson", () => {
715715
approval: { approved: false, approvedBy: approvedBy() },
716716
decision: null,
717717
},
718+
{
719+
reading: "a reviewer requesting changes after another approved",
720+
enterprise: false,
721+
approval: {
722+
approved: true,
723+
approvedBy: approvedBy("julius"),
724+
reviewers: {
725+
nodes: [
726+
{ mergeRequestInteraction: { reviewState: "APPROVED" } },
727+
{ mergeRequestInteraction: { reviewState: "REQUESTED_CHANGES" } },
728+
],
729+
},
730+
},
731+
decision: "changes-requested",
732+
},
718733
])("reads $reading as $decision", ({ enterprise, approval, decision }) => {
719734
expect([...decisionsOf(enterprise, [{ iid: "7", ...approval }])]).toEqual([[7, decision]]);
720735
});

0 commit comments

Comments
 (0)