1
1
import express , { NextFunction , Request , Response } from "express" ;
2
2
import { Webhook , WebhookUnbrandedRequiredHeaders , WebhookVerificationError } from "standardwebhooks"
3
+ import { RenderDeploy , RenderEvent , RenderKeyValue , RenderPostgres , RenderService , WebhookPayload } from "./render" ;
3
4
4
5
const app = express ( ) ;
5
6
const port = process . env . PORT || 3001 ;
@@ -10,17 +11,6 @@ const renderAPIURL = process.env.RENDER_API_URL || "https://api.render.com/v1"
10
11
// To create a Render API token, follow instructions here: https://render.com/docs/api#1-create-an-api-key
11
12
const renderAPIToken = process . env . RENDER_API_TOKEN || '' ;
12
13
13
- interface WebhookData {
14
- id : string
15
- serviceId : string
16
- }
17
-
18
- interface WebhookPayload {
19
- type : string
20
- timestamp : Date
21
- data : WebhookData
22
- }
23
-
24
14
app . post ( "/webhook" , express . raw ( { type : 'application/json' } ) , ( req : Request , res : Response , next : NextFunction ) => {
25
15
try {
26
16
validateWebhook ( req ) ;
@@ -68,7 +58,7 @@ async function handleWebhook(payload: WebhookPayload) {
68
58
69
59
if ( deploy . commit ) {
70
60
console . log ( `deploy started for service ${ service . name } with commit "${ deploy . commit . message } "` )
71
- } else {
61
+ } else if ( deploy . image ) {
72
62
console . log ( `deploy started for service ${ service . name } with image sha "${ deploy . image . sha } "` )
73
63
}
74
64
return
@@ -91,7 +81,7 @@ async function handleWebhook(payload: WebhookPayload) {
91
81
// fetchEventInfo fetches the event that triggered the webhook
92
82
// some events have additional information that isn't in the webhook payload
93
83
// for example, deploy events have the deploy id
94
- async function fetchEventInfo ( payload : WebhookPayload ) {
84
+ async function fetchEventInfo ( payload : WebhookPayload ) : Promise < RenderEvent > {
95
85
const res = await fetch (
96
86
`${ renderAPIURL } /events/${ payload . data . id } ` ,
97
87
{
@@ -110,7 +100,7 @@ async function fetchEventInfo(payload: WebhookPayload) {
110
100
}
111
101
}
112
102
113
- async function fetchDeployInfo ( serviceId : string , deployId : string ) {
103
+ async function fetchDeployInfo ( serviceId : string , deployId : string ) : Promise < RenderDeploy > {
114
104
const res = await fetch (
115
105
`${ renderAPIURL } /services/${ serviceId } /deploys/${ deployId } ` ,
116
106
{
@@ -129,7 +119,7 @@ async function fetchDeployInfo(serviceId: string, deployId: string) {
129
119
}
130
120
}
131
121
132
- async function fetchServiceInfo ( payload : WebhookPayload ) {
122
+ async function fetchServiceInfo ( payload : WebhookPayload ) : Promise < RenderService > {
133
123
const res = await fetch (
134
124
`${ renderAPIURL } /services/${ payload . data . serviceId } ` ,
135
125
{
@@ -149,7 +139,7 @@ async function fetchServiceInfo(payload: WebhookPayload) {
149
139
}
150
140
151
141
152
- async function fetchPostgresInfo ( payload : WebhookPayload ) {
142
+ async function fetchPostgresInfo ( payload : WebhookPayload ) : Promise < RenderPostgres > {
153
143
const res = await fetch (
154
144
`${ renderAPIURL } /postgres/${ payload . data . serviceId } ` ,
155
145
{
@@ -169,7 +159,7 @@ async function fetchPostgresInfo(payload: WebhookPayload) {
169
159
}
170
160
171
161
172
- async function fetchKeyValueInfo ( payload : WebhookPayload ) {
162
+ async function fetchKeyValueInfo ( payload : WebhookPayload ) : Promise < RenderKeyValue > {
173
163
const res = await fetch (
174
164
`${ renderAPIURL } /key-value/${ payload . data . serviceId } ` ,
175
165
{
0 commit comments