From 8c9899559ef8b3198815201bec7aee774128aaa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Mart=C3=ADn=20Agra?= Date: Sat, 2 May 2020 19:02:45 +0200 Subject: [PATCH 1/3] feat: add 'MergeRequestMerged' to '/gitlab' route --- src/reactions/gitlab/index.ts | 12 +++++++ src/routes/gitlab/index.ts | 31 ++++++++++++++-- src/routes/index.ts | 2 +- test/routes/gitlab/merge-request-hook.spec.ts | 36 +++++++++++++++++++ 4 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 src/reactions/gitlab/index.ts create mode 100644 test/routes/gitlab/merge-request-hook.spec.ts diff --git a/src/reactions/gitlab/index.ts b/src/reactions/gitlab/index.ts new file mode 100644 index 0000000..f3c4811 --- /dev/null +++ b/src/reactions/gitlab/index.ts @@ -0,0 +1,12 @@ +import { TwitchChat } from '../../services/TwitchChat'; +import { StreamLabs } from '../../services/StreamLabs'; +import { MergeRequestMerged } from './merge-request-merged'; +import { Reaction } from '../github/reaction'; +import { MergeRequestPayload } from '../../schemas/gitlab/merge-request-payload'; + +export const buildGitLabReactions = ( + twitchChat: TwitchChat, + streamlabs: StreamLabs, +): Reaction[] => { + return [new MergeRequestMerged(twitchChat, streamlabs)]; +}; diff --git a/src/routes/gitlab/index.ts b/src/routes/gitlab/index.ts index 7775891..f2448ed 100644 --- a/src/routes/gitlab/index.ts +++ b/src/routes/gitlab/index.ts @@ -1,7 +1,12 @@ import { ServerRoute, ResponseObject, Request, ResponseToolkit } from '@hapi/hapi'; import { gitlabHeader } from '../../schemas/gitlab/joi/gitlab-headers-schema'; +import { buildGitLabReactions } from '../../reactions/gitlab'; +import { TwitchChat } from '../../services/TwitchChat'; +import { Config } from '../../config'; +import { StreamLabs } from '../../services/StreamLabs'; +import { MergeRequestPayload } from '../../schemas/gitlab/merge-request-payload'; -export const routes = (): ServerRoute[] => { +export const routes = (config: Config): ServerRoute[] => { return [ { method: 'POST', @@ -11,9 +16,31 @@ export const routes = (): ServerRoute[] => { }, }, handler: async (request: Request, h: ResponseToolkit): Promise => { - const { headers } = request; + const { headers, payload } = (request as unknown) as { + headers: { 'x-gitlab-event': string }; + payload: MergeRequestPayload; + }; const event = headers['x-gitlab-event']; + const twitchChat = new TwitchChat({ + botName: config?.TWITCH_BOT_NAME || '', + botToken: config?.TWITCH_BOT_TOKEN || '', + channel: config?.TWITCH_BOT_CHANNEL || '', + }); + const streamlabs = new StreamLabs({ token: config?.STREAMLABS_TOKEN || '' }); + + const reactions = buildGitLabReactions(twitchChat, streamlabs).filter((reaction) => + reaction.canHandle({ event, payload, config }), + ); + + if (reactions.length > 0) { + const messages = await Promise.all( + reactions.map((reaction) => reaction.handle({ payload })), + ); + + return h.response({ messages }).code(200); + } + return h.response({ message: `Ignoring event: '${event}'` }).code(200); }, path: '/gitlab', diff --git a/src/routes/index.ts b/src/routes/index.ts index 164e799..e98fd73 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -4,5 +4,5 @@ import { routes as github } from './github'; import { Config } from '../config'; export const routes = (config: Config): ServerRoute[] => { - return [...github(config), ...gitlab()]; + return [...github(config), ...gitlab(config)]; }; diff --git a/test/routes/gitlab/merge-request-hook.spec.ts b/test/routes/gitlab/merge-request-hook.spec.ts new file mode 100644 index 0000000..49f1b6a --- /dev/null +++ b/test/routes/gitlab/merge-request-hook.spec.ts @@ -0,0 +1,36 @@ +import { getConfig } from '../../../src/config'; +import { initServer } from '../../../src/server'; +import { StreamLabs } from '../../../src/services/StreamLabs'; +import { TwitchChat } from '../../../src/services/TwitchChat'; +import { MergeRequestPayloadBuilder } from '../../builders/payload/gitlab/merge-request-payload-builder'; + +describe('POST /gitlab', () => { + let streamLabsSpy: jest.SpyInstance>; + let twitchChatSpy: jest.SpyInstance>; + + beforeEach(() => { + streamLabsSpy = jest.spyOn(StreamLabs.prototype, 'alert'); + streamLabsSpy.mockImplementationOnce(jest.fn()); + + twitchChatSpy = jest.spyOn(TwitchChat.prototype, 'send'); + twitchChatSpy.mockImplementationOnce(jest.fn()); + }); + + describe('Merge Request Hook', () => { + it('handles merge request merged event', async () => { + const subject = await initServer(getConfig()); + const payload = new MergeRequestPayloadBuilder() + .with({ object_attributes: { state: 'merged' } }) + .getInstance(); + + const { result } = await subject.inject({ + method: 'POST', + url: '/gitlab', + payload, + headers: { 'x-gitlab-event': 'Merge Request Hook' }, + }); + + expect(result).toEqual(expect.objectContaining({ messages: expect.anything() })); + }); + }); +}); From 1e614cf0581effe658604b12ce853f4367da7aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Mart=C3=ADn=20Agra?= Date: Sat, 2 May 2020 19:06:38 +0200 Subject: [PATCH 2/3] feat: add 'MergeRequestOpened' to '/gitlab' route --- src/reactions/gitlab/index.ts | 8 ++++++-- test/routes/gitlab/merge-request-hook.spec.ts | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/reactions/gitlab/index.ts b/src/reactions/gitlab/index.ts index f3c4811..68cb178 100644 --- a/src/reactions/gitlab/index.ts +++ b/src/reactions/gitlab/index.ts @@ -1,12 +1,16 @@ import { TwitchChat } from '../../services/TwitchChat'; import { StreamLabs } from '../../services/StreamLabs'; import { MergeRequestMerged } from './merge-request-merged'; -import { Reaction } from '../github/reaction'; import { MergeRequestPayload } from '../../schemas/gitlab/merge-request-payload'; +import { MergeRequestOpened } from './merge-request-opened'; +import { Reaction } from '../github/reaction'; export const buildGitLabReactions = ( twitchChat: TwitchChat, streamlabs: StreamLabs, ): Reaction[] => { - return [new MergeRequestMerged(twitchChat, streamlabs)]; + return [ + new MergeRequestMerged(twitchChat, streamlabs), + new MergeRequestOpened(twitchChat, streamlabs), + ]; }; diff --git a/test/routes/gitlab/merge-request-hook.spec.ts b/test/routes/gitlab/merge-request-hook.spec.ts index 49f1b6a..f04f77d 100644 --- a/test/routes/gitlab/merge-request-hook.spec.ts +++ b/test/routes/gitlab/merge-request-hook.spec.ts @@ -32,5 +32,21 @@ describe('POST /gitlab', () => { expect(result).toEqual(expect.objectContaining({ messages: expect.anything() })); }); + + it('handles merge request opened event', async () => { + const subject = await initServer(getConfig()); + const payload = new MergeRequestPayloadBuilder() + .with({ object_attributes: { state: 'opened' } }) + .getInstance(); + + const { result } = await subject.inject({ + method: 'POST', + url: '/gitlab', + payload, + headers: { 'x-gitlab-event': 'Merge Request Hook' }, + }); + + expect(result).toEqual(expect.objectContaining({ messages: expect.anything() })); + }); }); }); From 7d41815410570ff9b9b8b28a4d7982b3bece8be7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Mart=C3=ADn=20Agra?= Date: Sat, 2 May 2020 22:50:40 +0200 Subject: [PATCH 3/3] test: improve integration test --- test/routes/gitlab/merge-request-hook.spec.ts | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/test/routes/gitlab/merge-request-hook.spec.ts b/test/routes/gitlab/merge-request-hook.spec.ts index f04f77d..b2531d6 100644 --- a/test/routes/gitlab/merge-request-hook.spec.ts +++ b/test/routes/gitlab/merge-request-hook.spec.ts @@ -30,7 +30,16 @@ describe('POST /gitlab', () => { headers: { 'x-gitlab-event': 'Merge Request Hook' }, }); - expect(result).toEqual(expect.objectContaining({ messages: expect.anything() })); + expect(result).toEqual( + expect.objectContaining({ + messages: [ + expect.objectContaining({ + twitchChat: expect.anything(), + streamlabs: expect.anything(), + }), + ], + }), + ); }); it('handles merge request opened event', async () => { @@ -46,7 +55,16 @@ describe('POST /gitlab', () => { headers: { 'x-gitlab-event': 'Merge Request Hook' }, }); - expect(result).toEqual(expect.objectContaining({ messages: expect.anything() })); + expect(result).toEqual( + expect.objectContaining({ + messages: [ + expect.objectContaining({ + twitchChat: expect.anything(), + streamlabs: expect.anything(), + }), + ], + }), + ); }); }); });