Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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