-
Notifications
You must be signed in to change notification settings - Fork 140
/
webhook.go
120 lines (103 loc) · 2.86 KB
/
webhook.go
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
// Copyright 2024 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package notifier
import (
"bytes"
"context"
"encoding/json"
"net/http"
"strings"
"time"
"go.uber.org/zap"
"github.com/pipe-cd/pipecd/pkg/config"
"github.com/pipe-cd/pipecd/pkg/model"
)
const eventChannelBufferSize = 1000
type webhook struct {
name string
config config.NotificationReceiverWebhook
webURL string
httpClient *http.Client
eventCh chan model.NotificationEvent
logger *zap.Logger
}
func newWebhookSender(name string, cfg config.NotificationReceiverWebhook, webURL string, logger *zap.Logger) *webhook {
return &webhook{
name: name,
config: cfg,
webURL: strings.TrimRight(webURL, "/"),
httpClient: &http.Client{
Timeout: 5 * time.Second,
},
eventCh: make(chan model.NotificationEvent, eventChannelBufferSize),
logger: logger.Named("webhook").With(zap.String("name", name)),
}
}
func (w *webhook) Run(ctx context.Context) error {
for {
select {
case event, ok := <-w.eventCh:
if ok {
w.sendEvent(ctx, event)
}
case <-ctx.Done():
return nil
}
}
}
func (w *webhook) Notify(event model.NotificationEvent) {
w.eventCh <- event
}
func (w *webhook) sendEvent(ctx context.Context, event model.NotificationEvent) {
buf := &bytes.Buffer{}
if err := json.NewEncoder(buf).Encode(event); err != nil {
w.logger.Error("unable to send data to webhook url", zap.Error(err))
return
}
req, err := http.NewRequestWithContext(ctx, "POST", w.config.URL, buf)
if err != nil {
w.logger.Error("unable to send data to webhook url", zap.Error(err))
return
}
signature, err := w.config.LoadSignatureValue()
if err != nil {
w.logger.Error("unable to load webhook signature value", zap.Error(err))
return
}
req.Header.Add(w.config.SignatureKey, signature)
resp, err := w.httpClient.Do(req)
if err != nil {
w.logger.Error("unable to send data to webhook url", zap.Error(err))
return
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
w.logger.Warn("unexpected status was returned from the destination of webhook", zap.String("status", resp.Status))
}
}
func (w *webhook) Close(ctx context.Context) {
close(w.eventCh)
// Send all remaining events.
for {
select {
case event, ok := <-w.eventCh:
if !ok {
return
}
w.sendEvent(ctx, event)
case <-ctx.Done():
return
}
}
}