@@ -374,56 +374,111 @@ layer("GitLabPullRequestCli.layer", (it) => {
374374 } ) ,
375375 ) ;
376376
377- it . effect ( "merges immediately rather than leaving auto-merge armed" , ( ) =>
378- Effect . gen ( function * ( ) {
379- mockedExecute . mockReturnValueOnce ( Effect . succeed ( output ( "" ) ) ) ;
380- const cli = yield * GitLabPullRequestCli . GitLabPullRequestCli ;
381-
382- yield * cli . runMergeRequestAction ( {
383- cwd : "/w" ,
384- repository : "acme/web" ,
385- number : 7 ,
386- action : "merge" ,
387- mergeMethod : "squash" ,
388- } ) ;
389-
390- expect ( argsOfCall ( 0 ) ) . toEqual ( [
391- "mr" ,
392- "merge" ,
393- "7" ,
394- "--repo" ,
395- "acme/web" ,
396- "--auto-merge=false" ,
397- "--yes" ,
398- "--squash" ,
399- ] ) ;
400- } ) ,
377+ it . effect . each ( [
378+ { action : "merge" , autoMerge : "false" , mergeMethod : "squash" } ,
379+ { action : "enable-auto-merge" , autoMerge : "true" , mergeMethod : "squash" } ,
380+ { action : "merge" , autoMerge : "false" , mergeMethod : "rebase" } ,
381+ { action : "enable-auto-merge" , autoMerge : "true" , mergeMethod : "rebase" } ,
382+ ] as const ) (
383+ "$action with $mergeMethod succeeds on a project requiring the current source SHA" ,
384+ ( { action, autoMerge, mergeMethod } ) =>
385+ Effect . gen ( function * ( ) {
386+ const cli = yield * GitLabPullRequestCli . GitLabPullRequestCli ;
387+
388+ for ( const headSha of [ "first-head" , "updated-head" ] ) {
389+ let currentHeadSha = headSha ;
390+ mockedExecute . mockImplementation ( ( { cwd, args } ) => {
391+ if ( args [ 0 ] === "mr" && args [ 1 ] === "rebase" ) {
392+ return Effect . sync ( ( ) => {
393+ currentHeadSha = `${ headSha } -rebased` ;
394+ return output ( "" ) ;
395+ } ) ;
396+ }
397+ if ( args [ 0 ] === "api" ) {
398+ return Effect . succeed (
399+ output (
400+ mergeRequestJson ( {
401+ diff_refs : { base_sha : "base" , head_sha : currentHeadSha , start_sha : "start" } ,
402+ } ) ,
403+ ) ,
404+ ) ;
405+ }
406+ if ( args . includes ( "--rebase" ) || args [ args . indexOf ( "--sha" ) + 1 ] !== currentHeadSha ) {
407+ return Effect . fail (
408+ new GitLabCli . GitLabCliCommandError ( {
409+ operation : "execute" ,
410+ command : "glab" ,
411+ cwd,
412+ cause : new Error (
413+ args . includes ( "--rebase" )
414+ ? "Merge must not combine --rebase with --sha: rebase changes the source HEAD"
415+ : "SHA must match the current source branch HEAD" ,
416+ ) ,
417+ } ) ,
418+ ) ;
419+ }
420+ return Effect . succeed ( output ( "" ) ) ;
421+ } ) ;
422+
423+ yield * cli . runMergeRequestAction ( {
424+ cwd : "/w" ,
425+ repository : "acme/web" ,
426+ number : 7 ,
427+ action,
428+ mergeMethod,
429+ } ) ;
430+
431+ const callsPerAction = mergeMethod === "rebase" ? 3 : 2 ;
432+ expect (
433+ mockedExecute . mock . calls . slice ( - callsPerAction ) . map ( ( [ input ] ) => input . args ) ,
434+ ) . toEqual ( [
435+ ...( mergeMethod === "rebase" ? [ [ "mr" , "rebase" , "7" , "--repo" , "acme/web" ] ] : [ ] ) ,
436+ [ "api" , "projects/acme%2Fweb/merge_requests/7" ] ,
437+ [
438+ "mr" ,
439+ "merge" ,
440+ "7" ,
441+ "--repo" ,
442+ "acme/web" ,
443+ `--auto-merge=${ autoMerge } ` ,
444+ "--yes" ,
445+ ...( mergeMethod === "squash" ? [ "--squash" ] : [ ] ) ,
446+ "--sha" ,
447+ mergeMethod === "rebase" ? `${ headSha } -rebased` : headSha ,
448+ ] ,
449+ ] ) ;
450+ }
451+ expect ( mockedExecute ) . toHaveBeenCalledTimes ( mergeMethod === "rebase" ? 6 : 4 ) ;
452+ } ) ,
401453 ) ;
402454
403- it . effect ( "arms auto-merge with the same strategy a merge would have used" , ( ) =>
404- Effect . gen ( function * ( ) {
405- mockedExecute . mockReturnValueOnce ( Effect . succeed ( output ( "" ) ) ) ;
406- const cli = yield * GitLabPullRequestCli . GitLabPullRequestCli ;
407-
408- yield * cli . runMergeRequestAction ( {
409- cwd : "/w" ,
410- repository : "acme/web" ,
411- number : 7 ,
412- action : "enable-auto-merge" ,
413- mergeMethod : "squash" ,
414- } ) ;
455+ it . effect . each ( [ "merge" , "enable-auto-merge" ] as const ) (
456+ "does not run %s when GitLab has no diff revisions" ,
457+ ( action ) =>
458+ Effect . gen ( function * ( ) {
459+ mockedExecute . mockReturnValue (
460+ Effect . succeed ( output ( mergeRequestJson ( { diff_refs : null } ) ) ) ,
461+ ) ;
462+ const cli = yield * GitLabPullRequestCli . GitLabPullRequestCli ;
463+
464+ const result = yield * Effect . result (
465+ cli . runMergeRequestAction ( {
466+ cwd : "/w" ,
467+ repository : "acme/web" ,
468+ number : 7 ,
469+ action,
470+ mergeMethod : "squash" ,
471+ } ) ,
472+ ) ;
415473
416- expect ( argsOfCall ( 0 ) ) . toEqual ( [
417- "mr" ,
418- "merge" ,
419- "7" ,
420- "--repo" ,
421- "acme/web" ,
422- "--auto-merge=true" ,
423- "--yes" ,
424- "--squash" ,
425- ] ) ;
426- } ) ,
474+ expect ( mockedExecute . mock . calls . map ( ( [ input ] ) => input . args ) ) . toEqual ( [
475+ [ "api" , "projects/acme%2Fweb/merge_requests/7" ] ,
476+ ] ) ;
477+ expect ( result ) . toMatchObject ( {
478+ _tag : "Failure" ,
479+ failure : { _tag : "GitLabDiffRefsUnavailableError" } ,
480+ } ) ;
481+ } ) ,
427482 ) ;
428483
429484 it . effect ( "cancels an armed auto-merge through the API glab has no flag for" , ( ) =>
0 commit comments