Skip to content

Commit

Permalink
Update webhooks package README.md (#104)
Browse files Browse the repository at this point in the history
* Update README.md

* Flip if else ordering for project created
  • Loading branch information
shydefoo authored Jun 5, 2024
1 parent 529cb48 commit be7a48a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
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

0 comments on commit be7a48a

Please sign in to comment.