Skip to content

Commit

Permalink
fix(webhooks): send ActionPayload as string (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen committed Jun 26, 2024
1 parent 1002e74 commit b88989d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 7 additions & 4 deletions pkg/worker/webhook_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,21 @@ func (w *Worker) WebhookHttpHandler(opts WebhookHandlerOptions, workflows ...wor
return
}

var action client.Action
var action ActionPayload
if err := json.Unmarshal(data, &action); err != nil {
w.l.Error().Err(err).Msg("error unmarshaling action")
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(err.Error()))
return
}

actionWithPayload := action.Action
actionWithPayload.ActionPayload = []byte(action.ActionPayload)

timestamp := time.Now().UTC()
_, err = w.client.Dispatcher().SendStepActionEvent(r.Context(),
&client.ActionEvent{
Action: &action,
Action: actionWithPayload,
EventTimestamp: &timestamp,
EventType: client.ActionEventTypeStarted,
},
Expand All @@ -111,7 +114,7 @@ func (w *Worker) WebhookHttpHandler(opts WebhookHandlerOptions, workflows ...wor
return
}

ctx, err := newHatchetContext(r.Context(), &action, w.client, w.l)
ctx, err := newHatchetContext(r.Context(), actionWithPayload, w.client, w.l)
if err != nil {
w.l.Error().Err(err).Msg("error creating context")
writer.WriteHeader(http.StatusInternalServerError)
Expand All @@ -130,7 +133,7 @@ func (w *Worker) WebhookHttpHandler(opts WebhookHandlerOptions, workflows ...wor
timestamp = time.Now().UTC()
_, err = w.client.Dispatcher().SendStepActionEvent(r.Context(),
&client.ActionEvent{
Action: &action,
Action: actionWithPayload,
EventTimestamp: &timestamp,
EventType: client.ActionEventTypeCompleted,
EventPayload: resp,
Expand Down
12 changes: 11 additions & 1 deletion pkg/worker/worker_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ type RegisterWebhookWorkerOpts struct {
Secret *string
}

type ActionPayload struct {
*client.Action

ActionPayload string `json:"actionPayload"`
}

func (w *Worker) RegisterWebhook(ww RegisterWebhookWorkerOpts) error {
tenantId := openapi_types.UUID{}
if err := tenantId.Scan(w.client.TenantId()); err != nil {
Expand Down Expand Up @@ -107,7 +113,11 @@ func (w *Worker) StartWebhook(ww WebhookWorkerOpts) (func() error, error) {
func (w *Worker) sendWebhook(ctx context.Context, action *client.Action, ww WebhookWorkerOpts) error {
w.l.Debug().Msgf("action received from step run %s, sending webhook at %s", action.StepRunId, time.Now())

_, err := whrequest.Send(ctx, ww.URL, ww.Secret, action)
actionWithPayload := ActionPayload{
Action: action,
ActionPayload: string(action.ActionPayload),
}
_, err := whrequest.Send(ctx, ww.URL, ww.Secret, actionWithPayload)
if err != nil {
w.l.Warn().Msgf("step run %s could not send webhook to %s: %s", action.StepRunId, ww.URL, err)
if err := w.markFailed(action, fmt.Errorf("could not send webhook: %w", err)); err != nil {
Expand Down

0 comments on commit b88989d

Please sign in to comment.