From e0c159e82ce73b24398aa9ce88a216b5c4e5a7d2 Mon Sep 17 00:00:00 2001 From: Sebass van Boxel Date: Wed, 12 Sep 2018 21:41:39 +0200 Subject: [PATCH 1/2] Added test case --- test/lib/delete-merged-branch.test.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/lib/delete-merged-branch.test.js b/test/lib/delete-merged-branch.test.js index 034fed0..5626aaa 100644 --- a/test/lib/delete-merged-branch.test.js +++ b/test/lib/delete-merged-branch.test.js @@ -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 = 'foobar' + 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 From 037a986ab6f4938f5297baf05fa782b9bf397e56 Mon Sep 17 00:00:00 2001 From: Sebass van Boxel Date: Wed, 12 Sep 2018 21:54:33 +0200 Subject: [PATCH 2/2] Fix: forks should not branch trigger deletion --- lib/delete-merged-branch.js | 8 ++++++++ test/lib/delete-merged-branch.test.js | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/delete-merged-branch.js b/lib/delete-merged-branch.js index c433b91..c8f2af5 100644 --- a/lib/delete-merged-branch.js +++ b/lib/delete-merged-branch.js @@ -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 diff --git a/test/lib/delete-merged-branch.test.js b/test/lib/delete-merged-branch.test.js index 5626aaa..6c32189 100644 --- a/test/lib/delete-merged-branch.test.js +++ b/test/lib/delete-merged-branch.test.js @@ -18,7 +18,7 @@ describe('deleteMergedBranch function', () => { event: { event: 'pull_request.closed' }, - payload, + payload: JSON.parse(JSON.stringify(payload)), // Njeh... github: { gitdata: { deleteReference @@ -34,7 +34,7 @@ describe('deleteMergedBranch function', () => { beforeEach(async () => { context.payload.pull_request.base.repo.id = 200 context.payload.pull_request.head.repo.id = 100 - context.payload.pull_request.head.label = 'foobar' + context.payload.pull_request.head.label = 'foo:bar' await deleteMergedBranch(context) })