Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(3225): organize the return values of _parseHook [2] #61

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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