-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathwebhook.go
More file actions
127 lines (109 loc) · 3.54 KB
/
webhook.go
File metadata and controls
127 lines (109 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
* SPDX-License-Identifier: Apache-2.0
*/
package resolve
import (
"context"
"encoding/json"
"net/http"
"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/hypermodeinc/dgraph/v24/graphql/authorization"
"github.com/hypermodeinc/dgraph/v24/graphql/schema"
"github.com/hypermodeinc/dgraph/v24/x"
)
type webhookPayload struct {
Resolver string `json:"resolver"`
AccessJWT string `json:"X-Dgraph-AccessToken,omitempty"`
AuthHeader *authHeaderPayload `json:"authHeader,omitempty"`
Event eventPayload `json:"event"`
}
type authHeaderPayload struct {
Key string `json:"key"`
Value string `json:"value"`
}
type eventPayload struct {
Typename string `json:"__typename"`
Operation schema.MutationType `json:"operation"`
CommitTs uint64 `json:"commitTs"`
Add *addEvent `json:"add,omitempty"`
Update *updateEvent `json:"update,omitempty"`
Delete *deleteEvent `json:"delete,omitempty"`
}
type addEvent struct {
RootUIDs []string `json:"rootUIDs"`
Input []interface{} `json:"input"`
}
type updateEvent struct {
RootUIDs []string `json:"rootUIDs"`
SetPatch interface{} `json:"setPatch"`
RemovePatch interface{} `json:"removePatch"`
}
type deleteEvent struct {
RootUIDs []string `json:"rootUIDs"`
}
// sendWebhookEvent forms an HTTP payload required for the webhooks configured with @lambdaOnMutate
// directive, and then sends that payload to the lambda URL configured with Alpha. There is no
// guarantee that the payload will be delivered successfully to the lambda server.
func sendWebhookEvent(ctx context.Context, m schema.Mutation, commitTs uint64, rootUIDs []string) {
accessJWT, _ := x.ExtractJwt(ctx)
var authHeader *authHeaderPayload
if m.GetAuthMeta() != nil {
authHeader = &authHeaderPayload{
Key: m.GetAuthMeta().GetHeader(),
Value: authorization.GetJwtToken(ctx),
}
}
payload := webhookPayload{
Resolver: "$webhook",
AccessJWT: accessJWT,
AuthHeader: authHeader,
Event: eventPayload{
Typename: m.MutatedType().Name(),
Operation: m.MutationType(),
CommitTs: commitTs,
},
}
switch payload.Event.Operation {
case schema.AddMutation:
input, _ := m.ArgValue(schema.InputArgName).([]interface{})
payload.Event.Add = &addEvent{
RootUIDs: rootUIDs,
Input: input,
}
case schema.UpdateMutation:
inp, _ := m.ArgValue(schema.InputArgName).(map[string]interface{})
payload.Event.Update = &updateEvent{
RootUIDs: rootUIDs,
SetPatch: inp["set"],
RemovePatch: inp["remove"],
}
case schema.DeleteMutation:
payload.Event.Delete = &deleteEvent{RootUIDs: rootUIDs}
}
b, err := json.Marshal(payload)
if err != nil {
glog.Error(errors.Wrap(err, "error marshalling webhook payload"))
// don't care to send the payload if there are JSON marshalling errors
return
}
// send the request
ns, _ := x.ExtractNamespace(ctx)
headers := http.Header{}
headers.Set("Content-Type", "application/json")
resp, err := schema.MakeHttpRequest(nil, http.MethodPost, x.LambdaUrl(ns), headers, b)
// just log the response errors, if any.
if err != nil {
glog.V(3).Info(errors.Wrap(err, "unable to send webhook event"))
return
}
defer func() {
if err = resp.Body.Close(); err != nil {
glog.Errorf("Error while closing response body: %v", err)
}
}()
if resp != nil && (resp.StatusCode < 200 || resp.StatusCode >= 300) {
glog.V(3).Info(errors.Errorf("got unsuccessful status from webhook: %s", resp.Status))
}
}