@@ -3,6 +3,7 @@ package webhook
3
3
import (
4
4
"context"
5
5
"errors"
6
+ "io"
6
7
"net/http"
7
8
"net/url"
8
9
"sync"
@@ -12,9 +13,9 @@ import (
12
13
"github.com/quay/zlog"
13
14
14
15
clairerror "github.com/quay/clair/v4/clair-error"
15
- "github.com/quay/clair/v4/internal/codec"
16
16
"github.com/quay/clair/v4/internal/httputil"
17
- "github.com/quay/clair/v4/notifier"
17
+ "github.com/quay/clair/v4/internal/json"
18
+ "github.com/quay/clair/v4/internal/json/jsontext"
18
19
)
19
20
20
21
// SignedOnce is used to print a deprecation notice, but only once per run.
@@ -67,26 +68,60 @@ func (d *Deliverer) Name() string {
67
68
return "webhook"
68
69
}
69
70
71
+ var options = sync .OnceValue (func () json.Options {
72
+ return json .WithMarshalers (json .MarshalFuncV2 (marshalCallback ))
73
+ })
74
+
75
+ func marshalCallback (enc * jsontext.Encoder , cb * callbackRequest , opts json.Options ) error {
76
+ if err := enc .WriteToken (jsontext .ObjectStart ); err != nil {
77
+ return err
78
+ }
79
+ if err := enc .WriteToken (jsontext .String (`callback` )); err != nil {
80
+ return err
81
+ }
82
+ if err := enc .WriteToken (jsontext .String (cb .URL .String ())); err != nil {
83
+ return err
84
+ }
85
+ if err := enc .WriteToken (jsontext .String (`notification_id` )); err != nil {
86
+ return err
87
+ }
88
+ if err := enc .WriteToken (jsontext .String (cb .ID .String ())); err != nil {
89
+ return err
90
+ }
91
+ return enc .WriteToken (jsontext .ObjectEnd )
92
+ }
93
+
94
+ type callbackRequest struct {
95
+ ID * uuid.UUID
96
+ URL * url.URL
97
+ }
98
+
70
99
// Deliver implements the notifier.Deliverer interface.
71
100
//
72
- // Deliver POSTS a webhook data structure to the configured target.
101
+ // Deliver POSTs a webhook data structure to the configured target.
73
102
func (d * Deliverer ) Deliver (ctx context.Context , nID uuid.UUID ) error {
74
103
ctx = zlog .ContextWithValues (ctx ,
75
104
"component" , "notifier/webhook/Deliverer.Deliver" ,
76
105
"notification_id" , nID .String (),
77
106
)
78
107
79
- callback , err := d .callback .Parse (nID .String ())
108
+ url , err := d .callback .Parse (nID .String ())
80
109
if err != nil {
81
110
return err
82
111
}
83
-
84
- wh := notifier.Callback {
85
- NotificationID : nID ,
86
- Callback : * callback ,
112
+ cb := callbackRequest {
113
+ ID : & nID ,
114
+ URL : url ,
87
115
}
88
116
89
- req , err := httputil .NewRequestWithContext (ctx , http .MethodPost , d .target .String (), codec .JSONReader (& wh ))
117
+ rd , wr := io .Pipe ()
118
+ defer rd .Close ()
119
+ go func () {
120
+ err := json .MarshalWrite (wr , & cb , options ())
121
+ wr .CloseWithError (err )
122
+ }()
123
+
124
+ req , err := httputil .NewRequestWithContext (ctx , http .MethodPost , d .target .String (), rd )
90
125
if err != nil {
91
126
return err
92
127
}
@@ -102,7 +137,7 @@ func (d *Deliverer) Deliver(ctx context.Context, nID uuid.UUID) error {
102
137
}
103
138
104
139
zlog .Info (ctx ).
105
- Stringer ("callback" , callback ).
140
+ Stringer ("callback" , url ).
106
141
Stringer ("target" , d .target ).
107
142
Msg ("dispatching webhook" )
108
143
0 commit comments