-
Notifications
You must be signed in to change notification settings - Fork 40
/
webhook.go
40 lines (36 loc) · 950 Bytes
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package job
import (
"context"
"github.com/rs/zerolog/log"
"github.com/runabol/tork"
"github.com/runabol/tork/internal/webhook"
)
func Webhook(next HandlerFunc) HandlerFunc {
return func(ctx context.Context, et EventType, j *tork.Job) error {
if err := next(ctx, et, j); err != nil {
return err
}
if et != StateChange {
return nil
}
if len(j.Webhooks) == 0 {
return nil
}
summary := tork.NewJobSummary(j)
for _, wh := range j.Webhooks {
if wh.Event != webhook.EventJobStateChange && wh.Event != webhook.EventDefault {
continue
}
go func(w *tork.Webhook) {
callWebhook(w, summary)
}(wh)
}
return nil
}
}
func callWebhook(wh *tork.Webhook, summary *tork.JobSummary) {
log.Debug().Msgf("[Webhook] Calling %s for job %s %s", wh.URL, summary.ID, summary.State)
if err := webhook.Call(wh, summary); err != nil {
log.Error().Err(err).Msgf("[Webhook] error calling job webhook %s", wh.URL)
}
}