Skip to content

Commit

Permalink
feat: Support for wildcards in branch excludes (#50)
Browse files Browse the repository at this point in the history
* Specs for exclude wildcard support

* exclude config wildcard support

* Updated readme

* Update README.md
  • Loading branch information
SvanBoxel authored Dec 20, 2018
1 parent ddbe2ea commit 024eaba
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ This GitHub app listens to the `pull_request.closed` webhook. If a pull request
## Configuration
The optional app configuration YAML file should be saved as `.github/delete-merged-branch-config.yml`. At the moment it supports the following options:

- `exclude` _(array)_ - list of branches that should not be automatically deleted after a merge.
- `exclude` _(array)_ - list of branches that should not be automatically deleted after a merge. Wildcards supported.

Example `.github/delete-merged-branch-config.yml`:

```
exclude:
- development
- qa
- feature-*
```

## Release process
Expand Down
2 changes: 1 addition & 1 deletion lib/delete-merged-branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = async (context) => {
return
}

if (config.exclude.includes(branchName)) {
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
28 changes: 23 additions & 5 deletions test/lib/delete-merged-branch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,37 @@ describe('deleteMergedBranch function', () => {
})

describe('branch is excluded in config', () => {
beforeEach(async () => {
it('should log it didn\'t delete the branch', async () => {
context.config = jest.fn().mockReturnValue({ exclude: [context.payload.pull_request.head.ref] })
context.payload.pull_request.head.label = 'foo:bar'
await deleteMergedBranch(context)
})

it('should log it didn\'t delete the branch', () => {
expect(context.log.info).toBeCalledWith(`Branch ${context.payload.pull_request.head.ref} excluded. Keeping ${context.payload.pull_request.head.label}`)
})

it('should NOT call the deleteReference method', () => {
it('should NOT call the deleteReference method', async () => {
context.config = jest.fn().mockReturnValue({ exclude: [context.payload.pull_request.head.ref] })
context.payload.pull_request.head.label = 'foo:bar'
await deleteMergedBranch(context)
expect(context.github.gitdata.deleteReference).not.toHaveBeenCalled()
})

describe('wildcard expression is used', () => {
it('should check for wildcard in end of string', async () => {
const branchWilcard = `${context.payload.pull_request.head.ref.substr(0, 8)}*`
context.config = jest.fn().mockReturnValue({ exclude: ['test', branchWilcard] })
context.payload.pull_request.head.label = 'bar:foo'
await deleteMergedBranch(context)
expect(context.log.info).toBeCalledWith(`Branch ${context.payload.pull_request.head.ref} excluded. Keeping ${context.payload.pull_request.head.label}`)
})

it('should check for wildcard in beginning of string', async () => {
const branchWilcard = `*${context.payload.pull_request.head.ref.substr(1, 20)}`
context.config = jest.fn().mockReturnValue({ exclude: ['test', branchWilcard] })
context.payload.pull_request.head.label = 'bar:foobar'
await deleteMergedBranch(context)
expect(context.log.info).toBeCalledWith(`Branch ${context.payload.pull_request.head.ref} excluded. Keeping ${context.payload.pull_request.head.label}`)
})
})
})

describe('branch is merged', async () => {
Expand Down

0 comments on commit 024eaba

Please sign in to comment.