Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update webhooks package README.md #104

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion api/pkg/webhooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ method in the caller code based on the event.

#### Optional webhooks events

In the event that there are multiple events to be configured, for example `OnProjectCreated` and `OnProjectUpdated`, and only `OnProjectCreated` webhooks should be fired, use the `IsEventConfigured()` method provided by the `WebhookManager` to check if the event is set before calling `InvokeWebhooks()`
In the event that there are multiple events to be configured, for example `OnProjectCreated` and `OnProjectUpdated`, use the `IsEventConfigured()` method provided by the `WebhookManager` to check if the event `OnProjectUpdated` is set before calling `InvokeWebhooks()` for the `OnProjectUpdated`. If this check is not performed, the `OnProjectUpdated` event must always be configured in the webhooks configuration if webhooks are enabled.

For example:

Expand All @@ -65,6 +65,33 @@ For example:
}, webhooks.NoOpErrorHandler)
}
```
example config:
```yaml
webhooks:
enabled: true
config:
OnProjectCreated:
- url: http://localhost:8081/project_created
method: POST
finalResponse: true
name: webhook1
OnProjectUpdated: # <-- this must always be set if no check is performed before InvokeWebhooks() is called
- url: http://localhost:8081/project_updated
method: POST
finalResponse: true
name: webhook2
```
Checking if the event exists allows users to just specify a subset of the events available, in this case only `OnProjectCreated` is set.
```yaml
webhooks:
enabled: true
config:
OnProjectCreated:
- url: http://localhost:8081/project_created
method: POST
finalResponse: true
name: webhook1
```

### Single Webhook Configuration

Expand Down
33 changes: 17 additions & 16 deletions api/service/projects_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,26 @@ func (service *projectsService) CreateProject(ctx context.Context, project *mode
return nil, fmt.Errorf("error while creating authorization policy for project %s", project.Name)
}
}
if service.webhookManager == nil || !service.webhookManager.IsEventConfigured(ProjectCreatedEvent) {
return project, nil
}

if service.webhookManager != nil && service.webhookManager.IsEventConfigured(ProjectCreatedEvent) {
err = service.webhookManager.InvokeWebhooks(ctx, ProjectCreatedEvent, project, func(p []byte) error {
// Expects webhook output to be a project object
var tmpproject models.Project
if err := json.Unmarshal(p, &tmpproject); err != nil {
return err
}
project, err = service.save(&tmpproject)
if err != nil {
return err
}
return nil
}, webhooks.NoOpErrorHandler)
err = service.webhookManager.InvokeWebhooks(ctx, ProjectCreatedEvent, project, func(p []byte) error {
// Expects webhook output to be a project object
var tmpproject models.Project
if err := json.Unmarshal(p, &tmpproject); err != nil {
return err
}
project, err = service.save(&tmpproject)
if err != nil {
return project,
fmt.Errorf("error while invoking %s webhooks or on success callback function, err: %s",
ProjectCreatedEvent, err.Error())
return err
}
return nil
}, webhooks.NoOpErrorHandler)
if err != nil {
return project,
fmt.Errorf("error while invoking %s webhooks or on success callback function, err: %s",
ProjectCreatedEvent, err.Error())
}
return project, nil
}
Expand Down
Loading