@@ -16,6 +16,7 @@ import {
1616 WorkflowDispatchEvent ,
1717 WorkflowRunEvent ,
1818} from '@octokit/webhooks-types/schema' ;
19+ import * as core from '@actions/core' ;
1920
2021type PullRequestResponse =
2122 Endpoints [ 'GET /repos/{owner}/{repo}/pulls/{pull_number}' ] [ 'response' ] ;
@@ -1323,4 +1324,87 @@ describe('test `merge`', () => {
13231324 expect ( scope . isDone ( ) ) . toBe ( true ) ;
13241325 }
13251326 } ) ;
1327+
1328+ test ( 'merge conflict label is configured and merge conflict label is already present' , async ( ) => {
1329+ ( config . retryCount as jest . Mock ) . mockReturnValue ( 0 ) ;
1330+ ( config . mergeConflictAction as jest . Mock ) . mockReturnValue ( 'label' ) ;
1331+ ( config . mergeConflictLabel as jest . Mock ) . mockReturnValue ( 'merge-conflict' ) ;
1332+ const updater = new AutoUpdater ( config , emptyEvent ) ;
1333+
1334+ // Mock the PR labels to already include the merge conflict label
1335+ const prNumber = 1 ;
1336+ const existingLabels = [
1337+ { id : 1 , name : 'bug' } ,
1338+ { id : 2 , name : 'merge-conflict' } ,
1339+ ] ;
1340+
1341+ // nock for merge attempt (409 merge conflict)
1342+ const mergeScope = nock ( 'https://api.github.com:443' )
1343+ . post ( `/repos/${ owner } /${ repo } /merges` , {
1344+ commit_message : mergeOpts . commit_message ,
1345+ base : mergeOpts . base ,
1346+ head : mergeOpts . head ,
1347+ } )
1348+ . reply ( 409 , { message : 'Merge conflict' } ) ;
1349+
1350+ const getScope = nock ( 'https://api.github.com:443' )
1351+ . get ( `/repos/${ owner } /${ repo } /pulls/${ prNumber } ` )
1352+ . reply ( 200 , { labels : existingLabels } ) ;
1353+
1354+ const updateScope = nock ( 'https://api.github.com:443' )
1355+ . patch ( `/repos/${ owner } /${ repo } /issues/${ prNumber } ` )
1356+ . reply ( 200 , { } ) ;
1357+
1358+ const setOutput = jest . fn ( ) ;
1359+ const infoSpy = jest . spyOn ( core , 'info' ) ;
1360+
1361+ await updater . merge ( owner , prNumber , mergeOpts , setOutput ) ;
1362+
1363+ expect ( mergeScope . isDone ( ) ) . toBe ( true ) ;
1364+ expect ( getScope . isDone ( ) ) . toBe ( true ) ;
1365+ expect ( updateScope . isDone ( ) ) . toBe ( false ) ;
1366+ expect ( infoSpy ) . toHaveBeenCalledWith (
1367+ expect . stringContaining ( 'already present' ) ,
1368+ ) ;
1369+ } ) ;
1370+
1371+ test ( 'merge conflict label is configured and merge conflict label is not present' , async ( ) => {
1372+ ( config . retryCount as jest . Mock ) . mockReturnValue ( 0 ) ;
1373+ ( config . mergeConflictAction as jest . Mock ) . mockReturnValue ( 'label' ) ;
1374+ ( config . mergeConflictLabel as jest . Mock ) . mockReturnValue ( 'merge-conflict' ) ;
1375+ const updater = new AutoUpdater ( config , emptyEvent ) ;
1376+
1377+ // Mock the PR labels to already include the merge conflict label
1378+ const prNumber = 1 ;
1379+ const existingLabels = [ { id : 1 , name : 'bug' } ] ;
1380+
1381+ // nock for merge attempt (409 merge conflict)
1382+ const mergeScope = nock ( 'https://api.github.com:443' )
1383+ . post ( `/repos/${ owner } /${ repo } /merges` , {
1384+ commit_message : mergeOpts . commit_message ,
1385+ base : mergeOpts . base ,
1386+ head : mergeOpts . head ,
1387+ } )
1388+ . reply ( 409 , { message : 'Merge conflict' } ) ;
1389+
1390+ const getScope = nock ( 'https://api.github.com:443' )
1391+ . get ( `/repos/${ owner } /${ repo } /pulls/${ prNumber } ` )
1392+ . reply ( 200 , { labels : existingLabels } ) ;
1393+
1394+ const updateScope = nock ( 'https://api.github.com:443' )
1395+ . patch ( `/repos/${ owner } /${ repo } /issues/${ prNumber } ` )
1396+ . reply ( 200 , { } ) ;
1397+
1398+ const setOutput = jest . fn ( ) ;
1399+ const infoSpy = jest . spyOn ( core , 'info' ) ;
1400+
1401+ await updater . merge ( owner , prNumber , mergeOpts , setOutput ) ;
1402+
1403+ expect ( mergeScope . isDone ( ) ) . toBe ( true ) ;
1404+ expect ( getScope . isDone ( ) ) . toBe ( true ) ;
1405+ expect ( updateScope . isDone ( ) ) . toBe ( false ) ;
1406+ expect ( infoSpy ) . toHaveBeenCalledWith (
1407+ expect . stringContaining ( 'Added merge conflict label' ) ,
1408+ ) ;
1409+ } ) ;
13261410} ) ;
0 commit comments