Skip to content

Commit d4c4248

Browse files
committed
fix: forks should not trigger branch deletion (#23)
* Added test case * Fix: forks should not branch trigger deletion
1 parent 60c05e0 commit d4c4248

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/delete-merged-branch.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
module.exports = async (context) => {
2+
const headRepoId = context.payload.pull_request.head.repo.id
3+
const baseRepoId = context.payload.pull_request.base.repo.id
4+
25
const owner = context.payload.repository.owner.login
36
const repo = context.payload.repository.name
47
const ref = `heads/${context.payload.pull_request.head.ref}`
58

9+
if (headRepoId !== baseRepoId) {
10+
context.log.info(`Closing PR from fork. Keeping ${context.payload.pull_request.head.label}`)
11+
return
12+
}
13+
614
if (!context.payload.pull_request.merged) {
715
context.log.info(`PR was closed but not merged. Keeping ${owner}/${repo}/${ref}`)
816
return

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('deleteMergedBranch function', () => {
1818
event: {
1919
event: 'pull_request.closed'
2020
},
21-
payload,
21+
payload: JSON.parse(JSON.stringify(payload)), // Njeh...
2222
github: {
2323
gitdata: {
2424
deleteReference
@@ -30,6 +30,23 @@ describe('deleteMergedBranch function', () => {
3030
repo = payload.repository.name
3131
})
3232

33+
describe('branch is merged from fork', () => {
34+
beforeEach(async () => {
35+
context.payload.pull_request.base.repo.id = 200
36+
context.payload.pull_request.head.repo.id = 100
37+
context.payload.pull_request.head.label = 'foo:bar'
38+
await deleteMergedBranch(context)
39+
})
40+
41+
it('should log it didn\'t delete the branch', () => {
42+
expect(context.log.info).toBeCalledWith(`Closing PR from fork. Keeping ${context.payload.pull_request.head.label}`)
43+
})
44+
45+
it('should NOT call the deleteReference method', () => {
46+
expect(context.github.gitdata.deleteReference).not.toHaveBeenCalled()
47+
})
48+
})
49+
3350
describe('branch is merged', async () => {
3451
beforeEach(async () => {
3552
context.payload.pull_request.merged = true

0 commit comments

Comments
 (0)