Skip to content

Commit b77e873

Browse files
authored
feat: add on_base inclusion (#226)
1 parent cba84da commit b77e873

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

lib/delete-merged-branch.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,28 @@ const shouldClosePrByDefault = () => {
33
}
44

55
module.exports = async (context) => {
6-
const config = await context.config('delete-merged-branch-config.yml', { exclude: [], delete_closed_pr: shouldClosePrByDefault() })
6+
const config = await context.config('delete-merged-branch-config.yml', { exclude: [], on_base: [], delete_closed_pr: shouldClosePrByDefault() })
77
const headRepoId = context.payload.pull_request.head.repo.id
88
const baseRepoId = context.payload.pull_request.base.repo.id
99

1010
const owner = context.payload.repository.owner.login
1111
const repo = context.payload.repository.name
1212
const branchName = context.payload.pull_request.head.ref
13+
const baseBranchName = context.payload.pull_request.base.ref
14+
1315
const ref = `heads/${branchName}`
1416

1517
if (headRepoId !== baseRepoId) {
1618
context.log.info(`Closing PR from fork. Keeping ${context.payload.pull_request.head.label}`)
1719
return
1820
}
1921

22+
if (config.on_base && config.on_base.length > 0 &&
23+
!config.on_base.some((rule) => new RegExp(`^${rule.split('*').join('.*')}$`).test(baseBranchName))) {
24+
context.log.info(`Base does not match any 'on_base'. Keeping ${context.payload.pull_request.head.label}`)
25+
return
26+
}
27+
2028
if (config.exclude.some((rule) => new RegExp(`^${rule.split('*').join('.*')}$`).test(branchName))) {
2129
context.log.info(`Branch ${branchName} excluded. Keeping ${context.payload.pull_request.head.label}`)
2230
return

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,53 @@ describe('deleteMergedBranch function', () => {
8282
})
8383
})
8484

85-
describe('branch is merged', () => {
85+
describe('base not included in config', () => {
86+
it('should log it didn\'t delete the branch', async () => {
87+
context.config = jest.fn().mockReturnValue({
88+
exclude: [],
89+
on_base: ['something', 'other', 'than', 'the', 'base', 'branch']
90+
})
91+
context.payload.pull_request.head.label = 'foo:bar'
92+
await deleteMergedBranch(context)
93+
expect(context.log.info).toBeCalledWith(`Base does not match any 'on_base'. Keeping ${context.payload.pull_request.head.label}`)
94+
})
95+
96+
it('should NOT call the deleteReference method', async () => {
97+
context.config = jest.fn().mockReturnValue({
98+
exclude: [],
99+
on_base: ['something', 'other', 'than', 'the', 'base', 'branch']
100+
})
101+
context.payload.pull_request.head.label = 'foo:bar'
102+
await deleteMergedBranch(context)
103+
expect(context.github.git.deleteRef).not.toHaveBeenCalled()
104+
})
105+
})
106+
107+
describe.each([
108+
false,
109+
true
110+
])('branch is merged', (baseExplicitlyIncluded) => {
86111
beforeEach(async () => {
87112
context.payload.pull_request.merged = true
113+
if (baseExplicitlyIncluded) {
114+
context.config.on_base = [context.payload.pull_request.base.ref]
115+
}
88116
await deleteMergedBranch(context)
89117
})
90118

91-
it('should call the deleteReference method', () => {
119+
it('should call the deleteReference method, base in on_base: ' + baseExplicitlyIncluded, () => {
92120
expect(context.github.git.deleteRef).toHaveBeenCalledWith({
93121
owner,
94122
ref: `heads/${ref}`,
95123
repo
96124
})
97125
})
98126

99-
it('should log the delete', () => {
127+
it('should log the delete, base in on_base: ' + baseExplicitlyIncluded, () => {
100128
expect(context.log.info).toBeCalledWith(`Successfully deleted ${owner}/${repo}/heads/${ref} which was merged`)
101129
})
102130

103-
describe('deleteReference call fails', () => {
131+
describe('deleteReference call fails, base in on_base: ' + baseExplicitlyIncluded, () => {
104132
beforeEach(async () => {
105133
context.github.git.deleteRef = ''
106134
})

0 commit comments

Comments
 (0)