MCPcopy
hub / github.com/superplanehq/superplane / HandleWebhook

Method HandleWebhook

pkg/public/server.go:1134–1217  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

1132}
1133
1134func (s *Server) HandleWebhook(w http.ResponseWriter, r *http.Request) {
1135 vars := mux.Vars(r)
1136 webhookIDFromRequest := vars["webhookID"]
1137 webhookID, err := uuid.Parse(webhookIDFromRequest)
1138 if err != nil {
1139 http.Error(w, "webhook not found", http.StatusNotFound)
1140 return
1141 }
1142
1143 _, err = models.FindWebhook(webhookID)
1144 if err != nil {
1145 http.Error(w, "webhook not found", http.StatusNotFound)
1146 return
1147 }
1148
1149 r.Body = http.MaxBytesReader(w, r.Body, MaxEventSize)
1150 defer r.Body.Close()
1151
1152 body, err := io.ReadAll(r.Body)
1153 if err != nil {
1154 if _, ok := err.(*http.MaxBytesError); ok {
1155 http.Error(
1156 w,
1157 fmt.Sprintf("Request body is too large - must be up to %d bytes", MaxEventSize),
1158 http.StatusRequestEntityTooLarge,
1159 )
1160
1161 return
1162 }
1163
1164 http.Error(w, "Error reading request body", http.StatusBadRequest)
1165 return
1166 }
1167
1168 nodes, err := models.FindActiveWebhookNodes(webhookID)
1169 if err != nil || len(nodes) == 0 {
1170 http.Error(w, "webhook not found", http.StatusNotFound)
1171 return
1172 }
1173
1174 newEvents := []models.CanvasEvent{}
1175 onNewEvents := func(events []models.CanvasEvent) {
1176 newEvents = append(newEvents, events...)
1177 }
1178
1179 touchedExecutions := map[uuid.UUID]uuid.UUID{}
1180 recordExecution := func(workflowID, executionID uuid.UUID) {
1181 touchedExecutions[executionID] = workflowID
1182 }
1183
1184 var firstResponse *core.WebhookResponseBody
1185
1186 for _, node := range nodes {
1187 code, response, err := s.executeWebhookNode(r.Context(), body, r.Header, node, onNewEvents, recordExecution)
1188 if err != nil {
1189 http.Error(w, fmt.Sprintf("error handling webhook: %v", err), code)
1190 return
1191 }

Callers

nothing calls this directly

Calls 12

executeWebhookNodeMethod · 0.95
FindWebhookFunction · 0.92
FindActiveWebhookNodesFunction · 0.92
ParseMethod · 0.80
ErrorfMethod · 0.80
ErrorMethod · 0.65
CloseMethod · 0.65
SetMethod · 0.65
WriteHeaderMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected