diff --git a/index.js b/index.js index 61cbb3c..275ed31 100644 --- a/index.js +++ b/index.js @@ -376,6 +376,10 @@ class GitlabScm extends Scm { const commits = Hoek.reach(webhookPayload, 'commits'); const type = Hoek.reach(webhookPayload, 'object_kind'); + if (!Object.keys(payloadHeaders).includes('x-gitlab-event')) { + throwError('Missing x-gitlab-event header', 400); + } + switch (type) { case 'merge_request': { const mergeRequest = Hoek.reach(webhookPayload, 'object_attributes'); @@ -1234,16 +1238,10 @@ class GitlabScm extends Scm { * @return {Promise} */ async _canHandleWebhook(headers, payload) { - if (!Object.keys(headers).includes('x-gitlab-event')) { - logger.error('Failed to run canHandleWebhook'); - - return Promise.resolve(false); - } - try { - const result = await this._parseHook(headers, payload); + await this._parseHook(headers, payload); - return result !== null; + return true; } catch (err) { logger.error('Failed to run canHandleWebhook', err); diff --git a/test/index.test.js b/test/index.test.js index ae18804..06bbf1f 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -367,7 +367,7 @@ describe('index', function () { it('resolves null if events are not supported: repoFork', () => { const repoFork = { - 'x-event-key': 'repo:fork' + 'x-gitlab-event': 'repo:fork' }; return scm.parseHook(repoFork, {}).then(result => assert.deepEqual(result, null)); @@ -375,7 +375,7 @@ describe('index', function () { it('resolves null if events are not supported: prComment', () => { const prComment = { - 'x-event-key': 'pullrequest:comment_created' + 'x-gitlab-event': 'Note Hook' }; return scm.parseHook(prComment, {}).then(result => assert.deepEqual(result, null)); @@ -383,7 +383,7 @@ describe('index', function () { it('resolves null if events are not supported: issueCreated', () => { const issueCreated = { - 'x-event-key': 'issue:created' + 'x-gitlab-event': 'Issue Hook' }; return scm.parseHook(issueCreated, {}).then(result => assert.deepEqual(result, null)); @@ -2174,7 +2174,7 @@ describe('index', function () { }); }); - it('returns a false when parseHook resolves null', () => { + it('returns a true when parseHook resolves null', () => { const headers = { 'content-type': 'application/json', 'x-gitlab-event': 'Push Hook' @@ -2184,7 +2184,7 @@ describe('index', function () { scm._parseHook.resolves(null); return scm.canHandleWebhook(headers, testPayloadOpen).then(result => { - assert.strictEqual(result, false); + assert.strictEqual(result, true); }); });