Skip to content

Commit

Permalink
Use validator package, remove onError
Browse files Browse the repository at this point in the history
  • Loading branch information
shydefoo committed Jun 4, 2024
1 parent f9f9350 commit 720ccf0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 54 deletions.
19 changes: 6 additions & 13 deletions api/pkg/webhooks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,12 @@ import (

"github.com/avast/retry-go/v4"
"github.com/caraml-dev/mlp/api/log"
"github.com/go-playground/validator/v10"
)

type EventType string
type ServiceType string

const (
onErrorIgnore = "ignore"
onErrorAbort = "abort"
)

type WebhookClient interface {
Invoke(context.Context, []byte) ([]byte, error)
InvokeAsync(context.Context, []byte) error
Expand Down Expand Up @@ -146,14 +142,11 @@ func (g *simpleWebhookClient) GetName() string {
}

func validateWebhookConfig(webhookConfig *WebhookConfig) error {
if webhookConfig.Name == "" {
return fmt.Errorf("missing webhook name")
}
if webhookConfig.URL == "" {
return fmt.Errorf("missing webhook URL")
}
if webhookConfig.AuthEnabled && webhookConfig.AuthToken == "" {
return fmt.Errorf("missing webhook auth token")
validate := validator.New()

err := validate.Struct(webhookConfig)
if err != nil {
return fmt.Errorf("failed to validate configuration: %s", err)
}
if webhookConfig.NumRetries < 0 {
return fmt.Errorf("numRetries must be a non-negative integer")
Expand Down
36 changes: 19 additions & 17 deletions api/pkg/webhooks/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type WebhookManager interface {
}

type SimpleWebhookManager struct {
WebhookClients map[EventType]map[WebhookType][]WebhookClient
SyncClients map[EventType][]WebhookClient
AsyncClients map[EventType][]WebhookClient
}

// InvokeWebhooks iterates through the webhooks for a given event and invokes them.
Expand All @@ -38,16 +39,15 @@ func (w *SimpleWebhookManager) InvokeWebhooks(
onError func(error) error,
) error {
finalResponse := make([]byte, 0)
whc, ok := w.WebhookClients[event]
if !ok {
syncClients, ok := w.SyncClients[event]
asyncClients, ok1 := w.AsyncClients[event]
if !ok && !ok1 {
return fmt.Errorf("Could not find event %s", event)
}
originalPayload, err := json.Marshal(p)
if err != nil {
return err
}
asyncClients := whc[Async]
syncClients := whc[Sync]

// Mapping to store response from different webhooks
responsePayloadLookup := make(map[string][]byte)
Expand All @@ -56,12 +56,15 @@ func (w *SimpleWebhookManager) InvokeWebhooks(
var tmpPayload []byte
if client.GetUseDataFrom() == "" {
tmpPayload = originalPayload
} else if tmpPayload, ok = responsePayloadLookup[client.GetUseDataFrom()]; !ok {
// NOTE: This should never happen!
return fmt.Errorf(
"webhook name %s not found, this could be because of an error in a previous webhook that this webhook depends on",
client.GetUseDataFrom(),
)
} else {
tmpPayload, ok = responsePayloadLookup[client.GetUseDataFrom()]
if !ok {
// NOTE: This should never happen!
return fmt.Errorf(
"webhook name %s not found, this could be because of an error in a previous webhook that this webhook depends on",
client.GetUseDataFrom(),
)
}
}
p, err := client.Invoke(ctx, tmpPayload)
if err != nil {
Expand Down Expand Up @@ -90,7 +93,8 @@ func parseAndValidateConfig(
eventList []EventType,
webhookConfigMap map[EventType][]WebhookConfig,
) (WebhookManager, error) {
eventToWHMap := make(map[EventType]map[WebhookType][]WebhookClient)
syncClientMap := make(map[EventType][]WebhookClient)
asyncClientMap := make(map[EventType][]WebhookClient)
for _, eventType := range eventList {
webhookConfigList, ok := webhookConfigMap[eventType]
if !ok {
Expand All @@ -116,12 +120,10 @@ func parseAndValidateConfig(
if err := validateClients(allClients); err != nil {
return nil, err
}
tmpMap := make(map[WebhookType][]WebhookClient)
tmpMap[Async] = asyncClients
tmpMap[Sync] = syncClients
eventToWHMap[eventType] = tmpMap
syncClientMap[eventType] = syncClients
asyncClientMap[eventType] = asyncClients
}
return &SimpleWebhookManager{WebhookClients: eventToWHMap}, nil
return &SimpleWebhookManager{AsyncClients: asyncClientMap, SyncClients: syncClientMap}, nil

}

Expand Down
8 changes: 4 additions & 4 deletions api/pkg/webhooks/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ func TestInvokeWebhooksSimple(t *testing.T) {

// Setup WebhookManager with the mock client
webhookManager := &SimpleWebhookManager{
WebhookClients: map[EventType]map[WebhookType][]WebhookClient{
"validEvent": {Sync: {mockClient}},
SyncClients: map[EventType][]WebhookClient{
"validEvent": {mockClient},
},
}

Expand Down Expand Up @@ -344,8 +344,8 @@ func TestInvokeMultipleSyncWebhooks(t *testing.T) {
mockClient2.On("IsFinalResponse").Return(true)
// Setup WebhookManager with the mock client
webhookManager := &SimpleWebhookManager{
WebhookClients: map[EventType]map[WebhookType][]WebhookClient{
"validEvent": {Sync: {mockClient, mockClient2}},
SyncClients: map[EventType][]WebhookClient{
"validEvent": {mockClient, mockClient2},
},
}
// Execution
Expand Down
18 changes: 0 additions & 18 deletions api/pkg/webhooks/mock_WebhookClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions config-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ defaultSecretStorage:
# ONLY FOR TESTING PURPOSES
authMethod: token
token: root

# webhooks:
# enabled: true
# config:
# OnProjectCreated:
# - url: http://localhost:8081/project_created
# method: POST
# onError: abort

0 comments on commit 720ccf0

Please sign in to comment.