9
9
"os"
10
10
"path"
11
11
"strings"
12
+ "time"
12
13
)
13
14
14
15
// Webhook is an inbound github webhook
@@ -83,31 +84,40 @@ func parseWebhookRequest(req *http.Request) (*Webhook, string, error) {
83
84
return webhook , webhookID , err
84
85
}
85
86
87
+ // Process the webhook and log events
88
+ func processWebhook (c Connector , conf executeConfiguration ) {
89
+ err := ExecuteScript (c , conf )
90
+ if err != nil {
91
+ fmt .Printf ("%s Error %s" , time .Now ().Format (time .RFC3339 ), err )
92
+ }
93
+ fmt .Printf ("%s Success" , time .Now ().Format (time .RFC3339 ))
94
+ }
95
+
86
96
// WebhookHandler handles a HTTP POST request containing the webhook payload in its body
87
97
func (a * API ) WebhookHandler (w http.ResponseWriter , req * http.Request ) {
88
98
// Check the method
89
99
if ! strings .EqualFold (req .Method , "POST" ) {
90
100
w .WriteHeader (http .StatusMethodNotAllowed )
91
- fmt .Printf ("Error 405 - Method not allowed: invalid method: %s" , req .Method )
92
101
fmt .Fprint (w , "Error 405 - Method not allowed: invalid method: " , req .Method )
102
+ fmt .Printf ("%s Error 405 - Method not allowed: invalid method: %s" , time .Now ().Format (time .RFC3339 ), req .Method )
93
103
return
94
104
}
95
105
96
106
// Parse and validate the request
97
107
_ , webhookID , err := parseWebhookRequest (req )
98
108
if err != nil {
99
109
w .WriteHeader (http .StatusNotFound )
100
- fmt .Println (err )
101
110
fmt .Fprint (w , "Error 404 - Not found: " , err )
111
+ fmt .Printf ("%s Error %s" , time .Now ().Format (time .RFC3339 ), err )
102
112
return
103
113
}
104
114
105
115
// Check if webhookID exists
106
116
groupname , username , err := checkWebhookID (a .DB , a .HPCWebhookHost , a .HPCWebhookExternalPort , webhookID )
107
117
if err != nil {
108
118
w .WriteHeader (http .StatusNotFound )
109
- fmt .Println (err )
110
119
fmt .Fprint (w , "Error 404 - Not found: " , err )
120
+ fmt .Printf ("%s Error %s" , time .Now ().Format (time .RFC3339 ), err )
111
121
return
112
122
}
113
123
@@ -117,6 +127,7 @@ func (a *API) WebhookHandler(w http.ResponseWriter, req *http.Request) {
117
127
if err != nil {
118
128
w .WriteHeader (http .StatusNotFound )
119
129
fmt .Fprint (w , "Error 404 - Not found: " , err )
130
+ fmt .Printf ("%s Error %s" , time .Now ().Format (time .RFC3339 ), err )
120
131
return
121
132
}
122
133
@@ -125,8 +136,8 @@ func (a *API) WebhookHandler(w http.ResponseWriter, req *http.Request) {
125
136
err = os .MkdirAll (payloadDir , os .ModePerm )
126
137
if err != nil {
127
138
w .WriteHeader (http .StatusNotFound )
128
- fmt .Println (err )
129
139
fmt .Fprint (w , "Error 404 - Not found: " , err )
140
+ fmt .Printf ("%s Error %s" , time .Now ().Format (time .RFC3339 ), err )
130
141
return
131
142
}
132
143
@@ -135,6 +146,7 @@ func (a *API) WebhookHandler(w http.ResponseWriter, req *http.Request) {
135
146
if err != nil {
136
147
w .WriteHeader (http .StatusNotFound )
137
148
fmt .Fprint (w , "Error 404 - Not found: " , err )
149
+ fmt .Printf ("%s Error %s" , time .Now ().Format (time .RFC3339 ), err )
138
150
return
139
151
}
140
152
@@ -160,17 +172,12 @@ func (a *API) WebhookHandler(w http.ResponseWriter, req *http.Request) {
160
172
payload : payload ,
161
173
}
162
174
163
- // Execute the script
164
- err = ExecuteScript (a .Connector , executeConfig )
165
- if err != nil {
166
- w .WriteHeader (http .StatusNotFound )
167
- fmt .Fprint (w , "Error 404 - Not found: " , err )
168
- return
169
- }
175
+ // Process the webhook in the background
176
+ go processWebhook (a .Connector , executeConfig )
170
177
171
178
// Succes
172
179
w .WriteHeader (http .StatusOK )
173
- fmt .Fprint (w , "Webhook handled successfully" )
174
-
180
+ fmt .Fprint (w , "Payload delivered successfully" )
181
+ fmt . Printf ( "%s Payload delivered successfully" , time . Now (). Format ( time . RFC3339 ))
175
182
return
176
183
}
0 commit comments