Skip to content

Commit

Permalink
fix(ci)- fixed golangci linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ansh-devs committed Jul 21, 2024
1 parent b2f6f48 commit 1cb7be2
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 32 deletions.
6 changes: 5 additions & 1 deletion pkg/client/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/configuration/base/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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) {
Expand Down
30 changes: 17 additions & 13 deletions pkg/configuration/user/seedjobs/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions pkg/notifications/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"strings"

Expand Down Expand Up @@ -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))
}
}

Expand Down
11 changes: 6 additions & 5 deletions pkg/notifications/slack/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package slack
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
21 changes: 13 additions & 8 deletions pkg/notifications/smtp/smtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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+<tr>\n\t+<td><b>(.*):</b></td>\n\t+<td>(.*)</td>\n\t+</tr>`)
headersRegex := regexp.MustCompile(`(.*):\s(.*)`)

Expand Down Expand Up @@ -120,9 +125,9 @@ func (s *testSession) Data(r io.Reader) error {
return nil
}

func (s *testSession) Reset() {}
func (s testSession) Reset() {}

func (s *testSession) Logout() error {
func (s testSession) Logout() error {
return nil
}

Expand Down Expand Up @@ -166,11 +171,11 @@ func TestSMTP_Send(t *testing.T) {
},
}}

//ts := &testServer{event: e}
ts := &testServer{event: e}

// Create fake SMTP server
be := *new(smtp.Backend)
s := smtp.NewServer(be)
// be := *new(smtp.Backend)
s := smtp.NewServer(ts)

s.Addr = fmt.Sprintf(":%d", testSMTPPort)
s.Domain = "localhost"
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/port_forward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func setupPortForwardToPod(namespace, podName string, podPort int) (port int, cl
close(stopCh)
}

return
return port, cleanUpFunc, waitFunc, portForwardFunc, err
}

func portForwardToPod(req portForwardToPodRequest) error {
Expand Down

0 comments on commit 1cb7be2

Please sign in to comment.