-
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
WIP: webhook demo #508
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,101 @@ | ||||||||||||||||||||||||||||||
import * as TT from 'typings/tutorial' | ||||||||||||||||||||||||||||||
import fetch from 'node-fetch' | ||||||||||||||||||||||||||||||
import logger from '../logger' | ||||||||||||||||||||||||||||||
import { WEBHOOK_TOKEN } from '../../environment' | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
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> => { | ||||||||||||||||||||||||||||||
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(WEBHOOK_URI, { | ||||||||||||||||||||||||||||||
method: 'POST', | ||||||||||||||||||||||||||||||
headers, | ||||||||||||||||||||||||||||||
body, | ||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||
if (!sendEvent.ok) { | ||||||||||||||||||||||||||||||
throw new Error('Error sending event') | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} catch (err: unknown) { | ||||||||||||||||||||||||||||||
logger(`Failed to call webhook endpoint ${WEBHOOK_URI} with body ${body}`) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
type WebhookEventInit = { | ||||||||||||||||||||||||||||||
tutorialId: string | ||||||||||||||||||||||||||||||
coderoadVersion: string | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
export const onInit = (event: WebhookEventInit): void => { | ||||||||||||||||||||||||||||||
if (WEBHOOK_EVENTS.init) { | ||||||||||||||||||||||||||||||
callWebhookEndpoint<WebhookEventInit>(event) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
type WebhookEventReset = { | ||||||||||||||||||||||||||||||
tutorialId: string | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
export const onReset = (event: WebhookEventReset): void => { | ||||||||||||||||||||||||||||||
if (WEBHOOK_EVENTS.reset) { | ||||||||||||||||||||||||||||||
callWebhookEndpoint<WebhookEventReset>(event) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
type WebhookEventStepComplete = { tutorialId: string; version: string; levelId: string; stepId: string } | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
export const onStepComplete = (event: WebhookEventStepComplete): void => { | ||||||||||||||||||||||||||||||
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 (WEBHOOK_EVENTS.level_complete) { | ||||||||||||||||||||||||||||||
callWebhookEndpoint<WebhookEventLevelComplete>(event) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
type WebhookEevntTutorialComplete = { tutorialId: string; version: string } | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
export const onTutorialComplete = (event: WebhookEevntTutorialComplete): void => { | ||||||||||||||||||||||||||||||
if (WEBHOOK_EVENTS.tutorial_complete) { | ||||||||||||||||||||||||||||||
callWebhookEndpoint<WebhookEevntTutorialComplete>(event) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
Comment on lines
+95
to
+101
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
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. Sounds good, I'll merge for now with the note that it may require more of a guarantee. 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. Pretty sure I was just having issues running the extension without the 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. The published version has the @moT01 can you post an issue if you have any more details? |
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.