@@ -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+
188218export 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. */
199230const 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) {
0 commit comments