Skip to content

Commit

Permalink
Fix lines issue
Browse files Browse the repository at this point in the history
  • Loading branch information
shydefoo committed May 17, 2024
1 parent 56ac7f3 commit ec1659c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
12 changes: 8 additions & 4 deletions api/pkg/webhooks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ var EventList = []wh.EventType{
}
```
2. Define the event to webhook configuration. Optionally, the configuration can be provided in a yaml file and parsed via the `Config` struct. In the config file, define the event to webhook mapping for those events as required. For example, if projects need extra labels from an external source, we define the webhook config for the `OnProjectCreated` event
2. Define the event to webhook configuration. Optionally, the configuration can be provided in a yaml file
and parsed via the `Config` struct.
In the config file, define the event to webhook mapping for those events as required.
For example, if projects need extra labels from an external source,
we define the webhook config for the `OnProjectCreated` event
```go
webhooks:
Expand Down Expand Up @@ -82,11 +86,11 @@ type simpleWebhookClient struct {

// WebhookConfig struct is the configuration for each webhook to be called
type WebhookConfig struct {
Name string `yaml:"name" validate:"required"`
URL string `yaml:"url" validate:"required,url"`
Name string `yaml:"name" validate:"required"`
URL string `yaml:"url" validate:"required,url"`
Method string `yaml:"method"`
AuthEnabled bool `yaml:"authEnabled"`
AuthToken string `yaml:"authToken" validate:"required_if=AuthEnabled True"`
AuthToken string `yaml:"authToken" validate:"required_if=AuthEnabled True"`
OnError string `yaml:"onError"`
Async bool `yaml:"async"`
NumRetries int `yaml:"numRetries"`
Expand Down
32 changes: 27 additions & 5 deletions api/pkg/webhooks/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import (
)

type WebhookManager interface {
InvokeWebhooks(context.Context, EventType, interface{}, func([]byte) error, func(error) error) error
InvokeWebhooks(
context.Context,
EventType,
interface{},
func([]byte) error,
func(error) error,
) error
}

type SimpleWebhookManager struct {
Expand All @@ -24,7 +30,13 @@ type SimpleWebhookManager struct {
// This can be specified in the UseDataFrom field
// For async clients, the payload is only the original input payload.
// Only one webhook's response can be used as the finalResponse
func (w *SimpleWebhookManager) InvokeWebhooks(ctx context.Context, event EventType, p interface{}, onSuccess func([]byte) error, onError func(error) error) error {
func (w *SimpleWebhookManager) InvokeWebhooks(
ctx context.Context,
event EventType,
p interface{},
onSuccess func([]byte) error,
onError func(error) error,
) error {
var asyncClients []WebhookClient
var syncClients []WebhookClient
var finalResponse []byte
Expand Down Expand Up @@ -54,7 +66,10 @@ func (w *SimpleWebhookManager) InvokeWebhooks(ctx context.Context, event EventTy
} else if tmpPayload, ok = responsePayloadLookup[client.GetUseDataFrom()]; !ok {
// This should only happen if a previous error had an error, but did not abort
// and the current client is trying to use the response from that client
return fmt.Errorf("webhook name %s not found, this could be because an error in a downstream webhook", client.GetUseDataFrom())
return fmt.Errorf(
"webhook name %s not found, this could be because an error in a downstream webhook",
client.GetUseDataFrom(),
)
}
p, err := client.Invoke(ctx, tmpPayload)
if err == nil {
Expand Down Expand Up @@ -85,7 +100,10 @@ func (w *SimpleWebhookManager) InvokeWebhooks(ctx context.Context, event EventTy
return nil
}

func parseAndValidateConfig(eventList []EventType, webhookConfigMap map[EventType][]WebhookConfig) (WebhookManager, error) {
func parseAndValidateConfig(
eventList []EventType,
webhookConfigMap map[EventType][]WebhookConfig,
) (WebhookManager, error) {
eventToWHMap := make(map[EventType][]WebhookClient)
for _, eventType := range eventList {
if webhookConfigList, ok := webhookConfigMap[eventType]; ok {
Expand Down Expand Up @@ -162,7 +180,11 @@ func validateSyncClients(webhookClients []WebhookClient) error {
return fmt.Errorf("webhook name %s not found", client.GetUseDataFrom())
}
if useIdx > idx {
return fmt.Errorf("webhook name %s must be defined before %s", client.GetUseDataFrom(), client.GetName())
return fmt.Errorf(
"webhook name %s must be defined before %s",
client.GetUseDataFrom(),
client.GetName(),
)
}
}

Expand Down
16 changes: 14 additions & 2 deletions api/pkg/webhooks/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,13 @@ func TestInvokeWebhooksSimple(t *testing.T) {
}

// Execution
err := webhookManager.InvokeWebhooks(context.Background(), "validEvent", &testPayloadData, onSuccess(t, response), onError)
err := webhookManager.InvokeWebhooks(
context.Background(),
"validEvent",
&testPayloadData,
onSuccess(t, response),
onError,
)

// Assertion
assert.NoError(t, err) // Expect no error
Expand Down Expand Up @@ -315,7 +321,13 @@ func TestInvokeMultipleSyncWebhooks(t *testing.T) {
},
}
// Execution
err := webhookManager.InvokeWebhooks(context.Background(), "validEvent", &testPayloadData, onSuccess(t, response), onError)
err := webhookManager.InvokeWebhooks(
context.Background(),
"validEvent",
&testPayloadData,
onSuccess(t, response),
onError,
)

// Assertion
assert.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion api/service/projects_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ func (service *projectsService) CreateProject(ctx context.Context, project *mode
return nil
}, webhooks.NoOpErrorHandler)
if err != nil {
return project, fmt.Errorf("error while invoking %s webhooks were not invoked successfully %s", ProjectCreatedEvent, err.Error())
return project,
fmt.Errorf("error while invoking %s webhooks were not invoked successfully %s", ProjectCreatedEvent, err.Error())
}
}
return project, nil
Expand Down

0 comments on commit ec1659c

Please sign in to comment.