Skip to content

Commit

Permalink
fix(3225): organize the return values of _parseHook (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
y-oksaku authored Oct 21, 2024
1 parent b534ec9 commit f30ba16
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
14 changes: 6 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);

Expand Down
10 changes: 5 additions & 5 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,23 +367,23 @@ 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));
});

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));
});

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));
Expand Down Expand Up @@ -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'
Expand All @@ -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);
});
});

Expand Down

0 comments on commit f30ba16

Please sign in to comment.