diff --git a/lib/delete-merged-branch.js b/lib/delete-merged-branch.js index 59b5817..1f4a368 100644 --- a/lib/delete-merged-branch.js +++ b/lib/delete-merged-branch.js @@ -3,13 +3,15 @@ const shouldClosePrByDefault = () => { } module.exports = async (context) => { - const config = await context.config('delete-merged-branch-config.yml', { exclude: [], delete_closed_pr: shouldClosePrByDefault() }) + const config = await context.config('delete-merged-branch-config.yml', { exclude: [], on_base: [], delete_closed_pr: shouldClosePrByDefault() }) const headRepoId = context.payload.pull_request.head.repo.id const baseRepoId = context.payload.pull_request.base.repo.id const owner = context.payload.repository.owner.login const repo = context.payload.repository.name const branchName = context.payload.pull_request.head.ref + const baseBranchName = context.payload.pull_request.base.ref + const ref = `heads/${branchName}` if (headRepoId !== baseRepoId) { @@ -17,6 +19,12 @@ module.exports = async (context) => { return } + if (config.on_base && config.on_base.length > 0 && + !config.on_base.some((rule) => new RegExp(`^${rule.split('*').join('.*')}$`).test(baseBranchName))) { + context.log.info(`Base does not match any 'on_base'. Keeping ${context.payload.pull_request.head.label}`) + return + } + if (config.exclude.some((rule) => new RegExp(`^${rule.split('*').join('.*')}$`).test(branchName))) { context.log.info(`Branch ${branchName} excluded. Keeping ${context.payload.pull_request.head.label}`) return diff --git a/test/lib/delete-merged-branch.test.js b/test/lib/delete-merged-branch.test.js index 73addbf..c6f0adf 100644 --- a/test/lib/delete-merged-branch.test.js +++ b/test/lib/delete-merged-branch.test.js @@ -82,13 +82,41 @@ describe('deleteMergedBranch function', () => { }) }) - describe('branch is merged', () => { + describe('base not included in config', () => { + it('should log it didn\'t delete the branch', async () => { + context.config = jest.fn().mockReturnValue({ + exclude: [], + on_base: ['something', 'other', 'than', 'the', 'base', 'branch'] + }) + context.payload.pull_request.head.label = 'foo:bar' + await deleteMergedBranch(context) + expect(context.log.info).toBeCalledWith(`Base does not match any 'on_base'. Keeping ${context.payload.pull_request.head.label}`) + }) + + it('should NOT call the deleteReference method', async () => { + context.config = jest.fn().mockReturnValue({ + exclude: [], + on_base: ['something', 'other', 'than', 'the', 'base', 'branch'] + }) + context.payload.pull_request.head.label = 'foo:bar' + await deleteMergedBranch(context) + expect(context.github.git.deleteRef).not.toHaveBeenCalled() + }) + }) + + describe.each([ + false, + true + ])('branch is merged', (baseExplicitlyIncluded) => { beforeEach(async () => { context.payload.pull_request.merged = true + if (baseExplicitlyIncluded) { + context.config.on_base = [context.payload.pull_request.base.ref] + } await deleteMergedBranch(context) }) - it('should call the deleteReference method', () => { + it('should call the deleteReference method, base in on_base: ' + baseExplicitlyIncluded, () => { expect(context.github.git.deleteRef).toHaveBeenCalledWith({ owner, ref: `heads/${ref}`, @@ -96,11 +124,11 @@ describe('deleteMergedBranch function', () => { }) }) - it('should log the delete', () => { + it('should log the delete, base in on_base: ' + baseExplicitlyIncluded, () => { expect(context.log.info).toBeCalledWith(`Successfully deleted ${owner}/${repo}/heads/${ref} which was merged`) }) - describe('deleteReference call fails', () => { + describe('deleteReference call fails, base in on_base: ' + baseExplicitlyIncluded, () => { beforeEach(async () => { context.github.git.deleteRef = '' })