99 "os"
1010 "path"
1111 "strings"
12+ "time"
1213)
1314
1415// Webhook is an inbound github webhook
@@ -83,31 +84,40 @@ func parseWebhookRequest(req *http.Request) (*Webhook, string, error) {
8384 return webhook , webhookID , err
8485}
8586
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+
8696// WebhookHandler handles a HTTP POST request containing the webhook payload in its body
8797func (a * API ) WebhookHandler (w http.ResponseWriter , req * http.Request ) {
8898 // Check the method
8999 if ! strings .EqualFold (req .Method , "POST" ) {
90100 w .WriteHeader (http .StatusMethodNotAllowed )
91- fmt .Printf ("Error 405 - Method not allowed: invalid method: %s" , req .Method )
92101 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 )
93103 return
94104 }
95105
96106 // Parse and validate the request
97107 _ , webhookID , err := parseWebhookRequest (req )
98108 if err != nil {
99109 w .WriteHeader (http .StatusNotFound )
100- fmt .Println (err )
101110 fmt .Fprint (w , "Error 404 - Not found: " , err )
111+ fmt .Printf ("%s Error %s" , time .Now ().Format (time .RFC3339 ), err )
102112 return
103113 }
104114
105115 // Check if webhookID exists
106116 groupname , username , err := checkWebhookID (a .DB , a .HPCWebhookHost , a .HPCWebhookExternalPort , webhookID )
107117 if err != nil {
108118 w .WriteHeader (http .StatusNotFound )
109- fmt .Println (err )
110119 fmt .Fprint (w , "Error 404 - Not found: " , err )
120+ fmt .Printf ("%s Error %s" , time .Now ().Format (time .RFC3339 ), err )
111121 return
112122 }
113123
@@ -117,6 +127,7 @@ func (a *API) WebhookHandler(w http.ResponseWriter, req *http.Request) {
117127 if err != nil {
118128 w .WriteHeader (http .StatusNotFound )
119129 fmt .Fprint (w , "Error 404 - Not found: " , err )
130+ fmt .Printf ("%s Error %s" , time .Now ().Format (time .RFC3339 ), err )
120131 return
121132 }
122133
@@ -125,8 +136,8 @@ func (a *API) WebhookHandler(w http.ResponseWriter, req *http.Request) {
125136 err = os .MkdirAll (payloadDir , os .ModePerm )
126137 if err != nil {
127138 w .WriteHeader (http .StatusNotFound )
128- fmt .Println (err )
129139 fmt .Fprint (w , "Error 404 - Not found: " , err )
140+ fmt .Printf ("%s Error %s" , time .Now ().Format (time .RFC3339 ), err )
130141 return
131142 }
132143
@@ -135,6 +146,7 @@ func (a *API) WebhookHandler(w http.ResponseWriter, req *http.Request) {
135146 if err != nil {
136147 w .WriteHeader (http .StatusNotFound )
137148 fmt .Fprint (w , "Error 404 - Not found: " , err )
149+ fmt .Printf ("%s Error %s" , time .Now ().Format (time .RFC3339 ), err )
138150 return
139151 }
140152
@@ -160,17 +172,12 @@ func (a *API) WebhookHandler(w http.ResponseWriter, req *http.Request) {
160172 payload : payload ,
161173 }
162174
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 )
170177
171178 // Succes
172179 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 ))
175182 return
176183}
0 commit comments