@@ -52,67 +52,60 @@ func (d *database) GetWebhookLastRequest(id string) (string, error) {
5252
5353func (d * database ) GetWebhooks () (hooks []WebhookGetResponseDTO , err error ) {
5454
55- order , data , err := InternalConfig .Webhooks .Active ().List (context .Background (), d .etcd , clientv3 .WithSort (clientv3 .SortByKey , clientv3 .SortDescend ))
55+ result , err := InternalConfig .Webhooks .Active ().List (context .Background (), d .etcd , clientv3 .WithSort (clientv3 .SortByKey , clientv3 .SortDescend ))
5656 if err != nil {
5757 return nil , err
5858 }
5959
6060 // otherwise json returns null
61- hooks = make ([]WebhookGetResponseDTO , 0 , len (data ))
61+ hooks = make ([]WebhookGetResponseDTO , 0 , len (result . Values ))
6262
6363 txn := tetcd .NewTxn (context .Background (), d .etcd )
6464 then := txn .Then ()
6565
66- for _ , id := range order {
67- hooks = append (hooks , WebhookGetResponseDTO {Webhook : data [id ]})
68-
69- tetcd .GetTx (then , InternalConfig .Webhooks .LastRequests .Time ().Key (id ), clientv3 .WithRev (response .Header .Revision ))
70- tetcd .GetTx (then , InternalConfig .Webhooks .LastRequests .Status ().Key (id ), clientv3 .WithRev (response .Header .Revision ))
66+ type lastRequestDataHandles struct {
67+ time * tetcd.GetHandle [time.Time ]
68+ status * tetcd.GetHandle [string ]
7169 }
7270
73- resp , err := d .etcd .Txn (context .Background ()).Then (lastRequestOps ... ).Commit ()
74- // we'll just have no last_request times so this isnt critical
75- // intentional == nil
76- if err == nil {
77- for i := range resp .Responses {
71+ handles := make ([]lastRequestDataHandles , 0 , len (result .Order ))
7872
79- if len (resp .Responses [i ].GetResponseRange ().Kvs ) == 0 {
80- // webhook has never fired so ignore
81- continue
82- }
73+ for _ , id := range result .Order {
74+ hooks = append (hooks , WebhookGetResponseDTO {Webhook : result .Values [id ]})
8375
84- var t time. Time
85- err = json . Unmarshal ( resp . Responses [ i ]. GetResponseRange (). Kvs [ 0 ]. Value , & t )
86- if err != nil {
87- log . Error (). Err ( err ). Str ( "last_request" , string ( resp . Responses [ i ]. GetResponseRange (). Kvs [ 0 ]. Key )). Msg ( "could not unmarshal last request time from webhook" )
88- continue
89- }
76+ handles = append ( handles ,
77+ lastRequestDataHandles {
78+ time : tetcd . GetTx ( then ,
79+ InternalConfig . Webhooks . LastRequests . Time (). Key ( id ),
80+ clientv3 . WithRev ( result . Rev ),
81+ ),
9082
91- // As we're generating the txn list in order of the hooks we can do this. Its bad code and Im sure it'll blow up, but hey. Funni
92- hooks [i ].LastRequestTime = t
93- }
83+ status : tetcd .GetTx (then ,
84+ InternalConfig .Webhooks .LastRequests .Status ().Key (id ),
85+ clientv3 .WithRev (result .Rev ),
86+ ),
87+ })
9488 }
9589
96- resp , err = d . etcd . Txn ( context . Background ()). Then ( lastRequestStatusOps ... ) .Commit ()
97- // we'll just have no last_request status' so this isnt critical
90+ err = txn .Commit ()
91+ // we'll just have no last_request times so this isnt critical
9892 // intentional == nil
9993 if err == nil {
100- for i := range resp . Responses {
94+ for i := range handles {
10195
102- if len (resp .Responses [i ].GetResponseRange ().Kvs ) == 0 {
103- // webhook has never fired so ignore
96+ status , err := handles [i ].status .Value ()
97+ if err != nil {
98+ log .Info ().Err (err ).Msg ("could not fetch last request status from webhook" )
10499 continue
105100 }
106-
107- var status string
108- err = json .Unmarshal (resp .Responses [i ].GetResponseRange ().Kvs [0 ].Value , & status )
101+ time , err := handles [i ].time .Value ()
109102 if err != nil {
110-
111- log .Info ().Err (err ).Str ("last_request" , string (resp .Responses [i ].GetResponseRange ().Kvs [0 ].Key )).Msg ("could not unmarshal last request status from webhook" )
103+ log .Info ().Err (err ).Msg ("could not fetch last request time from webhook" )
112104 continue
113105 }
114106
115107 // As we're generating the txn list in order of the hooks we can do this. Its bad code and Im sure it'll blow up, but hey. Funni
108+ hooks [i ].LastRequestTime = time
116109 hooks [i ].LastRequestStatus = status
117110 }
118111 }
@@ -313,22 +306,22 @@ func (d *database) CreateTempWebhook() (string, string, error) {
313306 return temp .ID , authHeader , err
314307}
315308
316- func (d * database ) DeleteWebhooks (ids []string , txn * tetcd. TxnConditional ) error {
309+ func (d * database ) DeleteWebhooks (ids []string ) error {
317310
318- var ops []clientv3.Op
311+ txn := tetcd .NewTxn (context .Background (), d .etcd )
312+ then := txn .Then ()
319313
320314 for _ , id := range ids {
321315
322- tetcd .DeleteTx (txn , InternalConfig .Webhooks .Active ().Key (id ), clientv3 .WithPrefix ())
323- tetcd .DeleteTx (txn , InternalConfig .Webhooks .Auth ().Key (id ), clientv3 .WithPrefix ())
316+ tetcd .DeleteTx (then , InternalConfig .Webhooks .Active ().Key (id ), clientv3 .WithPrefix ())
317+ tetcd .DeleteTx (then , InternalConfig .Webhooks .Auth ().Key (id ), clientv3 .WithPrefix ())
324318 // this is a little gross, it might be better to make last requests a map
325- tetcd .DeleteTx (txn , InternalConfig .Webhooks .LastRequests .Data ().Key (id ), clientv3 .WithPrefix ())
326- tetcd .DeleteTx (txn , InternalConfig .Webhooks .LastRequests .Status ().Key (id ), clientv3 .WithPrefix ())
327- tetcd .DeleteTx (txn , InternalConfig .Webhooks .LastRequests .Time ().Key (id ), clientv3 .WithPrefix ())
319+ tetcd .DeleteTx (then , InternalConfig .Webhooks .LastRequests .Data ().Key (id ), clientv3 .WithPrefix ())
320+ tetcd .DeleteTx (then , InternalConfig .Webhooks .LastRequests .Status ().Key (id ), clientv3 .WithPrefix ())
321+ tetcd .DeleteTx (then , InternalConfig .Webhooks .LastRequests .Time ().Key (id ), clientv3 .WithPrefix ())
328322 }
329323
330- _ , err := d .etcd .Txn (context .Background ()).Then (ops ... ).Commit ()
331- return err
324+ return txn .Commit ()
332325}
333326
334327func Unpack (parent string , c map [string ]any ) []WebhookAttribute {
0 commit comments