From cb13bab825365eb02db63b3083d7bc8c0a41dd66 Mon Sep 17 00:00:00 2001 From: Josiah Adenegan Date: Mon, 11 Feb 2019 11:07:02 +0100 Subject: [PATCH 1/2] feat: Support webhook url config key --- config/config.yaml | 5 +++ server/service/GithubService.js | 3 +- test/server/github.test.js | 71 +++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/config/config.yaml b/config/config.yaml index 25bd8ca9..e4ea8017 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -66,6 +66,11 @@ VALID_PR_TEMPLATE_PATHS: - "/PULL_REQUEST_TEMPLATE" - "/PULL_REQUEST_TEMPLATE.txt" - "/PULL_REQUEST_TEMPLATE.md" + +# Explicit webhook url to use for receiving webhooks from Github +# If this is commented out, Zappr uses "/api/hook' as the webhook url +# HOOK_URL: http://mywebhookdomain/zappr/api/hook + ZAPPR_DEFAULT_CONFIG: autobranch: pattern: "{number}-{title}" diff --git a/server/service/GithubService.js b/server/service/GithubService.js index 684d9055..f6293d0d 100644 --- a/server/service/GithubService.js +++ b/server/service/GithubService.js @@ -268,7 +268,8 @@ export class GithubService { async updateWebhookFor(user, repo, events, accessToken) { debug(`${user}/${repo}: updating webhook with events: ${events.join(", ")}`) let path = API_URL_TEMPLATES.HOOK.replace('${owner}', user).replace('${repo}', repo) - let hook_url = nconf.get('HOST_ADDR') + '/api/hook' + const HOOK_URL = nconf.get('HOOK_URL') + let hook_url = HOOK_URL ? HOOK_URL : nconf.get('HOST_ADDR') + '/api/hook' // payload for hook let payload = { name: 'web', diff --git a/test/server/github.test.js b/test/server/github.test.js index 62e54c00..87f9f930 100644 --- a/test/server/github.test.js +++ b/test/server/github.test.js @@ -317,4 +317,75 @@ describe('The Github service', () => { } }) }) + + describe('#updateWebhookFor', () => { + it('should build webhook url from host address', async(done) => { + try { + const USER = 'user' + const REPO = 'repo' + const TOKEN = 'token' + + github.fetchPath.returns([]) + nconf.set('HOOK_URL', null) + + await github.updateWebhookFor(USER, REPO, [], TOKEN) + expect(github.fetchPath.args).to.deep.equal([ + ['GET', `/repos/${USER}/${REPO}/hooks`, + null, + TOKEN], + ['POST', `/repos/${USER}/${REPO}/hooks`, + { + name: 'web', + active: true, + events: [], + config: { + url: nconf.get('HOST_ADDR') + '/api/hook', + content_type: 'json', + secret: nconf.get('GITHUB_HOOK_SECRET') + } + }, + TOKEN] + ]) + + done() + } catch (e) { + done(e) + } + }) + + it('should use provided webhook url', async(done) => { + try { + const USER = 'user' + const REPO = 'repo' + const TOKEN = 'token' + const HOOK_URL = 'hookurl' + + github.fetchPath.returns([]) + nconf.set('HOOK_URL', HOOK_URL) + + await github.updateWebhookFor(USER, REPO, [], TOKEN) + expect(github.fetchPath.args).to.deep.equal([ + ['GET', `/repos/${USER}/${REPO}/hooks`, + null, + TOKEN], + ['POST', `/repos/${USER}/${REPO}/hooks`, + { + name: 'web', + active: true, + events: [], + config: { + url: HOOK_URL, + content_type: 'json', + secret: nconf.get('GITHUB_HOOK_SECRET') + } + }, + TOKEN] + ]) + + done() + } catch (e) { + done(e) + } + }) + }) }) From 2770742ddbacab286a15bc8ec008af574c8fc8d6 Mon Sep 17 00:00:00 2001 From: Josiah Adenegan Date: Mon, 20 May 2019 16:56:46 +0200 Subject: [PATCH 2/2] add note about HOOK_URL config entry --- docs/run-your-own.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/run-your-own.md b/docs/run-your-own.md index 862a8134..fe14fa68 100644 --- a/docs/run-your-own.md +++ b/docs/run-your-own.md @@ -30,6 +30,7 @@ Zappr provides a whole lot of configuration options for flexibility. * `GITHUB_HOOK_SECRET`: Secret value used to verify only Github calls Zappr * `GITHUB_UI_URL`: URL to Github web user interface * `GITHUB_API_URL`: URL to Github API +* `HOOK_URL`: Optional explicit webhook url to use for receiving webhooks from Github. Required if there is a proxy or load balancer that receives traffic from the internet and forwards it to Zappr. ### Server