webhookd/pkg/notification/notifier.go

55 lines
1.1 KiB
Go
Raw Normal View History

package notification
import (
"fmt"
"net/url"
"strings"
"github.com/ncarlier/webhookd/pkg/logger"
)
// Notifier is able to send a notification.
type Notifier interface {
2022-05-26 07:05:49 +00:00
Notify(result HookResult) error
}
var notifier Notifier
2022-05-26 07:05:49 +00:00
// Notify is the global method to notify hook result
func Notify(result HookResult) {
if notifier == nil {
return
}
2022-05-26 07:05:49 +00:00
if err := notifier.Notify(result); err != nil {
logger.Error.Printf("unable to send notification for webhook %s#%d: %v\n", result.Name(), result.ID(), err)
}
}
// Init creates a notifier regarding the URI.
func Init(uri string) error {
if uri == "" {
return nil
}
u, err := url.Parse(uri)
if err != nil {
return fmt.Errorf("invalid notification URL: %s", uri)
}
switch u.Scheme {
case "mailto":
notifier = newSMTPNotifier(u)
case "http", "https":
notifier = newHTTPNotifier(u)
default:
return fmt.Errorf("unable to create notifier: %v", err)
}
return nil
}
func getValueOrAlt(values url.Values, key, alt string) string {
if val, ok := values[key]; ok {
return strings.Join(val[:], ",")
}
return alt
}