@@ -34,6 +34,7 @@ import {
3434 decodeCommitDiffRefsJson ,
3535 decodeCommitsJson ,
3636 decodeDiffRefsJson ,
37+ decodeRevertTargetJson ,
3738 decodeDiscussionsJson ,
3839 decodeMergeRequestDetailJson ,
3940 decodeMergeRequestDiffsJson ,
@@ -116,6 +117,23 @@ export class GitLabDiffRefsUnavailableError extends Schema.TaggedError<GitLabDif
116117 }
117118}
118119
120+ export class GitLabRevertUnavailableError extends Schema . TaggedError < GitLabRevertUnavailableError > ( ) (
121+ "GitLabRevertUnavailableError" ,
122+ {
123+ command : Schema . Literal ( "glab" ) ,
124+ cwd : Schema . String ,
125+ number : Schema . Int ,
126+ } ,
127+ ) {
128+ get detail ( ) : string {
129+ return "This merge request landed as several commits, so there is no single commit to revert." ;
130+ }
131+
132+ override get message ( ) : string {
133+ return `GitLab CLI failed in runMergeRequestAction: ${ this . detail } ` ;
134+ }
135+ }
136+
119137export class GitLabDiffCursorError extends Schema . TaggedError < GitLabDiffCursorError > ( ) (
120138 "GitLabDiffCursorError" ,
121139 {
@@ -193,6 +211,7 @@ export type GitLabPullRequestCliError =
193211 | GitLabDiffCommitParentUnavailableError
194212 | GitLabDiffFileContentsUnavailableError
195213 | GitLabDiffRefsUnavailableError
214+ | GitLabRevertUnavailableError
196215 | GitLabViewerUnavailableError ;
197216
198217/** GitLab's `per_page` limit. */
@@ -596,6 +615,7 @@ function actionArgs(
596615 case "reopen" :
597616 return [ "reopen" ] ;
598617 case "revert" :
618+ return [ ] ;
599619 case "approve-workflows" :
600620 case "delete-branch" :
601621 throw new Error ( `GitLab merge request action ${ action } is unsupported` ) ;
@@ -908,6 +928,68 @@ export const make = Effect.gen(function* () {
908928 } ) ,
909929 ) ;
910930
931+ /** What GitLab's own Revert button does: revert onto a new branch and open a merge request. */
932+ const revertMergeRequest = Effect . fn ( "GitLabPullRequestCli.revertMergeRequest" ) (
933+ function * ( input : {
934+ readonly cwd : string ;
935+ readonly host ?: string | undefined ;
936+ readonly repository : string ;
937+ readonly number : number ;
938+ } ) {
939+ const project = `projects/${ projectPath ( input . repository ) } ` ;
940+ const write = ( path : string , body : object ) =>
941+ api ( {
942+ cwd : input . cwd ,
943+ host : input . host ,
944+ path : `${ project } /${ path } ` ,
945+ method : "POST" ,
946+ stdin : JSON . stringify ( body ) ,
947+ } ) ;
948+ const raw = yield * api ( {
949+ cwd : input . cwd ,
950+ host : input . host ,
951+ path : `${ project } /merge_requests/${ input . number } ` ,
952+ } ) ;
953+ const decoded = decodeRevertTargetJson ( raw . stdout . trim ( ) ) ;
954+ if ( ! Result . isSuccess ( decoded ) ) {
955+ return yield * new GitLabMergeRequestReadError ( {
956+ command : "glab" ,
957+ cwd : input . cwd ,
958+ operation : "runMergeRequestAction" ,
959+ cause : decoded . failure ,
960+ } ) ;
961+ }
962+ const target = decoded . success ;
963+ if ( target . commitSha === null ) {
964+ return yield * new GitLabRevertUnavailableError ( {
965+ command : "glab" ,
966+ cwd : input . cwd ,
967+ number : input . number ,
968+ } ) ;
969+ }
970+ const branch = `revert-${ target . commitSha . slice ( 0 , 8 ) } ` ;
971+ yield * write ( "repository/branches" , { branch, ref : target . targetBranch } ) ;
972+ yield * write ( `repository/commits/${ target . commitSha } /revert` , { branch } ) . pipe (
973+ // A revert that conflicts leaves nothing worth keeping on the new branch.
974+ Effect . tapError ( ( ) =>
975+ api ( {
976+ cwd : input . cwd ,
977+ host : input . host ,
978+ path : `${ project } /repository/branches/${ encodeURIComponent ( branch ) } ` ,
979+ method : "DELETE" ,
980+ } ) . pipe ( Effect . ignore ) ,
981+ ) ,
982+ ) ;
983+ yield * write ( "merge_requests" , {
984+ source_branch : branch ,
985+ target_branch : target . targetBranch ,
986+ title : `Revert "${ target . title } "` ,
987+ description : `This reverts merge request !${ input . number } ` ,
988+ remove_source_branch : true ,
989+ } ) ;
990+ } ,
991+ ) ;
992+
911993 const getDiffRefs = ( input : {
912994 readonly cwd : string ;
913995 readonly host ?: string | undefined ;
@@ -1658,6 +1740,7 @@ export const make = Effect.gen(function* () {
16581740 method : "POST" ,
16591741 } ) . pipe ( Effect . asVoid ) ;
16601742 }
1743+ if ( input . action === "revert" ) return yield * revertMergeRequest ( input ) ;
16611744 const executeMr = ( subcommand : string , ...flags : ReadonlyArray < string > ) =>
16621745 gitlab . execute ( {
16631746 cwd : input . cwd ,
0 commit comments