Skip to content

Commit be7a48a

Browse files
authored
Update webhooks package README.md (#104)
* Update README.md * Flip if else ordering for project created
1 parent 529cb48 commit be7a48a

2 files changed

Lines changed: 45 additions & 17 deletions

File tree

api/pkg/webhooks/README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ method in the caller code based on the event.
5050

5151
#### Optional webhooks events
5252

53-
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()`
53+
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.
5454

5555
For example:
5656

@@ -65,6 +65,33 @@ For example:
6565
}, webhooks.NoOpErrorHandler)
6666
}
6767
```
68+
example config:
69+
```yaml
70+
webhooks:
71+
enabled: true
72+
config:
73+
OnProjectCreated:
74+
- url: http://localhost:8081/project_created
75+
method: POST
76+
finalResponse: true
77+
name: webhook1
78+
OnProjectUpdated: # <-- this must always be set if no check is performed before InvokeWebhooks() is called
79+
- url: http://localhost:8081/project_updated
80+
method: POST
81+
finalResponse: true
82+
name: webhook2
83+
```
84+
Checking if the event exists allows users to just specify a subset of the events available, in this case only `OnProjectCreated` is set.
85+
```yaml
86+
webhooks:
87+
enabled: true
88+
config:
89+
OnProjectCreated:
90+
- url: http://localhost:8081/project_created
91+
method: POST
92+
finalResponse: true
93+
name: webhook1
94+
```
6895

6996
### Single Webhook Configuration
7097

api/service/projects_service.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,26 @@ func (service *projectsService) CreateProject(ctx context.Context, project *mode
8080
return nil, fmt.Errorf("error while creating authorization policy for project %s", project.Name)
8181
}
8282
}
83+
if service.webhookManager == nil || !service.webhookManager.IsEventConfigured(ProjectCreatedEvent) {
84+
return project, nil
85+
}
8386

84-
if service.webhookManager != nil && service.webhookManager.IsEventConfigured(ProjectCreatedEvent) {
85-
err = service.webhookManager.InvokeWebhooks(ctx, ProjectCreatedEvent, project, func(p []byte) error {
86-
// Expects webhook output to be a project object
87-
var tmpproject models.Project
88-
if err := json.Unmarshal(p, &tmpproject); err != nil {
89-
return err
90-
}
91-
project, err = service.save(&tmpproject)
92-
if err != nil {
93-
return err
94-
}
95-
return nil
96-
}, webhooks.NoOpErrorHandler)
87+
err = service.webhookManager.InvokeWebhooks(ctx, ProjectCreatedEvent, project, func(p []byte) error {
88+
// Expects webhook output to be a project object
89+
var tmpproject models.Project
90+
if err := json.Unmarshal(p, &tmpproject); err != nil {
91+
return err
92+
}
93+
project, err = service.save(&tmpproject)
9794
if err != nil {
98-
return project,
99-
fmt.Errorf("error while invoking %s webhooks or on success callback function, err: %s",
100-
ProjectCreatedEvent, err.Error())
95+
return err
10196
}
97+
return nil
98+
}, webhooks.NoOpErrorHandler)
99+
if err != nil {
100+
return project,
101+
fmt.Errorf("error while invoking %s webhooks or on success callback function, err: %s",
102+
ProjectCreatedEvent, err.Error())
102103
}
103104
return project, nil
104105
}

0 commit comments

Comments
 (0)