From 21a14ad538326d90dbbf8699a6034cc9aeb90f07 Mon Sep 17 00:00:00 2001 From: Yuki Okesaku Date: Mon, 21 Oct 2024 11:01:38 +0900 Subject: [PATCH 1/2] fix: parse-hook error handling --- index.js | 14 ++++++-------- test/index.test.js | 8 ++++---- 2 files changed, 10 insertions(+), 12 deletions(-) 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..a39c34a 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)); @@ -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); }); }); From dd24c531406e0e781699e7529cb7ecea0d2ce7e2 Mon Sep 17 00:00:00 2001 From: y-oksaku <43719835+y-oksaku@users.noreply.github.com> Date: Tue, 22 Oct 2024 06:33:30 +0900 Subject: [PATCH 2/2] fix: test name --- test/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.test.js b/test/index.test.js index a39c34a..06bbf1f 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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'