Skip to content

Commit

Permalink
fix: forks should not trigger branch deletion (#23)
Browse files Browse the repository at this point in the history
* Added test case

* Fix: forks should not branch trigger deletion
  • Loading branch information
SvanBoxel committed Sep 12, 2018
1 parent 60c05e0 commit d4c4248
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/delete-merged-branch.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
module.exports = async (context) => {
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 ref = `heads/${context.payload.pull_request.head.ref}`

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

if (!context.payload.pull_request.merged) {
context.log.info(`PR was closed but not merged. Keeping ${owner}/${repo}/${ref}`)
return
Expand Down
19 changes: 18 additions & 1 deletion test/lib/delete-merged-branch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('deleteMergedBranch function', () => {
event: {
event: 'pull_request.closed'
},
payload,
payload: JSON.parse(JSON.stringify(payload)), // Njeh...
github: {
gitdata: {
deleteReference
Expand All @@ -30,6 +30,23 @@ describe('deleteMergedBranch function', () => {
repo = payload.repository.name
})

describe('branch is merged from fork', () => {
beforeEach(async () => {
context.payload.pull_request.base.repo.id = 200
context.payload.pull_request.head.repo.id = 100
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(`Closing PR from fork. Keeping ${context.payload.pull_request.head.label}`)
})

it('should NOT call the deleteReference method', () => {
expect(context.github.gitdata.deleteReference).not.toHaveBeenCalled()
})
})

describe('branch is merged', async () => {
beforeEach(async () => {
context.payload.pull_request.merged = true
Expand Down

0 comments on commit d4c4248

Please sign in to comment.