Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitae5345c

Browse files
committed
configure webhook with token
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent82eed3d commitae5345c

File tree

6 files changed

+69
-25
lines changed

6 files changed

+69
-25
lines changed

‎docs/docs/env-vars.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ CodeRoad has a number of configurations:
1818

1919
-`CODEROAD_CONTENT_SECURITY_POLICY_EXEMPTIONS` - a list of CSP exemption hashes. For multiples, separate the list with a space.
2020

21+
-`CODEROAD_WEBHOOK_TOKEN` - an optional token for authenticating/authorizing webhook endpoints. Passed to the webhook endpoint in a`CodeRoad-User-Token` header.
22+
2123
##How to Use Variables
2224

2325
###Local

‎src/actions/onTutorialConfigContinue.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Context from '../services/context/context'
55
importtutorialConfigfrom'./utils/tutorialConfig'
66
import{COMMANDS,send}from'../commands'
77
importloggerfrom'../services/logger'
8+
import{setupWebhook}from'../services/hooks/webhooks'
89

910
constonTutorialConfigContinue=async(action:T.Action,context:Context):Promise<void>=>{
1011
logger('onTutorialConfigContinue',action)
@@ -19,6 +20,11 @@ const onTutorialConfigContinue = async (action: T.Action, context: Context): Pro
1920
data:tutorialToContinue,
2021
alreadyConfigured:true,
2122
})
23+
24+
// configure webhook
25+
if(tutorialToContinue.config?.webhook){
26+
setupWebhook(tutorialToContinue.config.webhook)
27+
}
2228
}catch(e){
2329
consterror={
2430
type:'UnknownError',

‎src/actions/onTutorialConfigNew.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { version, compareVersions } from '../services/dependencies'
88
importContextfrom'../services/context/context'
99
importtutorialConfigfrom'./utils/tutorialConfig'
1010
import{send}from'../commands'
11+
import{setupWebhook}from'../services/hooks/webhooks'
1112

1213
constonTutorialConfigNew=async(action:T.Action,context:Context):Promise<void>=>{
1314
try{
@@ -108,6 +109,11 @@ const onTutorialConfigNew = async (action: T.Action, context: Context): Promise<
108109
return
109110
}
110111

112+
// configure webhook
113+
if(data.config?.webhook){
114+
setupWebhook(data.config.webhook)
115+
}
116+
111117
// report back to the webview that setup is complete
112118
send({type:'TUTORIAL_CONFIGURED'})
113119
}catch(e){

‎src/environment.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ export const DISABLE_RUN_ON_SAVE = (process.env.CODEROAD_DISABLE_RUN_ON_SAVE ||
4343
// for multiple exemptions, separate each with a space "a1 b1"
4444
exportconstCONTENT_SECURITY_POLICY_EXEMPTIONS:string|null=
4545
process.env.CODEROAD_CONTENT_SECURITY_POLICY_EXEMPTIONS||null
46+
47+
// optional token for authorization/authentication of webhook calls
48+
exportconstWEBHOOK_TOKEN=process.env.CODEROAD_WEBHOOK_TOKEN||null

‎src/services/hooks/webhooks.ts

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
1+
import*asTTfrom'typings/tutorial'
12
importfetchfrom'node-fetch'
23
importloggerfrom'../logger'
4+
import{WEBHOOK_TOKEN}from'../../environment'
35

4-
constWEBHOOKS={
5-
init:true,
6-
reset:true,
7-
step_complete:true,
8-
level_complete:true,
9-
tutorial_complete:true,
6+
constWEBHOOK_EVENTS={
7+
init:false,
8+
reset:false,
9+
step_complete:false,
10+
level_complete:false,
11+
tutorial_complete:false,
12+
}
13+
14+
// varaibles set on init
15+
letWEBHOOK_URI:string|undefined
16+
17+
exportconstsetupWebhook=(webhookConfig:TT.WebhookConfig)=>{
18+
if(!webhookConfig.url){
19+
return
20+
}
21+
// set webhook uri
22+
WEBHOOK_URI=webhookConfig.url
23+
24+
// set webhook event triggers
25+
constevents=webhookConfig.eventsasTT.WebhookConfigEvents
26+
for(consteventNameofObject.keys(events||{})){
27+
WEBHOOK_EVENTS[eventName]=events[eventName]
28+
}
1029
}
1130

1231
constcallWebhookEndpoint=async<B>(bodyObject:B):Promise<void>=>{
13-
constendpoint='http://localhost:3000'
32+
if(!WEBHOOK_URI){
33+
return
34+
}
35+
36+
constheaders={'Content-Type':'application/json'}
37+
// if the webhook token is specified as env var, sends a token with the request
38+
if(WEBHOOK_TOKEN){
39+
headers['CodeRoad-User-Token']=WEBHOOK_TOKEN
40+
}
41+
1442
constbody=JSON.stringify(bodyObject)
43+
1544
try{
16-
constsendEvent=awaitfetch(endpoint,{
45+
constsendEvent=awaitfetch(WEBHOOK_URI,{
1746
method:'POST',
18-
headers:{'Content-Type':'application/json'},
47+
headers,
1948
body,
2049
})
2150
if(!sendEvent.ok){
2251
thrownewError('Error sending event')
2352
}
2453
}catch(err:unknown){
25-
logger(`Failed to call webhook endpoint${endpoint} with body${body}`)
54+
logger(`Failed to call webhook endpoint${WEBHOOK_URI} with body${body}`)
2655
}
2756
}
2857

@@ -32,7 +61,7 @@ type WebhookEventInit = {
3261
}
3362

3463
exportconstonInit=(event:WebhookEventInit):void=>{
35-
if(WEBHOOKS.init){
64+
if(WEBHOOK_EVENTS.init){
3665
callWebhookEndpoint<WebhookEventInit>(event)
3766
}
3867
}
@@ -42,31 +71,31 @@ type WebhookEventReset = {
4271
}
4372

4473
exportconstonReset=(event:WebhookEventReset):void=>{
45-
if(WEBHOOKS.reset){
74+
if(WEBHOOK_EVENTS.reset){
4675
callWebhookEndpoint<WebhookEventReset>(event)
4776
}
4877
}
4978

5079
typeWebhookEventStepComplete={tutorialId:string;version:string;levelId:string;stepId:string}
5180

5281
exportconstonStepComplete=(event:WebhookEventStepComplete):void=>{
53-
if(WEBHOOKS.step_complete){
82+
if(WEBHOOK_EVENTS.step_complete){
5483
callWebhookEndpoint<WebhookEventStepComplete>(event)
5584
}
5685
}
5786

5887
typeWebhookEventLevelComplete={tutorialId:string;version:string;levelId:string}
5988

6089
exportconstonLevelComplete=(event:WebhookEventLevelComplete):void=>{
61-
if(WEBHOOKS.level_complete){
90+
if(WEBHOOK_EVENTS.level_complete){
6291
callWebhookEndpoint<WebhookEventLevelComplete>(event)
6392
}
6493
}
6594

6695
typeWebhookEevntTutorialComplete={tutorialId:string;version:string}
6796

6897
exportconstonTutorialComplete=(event:WebhookEevntTutorialComplete):void=>{
69-
if(WEBHOOKS.tutorial_complete){
98+
if(WEBHOOK_EVENTS.tutorial_complete){
7099
callWebhookEndpoint<WebhookEevntTutorialComplete>(event)
71100
}
72101
}

‎typings/tutorial.d.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,14 @@ export interface TutorialAppVersions {
9494

9595
exporttypeVSCodeCommand=string|[string,any]
9696

97+
exportinterfaceWebhookConfigEvents{
98+
init?:boolean
99+
reset?:boolean
100+
step_complete?:boolean
101+
level_complete?:boolean
102+
tutorial_complete?:boolean
103+
}
97104
exportinterfaceWebhookConfig{
98105
url:string
99-
config:{
100-
token:boolean
101-
}
102-
events:{
103-
init?:boolean
104-
reset?:boolean
105-
step_complete?:boolean
106-
level_complete?:boolean
107-
tutorial_complete?:boolean
108-
}
106+
events?:WebhookConfigEvents
109107
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp