Skip to content

Commit 230d9d2

Browse files
committed
Still allow accessing webhook.json after a wait
1 parent 2e97add commit 230d9d2

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

flows/runs/legacy.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package runs
22

33
import (
4+
"net/http"
45
"regexp"
56
"sort"
67
"strconv"
78
"strings"
89

10+
"github.com/nyaruka/gocommon/httpx"
911
"github.com/nyaruka/goflow/envs"
1012
"github.com/nyaruka/goflow/excellent/types"
1113
"github.com/nyaruka/goflow/flows"
@@ -100,7 +102,7 @@ func arrayToObject(array *types.XArray) *types.XObject {
100102
}
101103

102104
// finds the last webhook response that was saved as extra on a result
103-
func lastWebhookSavedAsExtra(r *run) types.XValue {
105+
func lastWebhookSavedAsExtra(r *run) *flows.WebhookCall {
104106
for i := len(r.events) - 1; i >= 0; i-- {
105107
switch typed := r.events[i].(type) {
106108
case *events.WebhookCalledEvent:
@@ -110,11 +112,11 @@ func lastWebhookSavedAsExtra(r *run) types.XValue {
110112
if resultEvent != nil {
111113
asResultEvent := resultEvent.(*events.RunResultChangedEvent)
112114
if asResultEvent.Extra != nil {
113-
value := types.JSONToXValue([]byte(asResultEvent.Extra))
114-
if value != nil {
115-
value.SetDeprecated("webhook recreated from extra")
115+
return &flows.WebhookCall{
116+
Trace: &httpx.Trace{Response: &http.Response{}},
117+
ResponseJSON: asResultEvent.Extra,
118+
Recreated: true,
116119
}
117-
return value
118120
}
119121
}
120122
default:

flows/runs/run.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,8 @@ func ReadRun(session flows.Session, data json.RawMessage, missing assets.Missing
471471
}
472472
}
473473

474+
// create context
475+
r.webhook = lastWebhookSavedAsExtra(r)
474476
r.legacyExtra = newLegacyExtra(r)
475477

476478
return r, nil

flows/services.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type WebhookCall struct {
5151
*httpx.Trace
5252
ResponseJSON []byte
5353
ResponseCleaned bool // whether response had to be cleaned to make it valid JSON
54+
Recreated bool // whether the call was recreated from a result
5455
}
5556

5657
// Context returns the properties available in expressions
@@ -66,6 +67,19 @@ func (w *WebhookCall) Context(env envs.Environment) map[string]types.XValue {
6667
headers := types.XObjectEmpty
6768
var json types.XValue
6869

70+
// TODO remove when users stop relying on this
71+
if w.Recreated {
72+
json = types.JSONToXValue(w.ResponseJSON)
73+
if types.IsXError(json) {
74+
json = nil
75+
}
76+
if json != nil {
77+
json.SetDeprecated("webhook recreated from extra")
78+
}
79+
80+
return map[string]types.XValue{"json": json}
81+
}
82+
6983
if w.Response != nil {
7084
status = types.NewXNumberFromInt(w.Response.StatusCode)
7185

test/testdata/runner/webhook_results.test.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -605,14 +605,14 @@
605605
{
606606
"created_on": "2018-07-06T12:31:02.123456789Z",
607607
"step_uuid": "44fe8d72-00ed-4736-acca-bbca70987315",
608-
"text": "error evaluating @webhook.json: null doesn't support lookups",
609-
"type": "error"
608+
"text": "deprecated context value accessed: webhook recreated from extra",
609+
"type": "warning"
610610
},
611611
{
612612
"created_on": "2018-07-06T12:31:04.123456789Z",
613613
"msg": {
614614
"locale": "eng-US",
615-
"text": "Finally..\n\n1. \n2. {greeting: hello}\n3. ",
615+
"text": "Finally..\n\n1. \n2. {greeting: hello}\n3. {greeting: hello}",
616616
"unsendable_reason": "no_destination",
617617
"uuid": "688e64f9-2456-4b42-afcb-91a2073e5459"
618618
},
@@ -805,14 +805,14 @@
805805
{
806806
"created_on": "2018-07-06T12:31:02.123456789Z",
807807
"step_uuid": "44fe8d72-00ed-4736-acca-bbca70987315",
808-
"text": "error evaluating @webhook.json: null doesn't support lookups",
809-
"type": "error"
808+
"text": "deprecated context value accessed: webhook recreated from extra",
809+
"type": "warning"
810810
},
811811
{
812812
"created_on": "2018-07-06T12:31:04.123456789Z",
813813
"msg": {
814814
"locale": "eng-US",
815-
"text": "Finally..\n\n1. \n2. {greeting: hello}\n3. ",
815+
"text": "Finally..\n\n1. \n2. {greeting: hello}\n3. {greeting: hello}",
816816
"unsendable_reason": "no_destination",
817817
"uuid": "688e64f9-2456-4b42-afcb-91a2073e5459"
818818
},

0 commit comments

Comments
 (0)