@@ -1324,28 +1324,88 @@ describe('test `merge`', () => {
13241324 }
13251325 } ) ;
13261326
1327- test ( 'label merge conflicts ' , async ( ) => {
1327+ test ( 'merge conflict label is configured and merge conflict label is already present ' , async ( ) => {
13281328 ( config . retryCount as jest . Mock ) . mockReturnValue ( 0 ) ;
13291329 ( config . mergeConflictAction as jest . Mock ) . mockReturnValue ( 'label' ) ;
1330- ( config . mergeConflictLabel as jest . Mock ) . mockReturnValue ( 'conflicted ' ) ;
1330+ ( config . mergeConflictLabel as jest . Mock ) . mockReturnValue ( 'merge-conflict ' ) ;
13311331 const updater = new AutoUpdater ( config , emptyEvent ) ;
13321332
1333- const scope = nock ( 'https://api.github.com:443' )
1333+ // Mock the PR labels to already include the merge conflict label
1334+ const prNumber = 1 ;
1335+ const existingLabels = [
1336+ { id : 1 , name : 'bug' } ,
1337+ { id : 2 , name : 'merge-conflict' } ,
1338+ ] ;
1339+
1340+ // nock for merge attempt (409 merge conflict)
1341+ const mergeScope = nock ( 'https://api.github.com:443' )
13341342 . post ( `/repos/${ owner } /${ repo } /merges` , {
13351343 commit_message : mergeOpts . commit_message ,
13361344 base : mergeOpts . base ,
13371345 head : mergeOpts . head ,
13381346 } )
1339- . reply ( 409 , {
1340- message : 'Merge conflict' ,
1341- } ) ;
1347+ . reply ( 409 , { message : 'Merge conflict' } ) ;
1348+
1349+ const getScope = nock ( 'https://api.github.com:443' )
1350+ . get ( `/repos/${ owner } /${ repo } /pulls/${ prNumber } ` )
1351+ . reply ( 200 , { labels : existingLabels } ) ;
1352+
1353+ const updateScope = nock ( 'https://api.github.com:443' )
1354+ . patch ( `/repos/${ owner } /${ repo } /issues/${ prNumber } ` )
1355+ . reply ( 200 , { } ) ;
13421356
13431357 const setOutput = jest . fn ( ) ;
1344- await updater . merge ( owner , 1 , mergeOpts , setOutput ) ;
1358+ const infoSpy = jest . spyOn ( require ( '@actions/core' ) , 'info' ) ;
13451359
1346- expect ( scope . isDone ( ) ) . toEqual ( true ) ;
1360+ await updater . merge ( owner , prNumber , mergeOpts , setOutput ) ;
13471361
1348- expect ( setOutput ) . toHaveBeenCalledTimes ( 1 ) ;
1349- expect ( setOutput ) . toHaveBeenCalledWith ( Output . Conflicted , true ) ;
1362+ expect ( mergeScope . isDone ( ) ) . toBe ( true ) ;
1363+ expect ( getScope . isDone ( ) ) . toBe ( true ) ;
1364+ expect ( updateScope . isDone ( ) ) . toBe ( false ) ;
1365+ expect ( infoSpy ) . toHaveBeenCalledWith (
1366+ expect . stringContaining ( "already present" )
1367+ ) ;
1368+ } ) ;
1369+
1370+ test ( 'merge conflict label is configured and merge conflict label is not present' , async ( ) => {
1371+ ( config . retryCount as jest . Mock ) . mockReturnValue ( 0 ) ;
1372+ ( config . mergeConflictAction as jest . Mock ) . mockReturnValue ( 'label' ) ;
1373+ ( config . mergeConflictLabel as jest . Mock ) . mockReturnValue ( 'merge-conflict' ) ;
1374+ const updater = new AutoUpdater ( config , emptyEvent ) ;
1375+
1376+ // Mock the PR labels to already include the merge conflict label
1377+ const prNumber = 1 ;
1378+ const existingLabels = [
1379+ { id : 1 , name : 'bug' } ,
1380+ ] ;
1381+
1382+ // nock for merge attempt (409 merge conflict)
1383+ const mergeScope = nock ( 'https://api.github.com:443' )
1384+ . post ( `/repos/${ owner } /${ repo } /merges` , {
1385+ commit_message : mergeOpts . commit_message ,
1386+ base : mergeOpts . base ,
1387+ head : mergeOpts . head ,
1388+ } )
1389+ . reply ( 409 , { message : 'Merge conflict' } ) ;
1390+
1391+ const getScope = nock ( 'https://api.github.com:443' )
1392+ . get ( `/repos/${ owner } /${ repo } /pulls/${ prNumber } ` )
1393+ . reply ( 200 , { labels : existingLabels } ) ;
1394+
1395+ const updateScope = nock ( 'https://api.github.com:443' )
1396+ . patch ( `/repos/${ owner } /${ repo } /issues/${ prNumber } ` )
1397+ . reply ( 200 , { } ) ;
1398+
1399+ const setOutput = jest . fn ( ) ;
1400+ const infoSpy = jest . spyOn ( require ( '@actions/core' ) , 'info' ) ;
1401+
1402+ await updater . merge ( owner , prNumber , mergeOpts , setOutput ) ;
1403+
1404+ expect ( mergeScope . isDone ( ) ) . toBe ( true ) ;
1405+ expect ( getScope . isDone ( ) ) . toBe ( true ) ;
1406+ expect ( updateScope . isDone ( ) ) . toBe ( false ) ;
1407+ expect ( infoSpy ) . toHaveBeenCalledWith (
1408+ expect . stringContaining ( "Added merge conflict label" )
1409+ ) ;
13501410 } ) ;
13511411} ) ;
0 commit comments