Skip to content

Commit

Permalink
fix: add the ability to remove webhook token
Browse files Browse the repository at this point in the history
Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Nov 21, 2024
1 parent af6f033 commit 0eafa21
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
28 changes: 17 additions & 11 deletions pkg/api/handlers/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,9 @@ func (a *WebhookHandler) Update(req api.Context) error {
}

func (a *WebhookHandler) Delete(req api.Context) error {
var (
id = req.PathValue("id")
)

return req.Delete(&v1.Webhook{
ObjectMeta: metav1.ObjectMeta{
Name: id,
Name: req.PathValue("id"),
Namespace: req.Namespace(),
},
})
Expand Down Expand Up @@ -159,12 +155,8 @@ func convertWebhook(webhook v1.Webhook, urlPrefix string) *types.Webhook {
}

func (a *WebhookHandler) ByID(req api.Context) error {
var (
wh v1.Webhook
id = req.PathValue("id")
)

if err := alias.Get(req.Context(), req.Storage, &wh, req.Namespace(), id); err != nil {
var wh v1.Webhook
if err := alias.Get(req.Context(), req.Storage, &wh, req.Namespace(), req.PathValue("id")); err != nil {
return err
}

Expand All @@ -185,6 +177,20 @@ func (a *WebhookHandler) List(req api.Context) error {
return req.Write(resp)
}

func (a *WebhookHandler) RemoveToken(req api.Context) error {
var wh v1.Webhook
if err := req.Get(&wh, req.PathValue("id")); err != nil {
return err
}

wh.Spec.TokenHash = nil
if err := req.Update(&wh); err != nil {
return fmt.Errorf("failed to remove token: %w", err)
}

return req.Write(convertWebhook(wh, server.GetURLPrefix(req)))
}

func (a *WebhookHandler) Execute(req api.Context) error {
var webhook v1.Webhook
if err := alias.Get(req.Context(), req.Storage, &webhook, "", req.PathValue("id")); err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func Router(services *services.Services) (http.Handler, error) {
mux.HandleFunc("DELETE /api/webhooks/{id}", webhooks.Delete)
mux.HandleFunc("PUT /api/webhooks/{id}", webhooks.Update)
mux.HandleFunc("POST /api/webhooks/{id}", webhooks.Execute)
mux.HandleFunc("POST /api/webhooks/{id}/remove-token", webhooks.RemoveToken)

// Email Receivers
mux.HandleFunc("POST /api/email-receivers", emailreceiver.Create)
Expand Down

0 comments on commit 0eafa21

Please sign in to comment.