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

Earlier return in event handler #123

Merged
merged 1 commit into from
Sep 23, 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
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,
};
Loading