-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathnotifier.go
63 lines (57 loc) · 1.98 KB
/
notifier.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package config
import "fmt"
type NotifierConf struct {
Kind NotifierKind `mapstructure:"kind" koanf:"kind"`
// Deprecated: Use WebhookURL instead
SlackWebhookURL string `mapstructure:"slackWebhookUrl" koanf:"slackwebhookurl"`
EndpointURL string `mapstructure:"endpointUrl" koanf:"endpointurl"`
Secret string `mapstructure:"secret" koanf:"secret"`
Meta map[string]string `mapstructure:"meta" koanf:"meta"`
Headers map[string][]string `mapstructure:"headers" koanf:"headers"`
WebhookURL string `mapstructure:"webhookUrl" koanf:"webhookurl"`
}
func (c *NotifierConf) IsValid() error {
if err := c.Kind.IsValid(); err != nil {
return err
}
if c.Kind == SlackNotifier && (c.SlackWebhookURL == "" && c.WebhookURL == "") {
return fmt.Errorf(
"invalid notifier: no \"slackWebhookUrl\" property found for kind \"%s\"",
c.Kind,
)
}
if c.Kind == MicrosoftTeamsNotifier && c.WebhookURL == "" {
return fmt.Errorf(
"invalid notifier: no \"WebhookURL\" property found for kind \"%s\"",
c.Kind,
)
}
if c.Kind == WebhookNotifier && c.EndpointURL == "" {
return fmt.Errorf(
"invalid notifier: no \"endpointUrl\" property found for kind \"%s\"",
c.Kind,
)
}
if c.Kind == DiscordNotifier && c.WebhookURL == "" {
return fmt.Errorf(
"invalid notifier: no \"webhookUrl\" property found for kind \"%s\"",
c.Kind,
)
}
return nil
}
type NotifierKind string
const (
SlackNotifier NotifierKind = "slack"
MicrosoftTeamsNotifier NotifierKind = "microsoftteams"
WebhookNotifier NotifierKind = "webhook"
DiscordNotifier NotifierKind = "discord"
)
// IsValid is checking if the value is part of the enum
func (r NotifierKind) IsValid() error {
switch r {
case SlackNotifier, WebhookNotifier, DiscordNotifier, MicrosoftTeamsNotifier:
return nil
}
return fmt.Errorf("invalid notifier: kind \"%s\" is not supported", r)
}