diff --git a/internal/notifier/mailgun.go b/internal/notifier/mailgun.go index 3bf190b9..7f7e5e9f 100644 --- a/internal/notifier/mailgun.go +++ b/internal/notifier/mailgun.go @@ -7,6 +7,7 @@ import ( "time" "github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" + "github.com/mailgun/mailgun-go/v3" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" diff --git a/internal/notifier/msteams.go b/internal/notifier/msteams.go index f1a1b290..39ca3ae0 100644 --- a/internal/notifier/msteams.go +++ b/internal/notifier/msteams.go @@ -8,6 +8,7 @@ import ( "net/http" "github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" ) diff --git a/internal/notifier/msteams_test.go b/internal/notifier/msteams_test.go index 2205eff2..f19bcf2e 100644 --- a/internal/notifier/msteams_test.go +++ b/internal/notifier/msteams_test.go @@ -85,7 +85,7 @@ func TestTeams_Send(t *testing.T) { err := notification.K8sClient.Create(context.TODO(), secret) assert.NoError(t, err) - assert.NoError(t, teams.Send(notification, v1alpha2.Notification{ + err := teams.Send(notification, v1alpha2.Notification{ Teams: v1alpha2.Teams{ URLSecretKeySelector: v1alpha2.SecretKeySelector{ LocalObjectReference: corev1.LocalObjectReference{ @@ -94,5 +94,6 @@ func TestTeams_Send(t *testing.T) { Key: testURLSelectorKeyName, }, }, - })) + }) + assert.NoError(t, err) } diff --git a/internal/notifier/sender.go b/internal/notifier/sender.go index 9735fcc5..b67bc407 100644 --- a/internal/notifier/sender.go +++ b/internal/notifier/sender.go @@ -4,10 +4,10 @@ import ( "fmt" "net/http" - "github.com/go-logr/logr" "github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" "github.com/jenkinsci/kubernetes-operator/pkg/log" + "github.com/go-logr/logr" k8sclient "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -70,31 +70,30 @@ type service interface { // Listen is goroutine that listens for incoming messages and sends it func Listen(notification chan *Notification) { for n := range notification { - if len(n.Jenkins.Spec.Notifications) > 0 { - for _, notificationConfig := range n.Jenkins.Spec.Notifications { - var err error - var svc service + for _, notificationConfig := range n.Jenkins.Spec.Notifications { + var err error + var svc service - if notificationConfig.Slack != (v1alpha2.Slack{}) { - svc = Slack{} - } else if notificationConfig.Teams != (v1alpha2.Teams{}) { - svc = Teams{} - } else if notificationConfig.Mailgun != (v1alpha2.Mailgun{}) { - svc = Mailgun{} - } else { - n.Logger.V(log.VWarn).Info(fmt.Sprintf("Notification service in `%s` not found or not defined", notificationConfig.Name)) - continue - } + if notificationConfig.Slack != (v1alpha2.Slack{}) { + svc = Slack{} + } else if notificationConfig.Teams != (v1alpha2.Teams{}) { + svc = Teams{} + } else if notificationConfig.Mailgun != (v1alpha2.Mailgun{}) { + svc = Mailgun{} + } else { + n.Logger.V(log.VWarn).Info(fmt.Sprintf("Notification service in `%s` not found or not defined", notificationConfig.Name)) + continue + } - err = notify(svc, n, notificationConfig) + err = notify(svc, n, notificationConfig) - if err != nil { - n.Logger.V(log.VWarn).Info(fmt.Sprintf("Failed to send notifications. %+v", err)) - } else { - n.Logger.V(log.VDebug).Info("Sent notification") - } + if err != nil { + n.Logger.V(log.VWarn).Info(fmt.Sprintf("Failed to send notifications. %+v", err)) + } else { + n.Logger.V(log.VDebug).Info("Sent notification") } } + } } diff --git a/internal/notifier/slack.go b/internal/notifier/slack.go index 185d3393..c8c3ba94 100644 --- a/internal/notifier/slack.go +++ b/internal/notifier/slack.go @@ -4,10 +4,11 @@ import ( "bytes" "context" "encoding/json" - "github.com/pkg/errors" "net/http" "github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" + + "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" ) diff --git a/internal/notifier/slack_test.go b/internal/notifier/slack_test.go index 06da7aae..67cf751f 100644 --- a/internal/notifier/slack_test.go +++ b/internal/notifier/slack_test.go @@ -84,7 +84,7 @@ func TestSlack_Send(t *testing.T) { slack := Slack{} - assert.NoError(t, slack.Send(notification, v1alpha2.Notification{ + err := slack.Send(notification, v1alpha2.Notification{ Slack: v1alpha2.Slack{ URLSecretKeySelector: v1alpha2.SecretKeySelector{ LocalObjectReference: corev1.LocalObjectReference{ @@ -93,5 +93,7 @@ func TestSlack_Send(t *testing.T) { Key: testURLSelectorKeyName, }, }, - })) + }) + + assert.NoError(t, err) }