This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
KA-559 Rework publisher to react to webhooks #26
Open
matus12
wants to merge
1
commit into
develop
Choose a base branch
from
KA-559_Rework-timer-to-webhooks
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,6 @@ obj | |
node_modules | ||
dist | ||
|
||
.idea/* | ||
|
||
local.settings.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum Operation { | ||
Upsert = 'upsert', | ||
ChangeWorkflowStep = 'change_workflow_step', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { HttpRequest } from '@azure/functions/Interfaces'; | ||
import { Operation } from '../enums/Operation'; | ||
|
||
export class Configuration { | ||
public static eventGridKey: string; | ||
public static eventGridHost: string; | ||
|
||
public static set(request: HttpRequest): void { | ||
const isWorkflowStepChange = request.body.message.operation === Operation.ChangeWorkflowStep; | ||
|
||
this.eventGridKey = (isWorkflowStepChange | ||
? process.env['EventGrid.WorkflowChanged.Key'] | ||
: process.env['EventGrid.DocsChanged.Key']) || ''; | ||
this.eventGridHost = (isWorkflowStepChange | ||
? process.env['EventGrid.WorkflowChanged.Endpoint'] | ||
: process.env['EventGrid.DocsChanged.Endpoint']) || ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,75 @@ | ||
import { eventComposer } from './eventComposer'; | ||
|
||
const webhookBody = { | ||
data: { | ||
xxx: 'xxx', | ||
yyy: 'yyy' | ||
}, | ||
message: { | ||
operation: 'someOperation', | ||
type: '' | ||
} | ||
const body = { | ||
data: { | ||
xxx: 'xxx', | ||
yyy: 'yyy', | ||
}, | ||
message: { | ||
operation: 'someOperation', | ||
type: '', | ||
}, | ||
}; | ||
const eventType = 'kentico-cloud'; | ||
|
||
describe('eventComposer', () => { | ||
test('composes event with data from webhook', async () => { | ||
const isTest = undefined; | ||
const event = eventComposer(webhookBody, isTest); | ||
test('composes event with data from webhook', async () => { | ||
const isTest = undefined; | ||
const request = { | ||
body, | ||
query: { | ||
source: eventType, | ||
test: isTest, | ||
}, | ||
} as any; | ||
const event = eventComposer(request); | ||
|
||
expect(event.id).toBeTruthy(); | ||
expect(event.subject).toBe(webhookBody.message.operation); | ||
expect(event.eventType).toBe(eventType); | ||
expect(event.dataVersion).toBe('1.0'); | ||
expect(event.data.webhook).toBe(webhookBody.data); | ||
expect(event.data.test).toBe('disabled'); | ||
expect(event.eventTime).toBeTruthy(); | ||
}); | ||
expect(event.id).toBeTruthy(); | ||
expect(event.subject).toBe(body.message.operation); | ||
expect(event.eventType).toBe(eventType); | ||
expect(event.dataVersion).toBe('1.0'); | ||
expect(event.data.webhook).toBe(body.data); | ||
expect(event.data.test).toBe('disabled'); | ||
expect(event.eventTime).toBeTruthy(); | ||
}); | ||
|
||
test('composes event with testing configuration', async () => { | ||
const isTest = 'enabled'; | ||
const event = eventComposer(webhookBody, isTest); | ||
test('composes event with testing configuration', async () => { | ||
const isTest = 'enabled'; | ||
const request = { | ||
body, | ||
query: { | ||
source: eventType, | ||
test: isTest, | ||
}, | ||
} as any; | ||
const event = eventComposer(request); | ||
|
||
expect(event.id).toBeTruthy(); | ||
expect(event.subject).toBe(webhookBody.message.operation); | ||
expect(event.eventType).toBe(eventType); | ||
expect(event.dataVersion).toBe('1.0'); | ||
expect(event.data.webhook).toBe(webhookBody.data); | ||
expect(event.data.test).toBe('enabled'); | ||
expect(event.eventTime).toBeTruthy(); | ||
}); | ||
expect(event.id).toBeTruthy(); | ||
expect(event.subject).toBe(body.message.operation); | ||
expect(event.eventType).toBe(eventType); | ||
expect(event.dataVersion).toBe('1.0'); | ||
expect(event.data.webhook).toBe(body.data); | ||
expect(event.data.test).toBe('enabled'); | ||
expect(event.eventTime).toBeTruthy(); | ||
}); | ||
|
||
test('composes event with incorrect testing configuration', async () => { | ||
const isTest = 'something'; | ||
const event = eventComposer(webhookBody, isTest); | ||
test('composes event with incorrect testing configuration', async () => { | ||
const isTest = 'something'; | ||
const request = { | ||
body, | ||
query: { | ||
source: eventType, | ||
test: isTest, | ||
}, | ||
} as any; | ||
const event = eventComposer(request); | ||
|
||
expect(event.id).toBeTruthy(); | ||
expect(event.subject).toBe(webhookBody.message.operation); | ||
expect(event.eventType).toBe(eventType); | ||
expect(event.dataVersion).toBe('1.0'); | ||
expect(event.data.webhook).toBe(webhookBody.data); | ||
expect(event.data.test).toBe('disabled'); | ||
expect(event.eventTime).toBeTruthy(); | ||
}); | ||
expect(event.id).toBeTruthy(); | ||
expect(event.subject).toBe(body.message.operation); | ||
expect(event.eventType).toBe(eventType); | ||
expect(event.data.webhook).toBe(body.data); | ||
expect(event.dataVersion).toBe('1.0'); | ||
expect(event.data.test).toBe('disabled'); | ||
expect(event.eventTime).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,15 @@ | ||
import { HttpRequest } from '@azure/functions/Interfaces'; | ||
import { EventGridModels } from 'azure-eventgrid'; | ||
import getUuid from 'uuid'; | ||
import { RequestBody } from '../@types/global'; | ||
|
||
export const eventComposer = ( | ||
webhookBody: RequestBody, | ||
test?: string | ||
): EventGridModels.EventGridEvent => { | ||
const isTest = test === 'enabled' ? 'enabled' : 'disabled'; | ||
|
||
return { | ||
export const eventComposer = ({body, query: {test}}: HttpRequest): EventGridModels.EventGridEvent => ({ | ||
data: { | ||
test: isTest, | ||
webhook: webhookBody.data | ||
test: test === 'enabled' ? test : 'disabled', | ||
webhook: body.data, | ||
}, | ||
dataVersion: '1.0', | ||
eventTime: new Date(), | ||
eventType: 'kentico-cloud', | ||
id: getUuid(), | ||
subject: webhookBody.message.operation | ||
}; | ||
}; | ||
subject: body.message.operation, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import EventGridClient from 'azure-eventgrid'; | ||
import { TopicCredentials } from 'ms-rest-azure'; | ||
import { | ||
HttpRequest, | ||
RequestBody, | ||
} from '../@types/global'; | ||
import { Operation } from '../enums/Operation'; | ||
import { Configuration } from './Configuration'; | ||
import { eventComposer } from './eventComposer'; | ||
import { publishEventsCreator } from './publishEventsCreator'; | ||
|
||
export const isRequestBodyValid = (body: any): body is RequestBody => | ||
body | ||
&& body.message | ||
&& body.message.type | ||
&& body.message.operation | ||
&& body.data; | ||
|
||
export const shouldIgnoreRequest = ({ message: { type, operation } }: RequestBody): boolean => | ||
type === 'content_item' && operation !== Operation.Upsert; | ||
|
||
export const publishEventWithWebhookData = async (request: HttpRequest) => { | ||
const topicCredentials = new TopicCredentials(Configuration.eventGridKey); | ||
const eventGridClient = new EventGridClient(topicCredentials); | ||
const publishEvents = publishEventsCreator({eventGridClient, host: Configuration.eventGridHost}); | ||
|
||
const event = eventComposer(request); | ||
await publishEvents([event]); | ||
|
||
return event; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
import azureFunction from './index'; | ||
|
||
describe('Azure function fails', () => { | ||
let context = {}; | ||
let context = {}; | ||
|
||
beforeEach(() => { | ||
context = { | ||
done: jest.fn(), | ||
res: null | ||
}; | ||
}); | ||
beforeEach(() => { | ||
context = { | ||
done: jest.fn(), | ||
res: null, | ||
}; | ||
}); | ||
|
||
test('returns 200 but does nothing on kentico-cloud and content_item', async () => { | ||
const request = { | ||
body: { | ||
data: 'anything', | ||
message: { | ||
operation: 'anything', | ||
type: 'content_item' | ||
} | ||
}, | ||
query: { | ||
source: 'kentico-cloud' | ||
} | ||
}; | ||
test('returns 200 but does nothing on kentico-cloud and content_item', async () => { | ||
const request = { | ||
body: { | ||
data: 'anything', | ||
message: { | ||
operation: 'anything', | ||
type: 'content_item', | ||
}, | ||
}, | ||
query: { | ||
source: 'kentico-cloud', | ||
}, | ||
}; | ||
|
||
const response = await azureFunction(context as any, request, null); | ||
const response = await azureFunction(context as any, request, null); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body).toBe('Nothing published'); | ||
}); | ||
expect(response.status).toBe(200); | ||
expect(response.body).toBe('Nothing published'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,43 @@ | ||
import { AzureFunction, Context } from '@azure/functions'; | ||
import EventGridClient from 'azure-eventgrid'; | ||
import { TopicCredentials } from 'ms-rest-azure'; | ||
import { HttpRequest, HttpResponse, RequestBody } from '../@types/global'; | ||
import { eventComposer } from './eventComposer'; | ||
import { publishEventsCreator } from './publishEventsCreator'; | ||
|
||
const isRequestBodyValid = (body: any): body is RequestBody => | ||
body | ||
&& body.message | ||
&& body.message.type | ||
&& body.message.operation | ||
&& body.data; | ||
|
||
const shouldIgnoreRequest = ({ message: { type, operation } }: RequestBody): boolean => | ||
type === 'content_item' && operation !== 'upsert'; | ||
|
||
const parseWebhook: AzureFunction = async ( | ||
_context: Context, | ||
request: HttpRequest | ||
): Promise<HttpResponse> => { | ||
try { | ||
if (!isRequestBodyValid(request.body)) { | ||
throw new Error('Received invalid message body'); | ||
} | ||
|
||
if (shouldIgnoreRequest(request.body)) { | ||
return { | ||
body: 'Nothing published', | ||
status: 200 | ||
}; | ||
import { | ||
AzureFunction, | ||
Context, | ||
} from '@azure/functions'; | ||
import { | ||
HttpRequest, | ||
HttpResponse, | ||
} from '../@types/global'; | ||
import { Configuration } from './Configuration'; | ||
import { | ||
isRequestBodyValid, | ||
publishEventWithWebhookData, | ||
shouldIgnoreRequest, | ||
} from './helpers'; | ||
|
||
const parseWebhook: AzureFunction = async (context: Context, request: HttpRequest): Promise<HttpResponse> => { | ||
try { | ||
if (!isRequestBodyValid(request.body)) { | ||
throw new Error('Received invalid message body'); | ||
} | ||
|
||
if (shouldIgnoreRequest(request.body)) { | ||
return { | ||
body: 'Nothing published', | ||
status: 200, | ||
}; | ||
} | ||
|
||
Configuration.set(request); | ||
|
||
const event = await publishEventWithWebhookData(request); | ||
|
||
return { | ||
body: event, | ||
status: 200, | ||
}; | ||
} catch (error) { | ||
/** This try-catch is required for correct logging of exceptions in Azure */ | ||
throw `Message: ${error.message} \nStack Trace: ${error.stack}`; | ||
} | ||
|
||
const eventGridKey = process.env['EventGrid.DocsChanged.Key']; | ||
const host = process.env['EventGrid.DocsChanged.Endpoint']; | ||
if (!eventGridKey || !host) { | ||
throw new Error('Undefined env property provided'); | ||
} | ||
|
||
const topicCredentials = new TopicCredentials(eventGridKey); | ||
const eventGridClient = new EventGridClient(topicCredentials); | ||
const publishEvents = publishEventsCreator({ eventGridClient, host }); | ||
|
||
const event = eventComposer(request.body, request.query.test); | ||
await publishEvents([event]); | ||
|
||
return { | ||
body: event, | ||
status: 200 | ||
}; | ||
} catch (error) { | ||
/** This try-catch is required for correct logging of exceptions in Azure */ | ||
throw `Message: ${error.message} \nStack Trace: ${error.stack}`; | ||
} | ||
}; | ||
|
||
export default parseWebhook; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EventType should be different for a different data structure.