diff --git a/lib/automations/release/utils/duplicate-pr-checker.js b/lib/automations/release/utils/duplicate-pr-checker.js index 06f3ae56..044b0620 100644 --- a/lib/automations/release/utils/duplicate-pr-checker.js +++ b/lib/automations/release/utils/duplicate-pr-checker.js @@ -16,14 +16,20 @@ */ const duplicateChecker = async ( context, octokit, title ) => { const search = await octokit.search.issuesAndPullRequests( { - q: `${ title } in:title type:pr state:open repo:${ context.payload.repository.full_name }`, + q: `${ title } in:title type:pr repo:${ context.payload.repository.full_name }`, per_page: 50, } ); if ( search.data.total_count !== 0 ) { - const existingPr = search.data.items.find( - ( pr ) => pr.title === title - ); + const existingPr = search.data.items.find( ( pr ) => { + if ( + pr.title !== title || + ( pr.merged === true && pr.state === 'closed' ) + ) { + return false; + } + return true; + } ); return existingPr; } }; diff --git a/lib/automations/release/utils/test/duplicate-pr-checker.js b/lib/automations/release/utils/test/duplicate-pr-checker.js index ac770f4b..2bc16163 100644 --- a/lib/automations/release/utils/test/duplicate-pr-checker.js +++ b/lib/automations/release/utils/test/duplicate-pr-checker.js @@ -32,4 +32,40 @@ describe( 'check-for-duplicate-pull', () => { const actual = await duplicateChecker( context, octokit, 'hello' ); expect( actual ).toBe( mock.data.items[ 0 ] ); } ); + it( 'returns the pull request if the pr is closed, matches the title and is not merged', async () => { + // mocked pull + const mock = { + data: { + total_count: 1, + items: [ + { + title: 'hello', + merged: false, + state: 'closed', + }, + ], + }, + }; + octokit.search.issuesAndPullRequests.mockReturnValueOnce( mock ); + const actual = await duplicateChecker( context, octokit, 'hello' ); + expect( actual ).toBe( mock.data.items[ 0 ] ); + } ); + it( 'returns undefined if the pr is closed, matches the title, and is merged', async () => { + // mocked pull + const mock = { + data: { + total_count: 1, + items: [ + { + title: 'hello', + merged: true, + state: 'closed', + }, + ], + }, + }; + octokit.search.issuesAndPullRequests.mockReturnValueOnce( mock ); + const actual = await duplicateChecker( context, octokit, 'hello' ); + expect( actual ).toBe( undefined ); + } ); } );