-
Notifications
You must be signed in to change notification settings - Fork 38
WIP: webhook demo #508
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
Merged
WIP: webhook demo #508
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Prev
Previous commit
configure webhook with token
Signed-off-by: shmck <[email protected]>
- Loading branch information
commit ae5345ce9db4578ec029e339addce35d82a7279b
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,28 +1,57 @@ | ||||||
import * as TT from 'typings/tutorial' | ||||||
import fetch from 'node-fetch' | ||||||
import logger from '../logger' | ||||||
import { WEBHOOK_TOKEN } from '../../environment' | ||||||
|
||||||
const WEBHOOKS = { | ||||||
init: true, | ||||||
reset: true, | ||||||
step_complete: true, | ||||||
level_complete: true, | ||||||
tutorial_complete: true, | ||||||
const WEBHOOK_EVENTS = { | ||||||
init: false, | ||||||
reset: false, | ||||||
step_complete: false, | ||||||
level_complete: false, | ||||||
tutorial_complete: false, | ||||||
} | ||||||
|
||||||
// varaibles set on init | ||||||
let WEBHOOK_URI: string | undefined | ||||||
|
||||||
export const setupWebhook = (webhookConfig: TT.WebhookConfig) => { | ||||||
if (!webhookConfig.url) { | ||||||
return | ||||||
} | ||||||
// set webhook uri | ||||||
WEBHOOK_URI = webhookConfig.url | ||||||
|
||||||
// set webhook event triggers | ||||||
const events = webhookConfig.events as TT.WebhookConfigEvents | ||||||
for (const eventName of Object.keys(events || {})) { | ||||||
WEBHOOK_EVENTS[eventName] = events[eventName] | ||||||
} | ||||||
} | ||||||
|
||||||
const callWebhookEndpoint = async <B>(bodyObject: B): Promise<void> => { | ||||||
const endpoint = 'http://localhost:3000' | ||||||
if (!WEBHOOK_URI) { | ||||||
return | ||||||
} | ||||||
|
||||||
const headers = { 'Content-Type': 'application/json' } | ||||||
// if the webhook token is specified as env var, sends a token with the request | ||||||
if (WEBHOOK_TOKEN) { | ||||||
headers['CodeRoad-User-Token'] = WEBHOOK_TOKEN | ||||||
} | ||||||
|
||||||
const body = JSON.stringify(bodyObject) | ||||||
|
||||||
try { | ||||||
const sendEvent = await fetch(endpoint, { | ||||||
const sendEvent = await fetch(WEBHOOK_URI, { | ||||||
method: 'POST', | ||||||
headers: { 'Content-Type': 'application/json' }, | ||||||
headers, | ||||||
body, | ||||||
}) | ||||||
if (!sendEvent.ok) { | ||||||
throw new Error('Error sending event') | ||||||
} | ||||||
} catch (err: unknown) { | ||||||
logger(`Failed to call webhook endpoint ${endpoint} with body ${body}`) | ||||||
logger(`Failed to call webhook endpoint ${WEBHOOK_URI} with body ${body}`) | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -32,7 +61,7 @@ type WebhookEventInit = { | |||||
} | ||||||
|
||||||
export const onInit = (event: WebhookEventInit): void => { | ||||||
if (WEBHOOKS.init) { | ||||||
if (WEBHOOK_EVENTS.init) { | ||||||
callWebhookEndpoint<WebhookEventInit>(event) | ||||||
} | ||||||
} | ||||||
|
@@ -42,31 +71,31 @@ type WebhookEventReset = { | |||||
} | ||||||
|
||||||
export const onReset = (event: WebhookEventReset): void => { | ||||||
if (WEBHOOKS.reset) { | ||||||
if (WEBHOOK_EVENTS.reset) { | ||||||
callWebhookEndpoint<WebhookEventReset>(event) | ||||||
} | ||||||
} | ||||||
|
||||||
type WebhookEventStepComplete = { tutorialId: string; version: string; levelId: string; stepId: string } | ||||||
|
||||||
export const onStepComplete = (event: WebhookEventStepComplete): void => { | ||||||
if (WEBHOOKS.step_complete) { | ||||||
if (WEBHOOK_EVENTS.step_complete) { | ||||||
callWebhookEndpoint<WebhookEventStepComplete>(event) | ||||||
} | ||||||
} | ||||||
|
||||||
type WebhookEventLevelComplete = { tutorialId: string; version: string; levelId: string } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
export const onLevelComplete = (event: WebhookEventLevelComplete): void => { | ||||||
if (WEBHOOKS.level_complete) { | ||||||
if (WEBHOOK_EVENTS.level_complete) { | ||||||
callWebhookEndpoint<WebhookEventLevelComplete>(event) | ||||||
} | ||||||
} | ||||||
|
||||||
type WebhookEevntTutorialComplete = { tutorialId: string; version: string } | ||||||
|
||||||
export const onTutorialComplete = (event: WebhookEevntTutorialComplete): void => { | ||||||
if (WEBHOOKS.tutorial_complete) { | ||||||
if (WEBHOOK_EVENTS.tutorial_complete) { | ||||||
callWebhookEndpoint<WebhookEevntTutorialComplete>(event) | ||||||
} | ||||||
} |
This file contains hidden or 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
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.