@@ -87,6 +87,10 @@ function mergeRequestJson(overrides: Record<string, unknown>): string {
8787 } ) ;
8888}
8989
90+ function commitJson ( id : string ) {
91+ return { id, title : "Commit" , committed_date : "2026-07-01T00:00:00Z" } ;
92+ }
93+
9094function sourceBranchJson ( overrides : Record < string , unknown > ) : string {
9195 return JSON . stringify ( {
9296 data : {
@@ -871,6 +875,182 @@ layer("GitLabPullRequestCli.layer", (it) => {
871875 } ) ,
872876 ) ;
873877
878+ it . effect ( "reverts a merged merge request onto a new branch and opens a merge request" , ( ) =>
879+ Effect . gen ( function * ( ) {
880+ const mergeCommit = "dac72fa101758a44b0068a23666559920a74478f" ;
881+ mockedExecute
882+ . mockReturnValueOnce (
883+ Effect . succeed (
884+ output (
885+ mergeRequestJson ( {
886+ state : "merged" ,
887+ merge_commit_sha : mergeCommit ,
888+ squash_commit_sha : "cb2d9f660ebe946e5056ae98993db5ea72b9bad1" ,
889+ } ) ,
890+ ) ,
891+ ) ,
892+ )
893+ . mockReturnValue ( Effect . succeed ( output ( "{}" ) ) ) ;
894+ const cli = yield * GitLabPullRequestCli . GitLabPullRequestCli ;
895+
896+ yield * cli . runMergeRequestAction ( {
897+ cwd : "/w" ,
898+ repository : "acme/web" ,
899+ number : 7 ,
900+ action : "revert" ,
901+ } ) ;
902+
903+ expect (
904+ mockedExecute . mock . calls . map ( ( [ input ] ) => [
905+ input . args [ 1 ] ,
906+ input . stdin && JSON . parse ( input . stdin ) ,
907+ ] ) ,
908+ ) . toEqual ( [
909+ [ "projects/acme%2Fweb/merge_requests/7" , undefined ] ,
910+ [ "projects/acme%2Fweb/repository/branches" , { branch : "revert-dac72fa1" , ref : "main" } ] ,
911+ [
912+ `projects/acme%2Fweb/repository/commits/${ mergeCommit } /revert` ,
913+ { branch : "revert-dac72fa1" } ,
914+ ] ,
915+ [
916+ "projects/acme%2Fweb/merge_requests" ,
917+ {
918+ source_branch : "revert-dac72fa1" ,
919+ target_branch : "main" ,
920+ title : 'Revert "Merge request 7"' ,
921+ description : "This reverts merge request !7" ,
922+ remove_source_branch : true ,
923+ } ,
924+ ] ,
925+ ] ) ;
926+ } ) ,
927+ ) ;
928+
929+ it . effect . each ( [
930+ [ "the revert conflicts" , "/revert" , "Failure" ] ,
931+ [ "opening the merge request fails" , "/merge_requests" , "Failure" ] ,
932+ [ "the request is interrupted" , "/revert" , "Interrupt" ] ,
933+ ] as const ) ( "deletes the revert branch when %s" , ( [ , failingPath , outcome ] ) =>
934+ Effect . gen ( function * ( ) {
935+ mockedExecute . mockImplementation ( ( { args, cwd } ) =>
936+ args [ 1 ] ?. endsWith ( failingPath ) && args . includes ( "POST" )
937+ ? outcome === "Interrupt"
938+ ? Effect . interrupt
939+ : Effect . fail (
940+ new GitLabCli . GitLabCliCommandError ( {
941+ operation : "execute" ,
942+ command : "glab" ,
943+ cwd,
944+ cause : new Error ( "GitLab rejected the write" ) ,
945+ } ) ,
946+ )
947+ : Effect . succeed (
948+ output (
949+ mergeRequestJson ( {
950+ state : "merged" ,
951+ merge_commit_sha : null ,
952+ squash_commit_sha : "abc12345ff" ,
953+ } ) ,
954+ ) ,
955+ ) ,
956+ ) ;
957+ const cli = yield * GitLabPullRequestCli . GitLabPullRequestCli ;
958+
959+ const exit = yield * Effect . exit (
960+ cli . runMergeRequestAction ( {
961+ cwd : "/w" ,
962+ repository : "acme/web" ,
963+ number : 7 ,
964+ action : "revert" ,
965+ } ) ,
966+ ) ;
967+
968+ expect ( exit . _tag ) . toBe ( "Failure" ) ;
969+ expect ( mockedExecute . mock . calls . at ( - 1 ) ?. [ 0 ] . args . slice ( 1 , 4 ) ) . toEqual ( [
970+ "projects/acme%2Fweb/repository/branches/revert-abc12345" ,
971+ "--method" ,
972+ "DELETE" ,
973+ ] ) ;
974+ } ) ,
975+ ) ;
976+
977+ it . effect . each ( [
978+ [ "reverts the sole commit of" , [ commitJson ( "1111111122" ) ] , "revert-11111111" ] ,
979+ [
980+ "refuses to revert several commits of" ,
981+ [ commitJson ( "1111111122" ) , commitJson ( "2222222233" ) ] ,
982+ "GitLabRevertUnavailableError" ,
983+ ] ,
984+ ] as const ) ( "%s a fast-forward merge without squash" , ( [ , commits , expected ] ) =>
985+ Effect . gen ( function * ( ) {
986+ mockedExecute
987+ . mockReturnValueOnce (
988+ Effect . succeed (
989+ output (
990+ mergeRequestJson ( {
991+ state : "merged" ,
992+ merge_commit_sha : null ,
993+ squash_commit_sha : null ,
994+ } ) ,
995+ ) ,
996+ ) ,
997+ )
998+ . mockReturnValueOnce ( Effect . succeed ( output ( JSON . stringify ( commits ) ) ) )
999+ . mockReturnValue ( Effect . succeed ( output ( "{}" ) ) ) ;
1000+ const cli = yield * GitLabPullRequestCli . GitLabPullRequestCli ;
1001+
1002+ const result = yield * Effect . result (
1003+ cli . runMergeRequestAction ( {
1004+ cwd : "/w" ,
1005+ repository : "acme/web" ,
1006+ number : 7 ,
1007+ action : "revert" ,
1008+ } ) ,
1009+ ) ;
1010+
1011+ expect ( argsOfCall ( 1 ) [ 1 ] ) . toBe ( "projects/acme%2Fweb/merge_requests/7/commits?per_page=2" ) ;
1012+ if ( expected === "GitLabRevertUnavailableError" ) {
1013+ expect ( result ) . toMatchObject ( { _tag : "Failure" , failure : { _tag : expected } } ) ;
1014+ } else {
1015+ expect ( JSON . parse ( callAt ( 2 ) . stdin ?? "" ) ) . toEqual ( { branch : expected , ref : "main" } ) ;
1016+ }
1017+ } ) ,
1018+ ) ;
1019+
1020+ it . effect ( "refuses to revert a merge request that is not merged before any write" , ( ) =>
1021+ Effect . gen ( function * ( ) {
1022+ mockedExecute
1023+ . mockReturnValueOnce (
1024+ Effect . succeed (
1025+ output (
1026+ mergeRequestJson ( {
1027+ state : "opened" ,
1028+ merge_commit_sha : null ,
1029+ squash_commit_sha : null ,
1030+ } ) ,
1031+ ) ,
1032+ ) ,
1033+ )
1034+ . mockReturnValue ( Effect . succeed ( output ( JSON . stringify ( [ commitJson ( "1111111122" ) ] ) ) ) ) ;
1035+ const cli = yield * GitLabPullRequestCli . GitLabPullRequestCli ;
1036+
1037+ const result = yield * Effect . result (
1038+ cli . runMergeRequestAction ( {
1039+ cwd : "/w" ,
1040+ repository : "acme/web" ,
1041+ number : 7 ,
1042+ action : "revert" ,
1043+ } ) ,
1044+ ) ;
1045+
1046+ expect ( result ) . toMatchObject ( {
1047+ _tag : "Failure" ,
1048+ failure : { _tag : "GitLabRevertUnavailableError" , reason : "not-merged" } ,
1049+ } ) ;
1050+ expect ( mockedExecute ) . toHaveBeenCalledTimes ( 1 ) ;
1051+ } ) ,
1052+ ) ;
1053+
8741054 it . effect ( "sends a comment body over stdin, never in argv" , ( ) =>
8751055 Effect . gen ( function * ( ) {
8761056 mockedExecute . mockReturnValueOnce ( Effect . succeed ( output ( "" ) ) ) ;
0 commit comments