diff --git a/lib/platform/bitbucket/index.js b/lib/platform/bitbucket/index.js index 829693bc5475218dd88d80641490db0492b21aaf..ce23b84ff00d986f41ed421e89e9333f995359a9 100644 --- a/lib/platform/bitbucket/index.js +++ b/lib/platform/bitbucket/index.js @@ -177,7 +177,17 @@ function getFile(filePath, branchName) { return config.storage.getFile(filePath, branchName); } -function deleteBranch(branchName) { +async function deleteBranch(branchName, closePr) { + if (closePr) { + const pr = await findPr(branchName, null, 'open'); + if (pr) { + await api.post( + `/2.0/repositories/${config.repository}/pullrequests/${ + pr.number + }/decline` + ); + } + } return config.storage.deleteBranch(branchName); } diff --git a/test/platform/bitbucket/__snapshots__/index.spec.js.snap b/test/platform/bitbucket/__snapshots__/index.spec.js.snap index 0c21d78b57f08d575fa39eef303630ce770b61f2..21421c6e37661517193f3fb4442b2ad1186208ee 100644 --- a/test/platform/bitbucket/__snapshots__/index.spec.js.snap +++ b/test/platform/bitbucket/__snapshots__/index.spec.js.snap @@ -46,6 +46,16 @@ Array [ ] `; +exports[`platform/bitbucket deleteBranch() should handle closing PRs when none exist 1`] = `Array []`; + +exports[`platform/bitbucket deleteBranch() should handle closing PRs when some exist 1`] = ` +Array [ + Array [ + "/2.0/repositories/some/repo/pullrequests/5/decline", + ], +] +`; + exports[`platform/bitbucket ensureIssue() creates new issue 1`] = ` Array [ Array [ diff --git a/test/platform/bitbucket/index.spec.js b/test/platform/bitbucket/index.spec.js index 5f8bde363efb4685458c04fafda0a1c443cf6b86..18f60675386b716089ca2e90af6c436d67b8ea4c 100644 --- a/test/platform/bitbucket/index.spec.js +++ b/test/platform/bitbucket/index.spec.js @@ -204,6 +204,20 @@ describe('platform/bitbucket', () => { await bitbucket.deleteBranch(); }); }); + it('should handle closing PRs when none exist', async () => { + await initRepo(); + await mocked(async () => { + await bitbucket.deleteBranch('some-branch', true); + expect(api.post.mock.calls).toMatchSnapshot(); + }); + }); + it('should handle closing PRs when some exist', async () => { + await initRepo(); + await mocked(async () => { + await bitbucket.deleteBranch('branch', true); + expect(api.post.mock.calls).toMatchSnapshot(); + }); + }); }); describe('mergeBranch()', () => {