Skip to content

Commit

Permalink
refactor: remove code lints
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Davim <[email protected]>
  • Loading branch information
luisdavim committed Nov 23, 2022
1 parent c209547 commit 2c908db
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 31 deletions.
3 changes: 1 addition & 2 deletions cmd/jiralert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func main() {

flag.Parse()

var logger = setupLogger(*logLevel, *logFormat)
logger := setupLogger(*logLevel, *logFormat)
level.Info(logger).Log("msg", "starting JIRAlert", "version", Version)

if !*hashJiraLabel {
Expand Down Expand Up @@ -133,7 +133,6 @@ func main() {
return
}
requestTotal.WithLabelValues(conf.Name, "200").Inc()

})

http.HandleFunc("/", HomeHandlerFunc())
Expand Down
14 changes: 6 additions & 8 deletions cmd/jiralert/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ package main

import "github.com/prometheus/client_golang/prometheus"

var (
requestTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "jiralert_requests_total",
Help: "Requests processed, by receiver and status code.",
},
[]string{"receiver", "code"},
)
var requestTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "jiralert_requests_total",
Help: "Requests processed, by receiver and status code.",
},
[]string{"receiver", "code"},
)

func init() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func LoadFile(filename string, logger log.Logger) (*Config, []byte, error) {
// expand env variables $(var) from the config file
// taken from https://github.dev/thanos-io/thanos/blob/296c4ab4baf2c8dd6abdf2649b0660ac77505e63/pkg/reloader/reloader.go#L445-L462 by https://github.com/fabxc
func substituteEnvVars(b []byte, logger log.Logger) (r []byte, err error) {
var envRe = regexp.MustCompile(`\$\(([a-zA-Z_0-9]+)\)`)
envRe := regexp.MustCompile(`\$\(([a-zA-Z_0-9]+)\)`)
r = envRe.ReplaceAllFunc(b, func(n []byte) []byte {
if err != nil {
return nil
Expand All @@ -92,7 +92,7 @@ func substituteEnvVars(b []byte, logger log.Logger) (r []byte, err error) {

v, ok := os.LookupEnv(string(n))
if !ok {
err = fmt.Errorf("Missing env variable: %q", n)
err = fmt.Errorf("missing env variable: %q", n)
return nil
}
return []byte(v)
Expand Down
27 changes: 12 additions & 15 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,10 @@ func TestLoadFile(t *testing.T) {

require.NoError(t, err)
require.Equal(t, testConf, string(content))

}

// Checks if the env var substitution is happening correctly in the loaded file
func TestEnvSubstitution(t *testing.T) {

config := "user: $(JA_USER)"
os.Setenv("JA_USER", "user")

Expand All @@ -106,7 +104,6 @@ func TestEnvSubstitution(t *testing.T) {
config = "user: $(JA_MISSING)"
_, err = substituteEnvVars([]byte(config), log.NewNopLogger())
require.Error(t, err)

}

// A test version of the ReceiverConfig struct to create test yaml fixtures.
Expand Down Expand Up @@ -186,7 +183,6 @@ func TestRequiredReceiverConfigKeys(t *testing.T) {
}
configErrorTestRunner(t, config, test.errorMessage)
}

}

// Auth keys error scenarios.
Expand Down Expand Up @@ -352,7 +348,6 @@ func TestReceiverOverrides(t *testing.T) {
configValue := reflect.ValueOf(receiver).Elem().FieldByName(test.overrideField).Interface()
require.Equal(t, configValue, test.expectedValue)
}

}

// TODO(bwplotka, rporres). Add more tests:
Expand All @@ -366,11 +361,12 @@ func newReceiverTestConfig(mandatory []string, optional []string) *receiverTestC

for _, name := range mandatory {
var value reflect.Value
if name == "APIURL" {
switch name {
case "APIURL":
value = reflect.ValueOf("https://jiralert.atlassian.net")
} else if name == "ReopenDuration" {
case "ReopenDuration":
value = reflect.ValueOf("30d")
} else {
default:
value = reflect.ValueOf(name)
}

Expand All @@ -379,11 +375,12 @@ func newReceiverTestConfig(mandatory []string, optional []string) *receiverTestC

for _, name := range optional {
var value reflect.Value
if name == "AddGroupLabels" {
switch name {
case "AddGroupLabels":
value = reflect.ValueOf(true)
} else if name == "AutoResolve" {
case "AutoResolve":
value = reflect.ValueOf(&AutoResolve{State: "Done"})
} else {
default:
value = reflect.ValueOf(name)
}

Expand Down Expand Up @@ -418,8 +415,10 @@ func removeFromStrSlice(strSlice []string, element string) []string {
// Returns mandatory receiver fields to be used creating test config structs.
// It does not include PAT auth, those tests will be created separately.
func mandatoryReceiverFields() []string {
return []string{"Name", "APIURL", "User", "Password", "Project",
"IssueType", "Summary", "ReopenState", "ReopenDuration"}
return []string{
"Name", "APIURL", "User", "Password", "Project",
"IssueType", "Summary", "ReopenState", "ReopenDuration",
}
}

func TestAutoResolveConfigReceiver(t *testing.T) {
Expand All @@ -439,7 +438,6 @@ func TestAutoResolveConfigReceiver(t *testing.T) {
}

configErrorTestRunner(t, config, "bad config in receiver \"test\", 'auto_resolve' was defined with empty 'state' field")

}

func TestAutoResolveConfigDefault(t *testing.T) {
Expand All @@ -457,5 +455,4 @@ func TestAutoResolveConfigDefault(t *testing.T) {
}

configErrorTestRunner(t, config, "bad config in defaults section: state cannot be empty")

}
6 changes: 2 additions & 4 deletions pkg/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,11 @@ func toGroupTicketLabel(groupLabels alertmanager.KV, hashJiraLabel bool) string
// old default behavior
buf := bytes.NewBufferString("ALERT{")
for _, p := range groupLabels.SortedPairs() {
buf.WriteString(p.Name)
buf.WriteString(fmt.Sprintf("=%q,", p.Value))
fmt.Fprintf(buf, "%s=%q,", p.Name, p.Value)
}
buf.Truncate(buf.Len() - 1)
buf.WriteString("}")
return strings.Replace(buf.String(), " ", "", -1)
return strings.ReplaceAll(buf.String(), " ", "")
}

func (r *Receiver) search(project, issueLabel string) (*jira.Issue, bool, error) {
Expand Down Expand Up @@ -406,5 +405,4 @@ func (r *Receiver) doTransition(issueKey string, transitionState string) (bool,
}
}
return false, errors.Errorf("JIRA state %q does not exist or no transition possible for %s", r.conf.ReopenState, issueKey)

}

0 comments on commit 2c908db

Please sign in to comment.