Skip to content

Commit 024eaba

Browse files
authored
feat: Support for wildcards in branch excludes (#50)
* Specs for exclude wildcard support * exclude config wildcard support * Updated readme * Update README.md
1 parent ddbe2ea commit 024eaba

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ This GitHub app listens to the `pull_request.closed` webhook. If a pull request
6868
## Configuration
6969
The optional app configuration YAML file should be saved as `.github/delete-merged-branch-config.yml`. At the moment it supports the following options:
7070

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

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

7575
```
7676
exclude:
7777
- development
7878
- qa
79+
- feature-*
7980
```
8081

8182
## Release process

lib/delete-merged-branch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = async (context) => {
1313
return
1414
}
1515

16-
if (config.exclude.includes(branchName)) {
16+
if (config.exclude.some((rule) => new RegExp(`^${rule.split('*').join('.*')}$`).test(branchName))) {
1717
context.log.info(`Branch ${branchName} excluded. Keeping ${context.payload.pull_request.head.label}`)
1818
return
1919
}

test/lib/delete-merged-branch.test.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,37 @@ describe('deleteMergedBranch function', () => {
4949
})
5050

5151
describe('branch is excluded in config', () => {
52-
beforeEach(async () => {
52+
it('should log it didn\'t delete the branch', async () => {
5353
context.config = jest.fn().mockReturnValue({ exclude: [context.payload.pull_request.head.ref] })
5454
context.payload.pull_request.head.label = 'foo:bar'
5555
await deleteMergedBranch(context)
56-
})
57-
58-
it('should log it didn\'t delete the branch', () => {
5956
expect(context.log.info).toBeCalledWith(`Branch ${context.payload.pull_request.head.ref} excluded. Keeping ${context.payload.pull_request.head.label}`)
6057
})
6158

62-
it('should NOT call the deleteReference method', () => {
59+
it('should NOT call the deleteReference method', async () => {
60+
context.config = jest.fn().mockReturnValue({ exclude: [context.payload.pull_request.head.ref] })
61+
context.payload.pull_request.head.label = 'foo:bar'
62+
await deleteMergedBranch(context)
6363
expect(context.github.gitdata.deleteReference).not.toHaveBeenCalled()
6464
})
65+
66+
describe('wildcard expression is used', () => {
67+
it('should check for wildcard in end of string', async () => {
68+
const branchWilcard = `${context.payload.pull_request.head.ref.substr(0, 8)}*`
69+
context.config = jest.fn().mockReturnValue({ exclude: ['test', branchWilcard] })
70+
context.payload.pull_request.head.label = 'bar:foo'
71+
await deleteMergedBranch(context)
72+
expect(context.log.info).toBeCalledWith(`Branch ${context.payload.pull_request.head.ref} excluded. Keeping ${context.payload.pull_request.head.label}`)
73+
})
74+
75+
it('should check for wildcard in beginning of string', async () => {
76+
const branchWilcard = `*${context.payload.pull_request.head.ref.substr(1, 20)}`
77+
context.config = jest.fn().mockReturnValue({ exclude: ['test', branchWilcard] })
78+
context.payload.pull_request.head.label = 'bar:foobar'
79+
await deleteMergedBranch(context)
80+
expect(context.log.info).toBeCalledWith(`Branch ${context.payload.pull_request.head.ref} excluded. Keeping ${context.payload.pull_request.head.label}`)
81+
})
82+
})
6583
})
6684

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

0 commit comments

Comments
 (0)