Skip to content

Commit

Permalink
feat: add on_base inclusion (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
seeplusplus authored Jan 30, 2022
1 parent cba84da commit b77e873
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/delete-merged-branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@ 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) {
context.log.info(`Closing PR from fork. Keeping ${context.payload.pull_request.head.label}`)
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
Expand Down
36 changes: 32 additions & 4 deletions test/lib/delete-merged-branch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,53 @@ 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}`,
repo
})
})

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 = ''
})
Expand Down

0 comments on commit b77e873

Please sign in to comment.