diff --git a/src/routes/gitlab/index.ts b/src/routes/gitlab/index.ts new file mode 100644 index 0000000..7775891 --- /dev/null +++ b/src/routes/gitlab/index.ts @@ -0,0 +1,22 @@ +import { ServerRoute, ResponseObject, Request, ResponseToolkit } from '@hapi/hapi'; +import { gitlabHeader } from '../../schemas/gitlab/joi/gitlab-headers-schema'; + +export const routes = (): ServerRoute[] => { + return [ + { + method: 'POST', + options: { + validate: { + headers: gitlabHeader(), + }, + }, + handler: async (request: Request, h: ResponseToolkit): Promise => { + const { headers } = request; + const event = headers['x-gitlab-event']; + + return h.response({ message: `Ignoring event: '${event}'` }).code(200); + }, + path: '/gitlab', + }, + ]; +}; diff --git a/src/routes/index.ts b/src/routes/index.ts new file mode 100644 index 0000000..164e799 --- /dev/null +++ b/src/routes/index.ts @@ -0,0 +1,8 @@ +import { ServerRoute } from '@hapi/hapi'; +import { routes as gitlab } from './gitlab'; +import { routes as github } from './github'; +import { Config } from '../config'; + +export const routes = (config: Config): ServerRoute[] => { + return [...github(config), ...gitlab()]; +}; diff --git a/src/schemas/gitlab/joi/gitlab-headers-schema.ts b/src/schemas/gitlab/joi/gitlab-headers-schema.ts new file mode 100644 index 0000000..09c73e8 --- /dev/null +++ b/src/schemas/gitlab/joi/gitlab-headers-schema.ts @@ -0,0 +1,7 @@ +import { object, string, Schema } from '@hapi/joi'; + +export function gitlabHeader(): Schema { + return object({ + 'x-gitlab-event': string().required(), + }).unknown(); +} diff --git a/src/server.ts b/src/server.ts index fa0fe8f..ef3c4ef 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,5 +1,5 @@ import { Server } from '@hapi/hapi'; -import { routes } from './routes/github'; +import { routes } from './routes'; import laabr from 'laabr'; import { Config } from './config'; diff --git a/test/routes/gitlab/index.spec.ts b/test/routes/gitlab/index.spec.ts new file mode 100644 index 0000000..200304a --- /dev/null +++ b/test/routes/gitlab/index.spec.ts @@ -0,0 +1,39 @@ +import { initServer } from '../../../src/server'; +import { getConfig } from '../../../src/config'; + +describe('POST /gitlab', () => { + it('returns a 200 status ok', async () => { + const subject = await initServer(getConfig()); + + const { statusCode } = await subject.inject({ + method: 'POST', + url: '/gitlab', + headers: { 'X-Gitlab-Event': 'test' }, + }); + + expect(statusCode).toEqual(200); + }); + + it("returns 400 bad request is the request is missing the 'X-Gitlab-Event' header", async () => { + const subject = await initServer(getConfig()); + + const { statusCode } = await subject.inject({ + method: 'POST', + url: '/gitlab', + }); + + expect(statusCode).toEqual(400); + }); + + it('ignores unknown events gracefully', async () => { + const subject = await initServer(getConfig()); + + const { result } = await subject.inject({ + method: 'POST', + url: '/gitlab', + headers: { 'X-Gitlab-Event': 'test' }, + }); + + expect(result).toEqual({ message: `Ignoring event: 'test'` }); + }); +});