diff --git a/pkg/client/token.go b/pkg/client/token.go index 161e7c80f..6f06ce7c8 100644 --- a/pkg/client/token.go +++ b/pkg/client/token.go @@ -40,7 +40,11 @@ func (jenkins *jenkins) GenerateToken(userName, tokenName string) (*UserToken, e if err != nil { return nil, errors.Wrap(err, "couldn't generate API token") } - defer r.Body.Close() + defer func() { + if err := r.Body.Close(); err != nil { + log.Log.Error(err, "failed to close http response body") + } + }() if err := r.Body.Close(); err != nil { log.Log.Error(err, "failed to close jenkins.GenerateToken.Requester") } diff --git a/pkg/configuration/base/reconciler.go b/pkg/configuration/base/reconciler.go index 431aec8aa..0e033594f 100644 --- a/pkg/configuration/base/reconciler.go +++ b/pkg/configuration/base/reconciler.go @@ -264,7 +264,7 @@ func compareMap(expected, actual map[string]string) bool { } func compareEnv(expected, actual []corev1.EnvVar) bool { - var actualEnv []corev1.EnvVar + actualEnv := []corev1.EnvVar{} for _, env := range actual { if env.Name == "KUBERNETES_PORT_443_TCP_ADDR" || env.Name == "KUBERNETES_PORT" || env.Name == "KUBERNETES_PORT_443_TCP" || env.Name == "KUBERNETES_SERVICE_HOST" { @@ -289,7 +289,7 @@ func CompareContainerVolumeMounts(expected corev1.Container, actual corev1.Conta // compareVolumes returns true if Jenkins pod and Jenkins CR volumes are the same func (r *JenkinsBaseConfigurationReconciler) compareVolumes(actualPod corev1.Pod) bool { - var toCompare []corev1.Volume + toCompare := []corev1.Volume{} for _, volume := range actualPod.Spec.Volumes { // filter out service account if strings.HasPrefix(volume.Name, actualPod.Spec.ServiceAccountName) { diff --git a/pkg/configuration/user/seedjobs/validate.go b/pkg/configuration/user/seedjobs/validate.go index 7ce254e18..9acd4d035 100644 --- a/pkg/configuration/user/seedjobs/validate.go +++ b/pkg/configuration/user/seedjobs/validate.go @@ -16,7 +16,7 @@ import ( // ValidateSeedJobs verify seed jobs configuration func (s *seedJobs) ValidateSeedJobs(jenkins v1alpha2.Jenkins) ([]string, error) { - var messages []string + messages := []string{} if msg := s.validateIfIDIsUnique(jenkins.Spec.SeedJobs); len(msg) > 0 { messages = append(messages, msg...) @@ -88,24 +88,28 @@ func (s *seedJobs) ValidateSeedJobs(jenkins v1alpha2.Jenkins) ([]string, error) } } - if seedJob.GitHubPushTrigger { - if msg := s.validateGitHubPushTrigger(jenkins); len(msg) > 0 { - for _, m := range msg { - messages = append(messages, fmt.Sprintf("seedJob `%s` %s", seedJob.ID, m)) - } + s.setSeedJobPushTriggers(seedJob, messages, jenkins) + } + + return messages, nil +} + +func (s *seedJobs) setSeedJobPushTriggers(seedJob v1alpha2.SeedJob, messages []string, jenkins v1alpha2.Jenkins) { + if seedJob.GitHubPushTrigger { + if msg := s.validateGitHubPushTrigger(jenkins); len(msg) > 0 { + for _, m := range msg { + messages = append(messages, fmt.Sprintf("seedJob `%s` %s", seedJob.ID, m)) } } + } - if seedJob.BitbucketPushTrigger { - if msg := s.validateBitbucketPushTrigger(jenkins); len(msg) > 0 { - for _, m := range msg { - messages = append(messages, fmt.Sprintf("seedJob `%s` %s", seedJob.ID, m)) - } + if seedJob.BitbucketPushTrigger { + if msg := s.validateBitbucketPushTrigger(jenkins); len(msg) > 0 { + for _, m := range msg { + messages = append(messages, fmt.Sprintf("seedJob `%s` %s", seedJob.ID, m)) } } } - - return messages, nil } func (s *seedJobs) validateGitHubPushTrigger(jenkins v1alpha2.Jenkins) []string { diff --git a/pkg/notifications/slack/slack.go b/pkg/notifications/slack/slack.go index acda18471..bfd9b891f 100644 --- a/pkg/notifications/slack/slack.go +++ b/pkg/notifications/slack/slack.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "fmt" "net/http" "strings" @@ -74,11 +75,11 @@ func (s Slack) generateMessage(e event.Event) Message { var messageStringBuilder strings.Builder if s.config.Verbose { for _, msg := range e.Reason.Verbose() { - messageStringBuilder.WriteString("\n - " + msg + "\n") + messageStringBuilder.WriteString(fmt.Sprintf("\n - %s \n", msg)) } } else { for _, msg := range e.Reason.Short() { - messageStringBuilder.WriteString("\n - " + msg + "\n") + messageStringBuilder.WriteString(fmt.Sprintf("\n - %s \n", msg)) } } diff --git a/pkg/notifications/slack/slack_test.go b/pkg/notifications/slack/slack_test.go index e17340885..923959fb3 100644 --- a/pkg/notifications/slack/slack_test.go +++ b/pkg/notifications/slack/slack_test.go @@ -3,6 +3,7 @@ package slack import ( "context" "encoding/json" + "fmt" "net/http" "net/http/httptest" "strings" @@ -80,7 +81,7 @@ func TestSlack_Send(t *testing.T) { case "": message := "" for _, msg := range e.Reason.Short() { - message = message + "\n - " + msg + "\n" + message = message + fmt.Sprintf("\n - %s \n", msg) } assert.Equal(t, field.Value, message) case provider.LevelFieldName: @@ -148,7 +149,7 @@ func TestGenerateMessage(t *testing.T) { var messageStringBuilder strings.Builder for _, msg := range e.Reason.Verbose() { - messageStringBuilder.WriteString("\n - " + msg + "\n") + messageStringBuilder.WriteString(fmt.Sprintf("\n - %s \n", msg)) } mainAttachment := message.Attachments[0] @@ -194,7 +195,7 @@ func TestGenerateMessage(t *testing.T) { var messageStringBuilder strings.Builder for _, msg := range e.Reason.Verbose() { - messageStringBuilder.WriteString("\n - " + msg + "\n") + messageStringBuilder.WriteString(fmt.Sprintf("\n - %s \n", msg)) } mainAttachment := message.Attachments[0] @@ -240,7 +241,7 @@ func TestGenerateMessage(t *testing.T) { var messageStringBuilder strings.Builder for _, msg := range e.Reason.Verbose() { - messageStringBuilder.WriteString("\n - " + msg + "\n") + messageStringBuilder.WriteString(fmt.Sprintf("\n - %s \n", msg)) } mainAttachment := message.Attachments[0] @@ -286,7 +287,7 @@ func TestGenerateMessage(t *testing.T) { var messageStringBuilder strings.Builder for _, msg := range e.Reason.Verbose() { - messageStringBuilder.WriteString("\n - " + msg + "\n") + messageStringBuilder.WriteString(fmt.Sprintf("\n - %s \n", msg)) } mainAttachment := message.Attachments[0] diff --git a/pkg/notifications/smtp/smtp_test.go b/pkg/notifications/smtp/smtp_test.go index 932f483a2..e02796b63 100644 --- a/pkg/notifications/smtp/smtp_test.go +++ b/pkg/notifications/smtp/smtp_test.go @@ -56,6 +56,11 @@ type testServer struct { event event.Event } +// NewSession implements smtp.Backend. +func (t *testServer) NewSession(c *smtp.Conn) (smtp.Session, error) { + return testSession{}, nil +} + // TODO: @brokenpip3 fix me //func (bkd *testServer) Login(_ *smtp.ConnectionState, username, password string) (smtp.Session, error) { // if username != testSMTPUsername || password != testSMTPPassword { @@ -74,21 +79,21 @@ type testSession struct { event event.Event } -func (s *testSession) Mail(from string) error { +func (s testSession) Mail(from string, mop *smtp.MailOptions) error { if from != testFrom { return fmt.Errorf("`From` header is not equal: '%s', expected '%s'", from, testFrom) } return nil } -func (s *testSession) Rcpt(to string) error { +func (s testSession) Rcpt(to string, mop *smtp.RcptOptions) error { if to != testTo { return fmt.Errorf("`To` header is not equal: '%s', expected '%s'", to, testTo) } return nil } -func (s *testSession) Data(r io.Reader) error { +func (s testSession) Data(r io.Reader) error { contentRegex := regexp.MustCompile(`\t+