Skip to content

Commit

Permalink
Earlier return in event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
vbihun committed Sep 20, 2024
1 parent 67016bd commit 44fa2b7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/entry/webtriggers/process-gitlab-event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('processGitlabEvent', () => {
await processGitlabEvent(webtriggerRequest);

expect(mockHandlePushEvent).toHaveBeenCalledWith(MOCK_PUSH_EVENT, TEST_TOKEN, MOCK_CLOUD_ID);
expect(serverResponse).toHaveBeenCalledWith('Processed webhook event');
expect(serverResponse).toHaveBeenCalledWith('Processed webhook event of type PUSH');
});

it('returns server response error in case of invalid webhook event secret', async () => {
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('processGitlabEvent', () => {
await processGitlabEvent(webtriggerRequest);

expect(mockHandlePipelineEvent).toHaveBeenCalledWith(MOCK_PIPELINE_EVENT, TEST_TOKEN, MOCK_CLOUD_ID);
expect(serverResponse).toHaveBeenCalledWith('Processed webhook event');
expect(serverResponse).toHaveBeenCalledWith('Processed webhook event of type PIPELINE');
});

it('handles merge request event', async () => {
Expand All @@ -111,7 +111,7 @@ describe('processGitlabEvent', () => {
await processGitlabEvent(webtriggerRequest);

expect(mockHandleMergeRequestEvent).toHaveBeenCalledWith(MOCK_MERGE_REQUEST_EVENT, TEST_TOKEN, MOCK_CLOUD_ID);
expect(serverResponse).toHaveBeenCalledWith('Processed webhook event');
expect(serverResponse).toHaveBeenCalledWith('Processed webhook event of type MERGE_REQUEST');
});

it('handles deployment event when FF is enabled', async () => {
Expand All @@ -120,6 +120,6 @@ describe('processGitlabEvent', () => {
await processGitlabEvent(webtriggerRequest);

expect(mockDeploymentEvent).toHaveBeenCalledWith(MOCK_DEPLOYMENT_EVENT, TEST_TOKEN, MOCK_CLOUD_ID);
expect(serverResponse).toHaveBeenCalledWith('Processed webhook event');
expect(serverResponse).toHaveBeenCalledWith('Processed webhook event of type DEPLOYMENT');
});
});
17 changes: 13 additions & 4 deletions src/entry/webtriggers/process-gitlab-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
MergeRequestEvent,
PipelineEvent,
PushEvent,
WebhookTypes,
WebtriggerRequest,
WebtriggerResponse,
} from '../../types';
Expand Down Expand Up @@ -47,20 +48,28 @@ export const processGitlabEvent = async (event: WebtriggerRequest): Promise<Webt

const parsedEvent = parseEventPayload(eventPayload);

if (parsedEvent.object_kind === 'push') {
if (parsedEvent.object_kind === WebhookTypes.PUSH) {
await handlePushEvent(parsedEvent as PushEvent, groupToken, cloudId);

return serverResponse('Processed webhook event of type PUSH');
}

if (parsedEvent.object_kind === 'merge_request') {
if (parsedEvent.object_kind === WebhookTypes.MERGE_REQUEST) {
await handleMergeRequestEvent(parsedEvent as MergeRequestEvent, groupToken, cloudId);

return serverResponse('Processed webhook event of type MERGE_REQUEST');
}

if (parsedEvent.object_kind === 'pipeline') {
if (parsedEvent.object_kind === WebhookTypes.PIPELINE) {
await handlePipelineEvent(parsedEvent as PipelineEvent, groupToken, cloudId);

return serverResponse('Processed webhook event of type PIPELINE');
}

if (parsedEvent.object_kind === 'deployment') {
if (parsedEvent.object_kind === WebhookTypes.DEPLOYMENT) {
await handleDeploymentEvent(parsedEvent as DeploymentEvent, groupToken, cloudId);

return serverResponse('Processed webhook event of type DEPLOYMENT');
}

return serverResponse('Processed webhook event');
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,13 @@ enum GitLabAccessLevels {
OWNER = 50,
}

enum WebhookTypes {
PUSH = 'push',
MERGE_REQUEST = 'merge_request',
PIPELINE = 'pipeline',
DEPLOYMENT = 'deployment',
}

type Environment = {
id: number;
name: string;
Expand Down Expand Up @@ -450,4 +457,5 @@ export {
EnvironmentTier,
GitlabPipelineStates,
GitLabAccessLevels,
WebhookTypes,
};

0 comments on commit 44fa2b7

Please sign in to comment.