diff --git a/cmd/jiralert/main.go b/cmd/jiralert/main.go index 2351203..54b2c3a 100644 --- a/cmd/jiralert/main.go +++ b/cmd/jiralert/main.go @@ -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 { @@ -133,7 +133,6 @@ func main() { return } requestTotal.WithLabelValues(conf.Name, "200").Inc() - }) http.HandleFunc("/", HomeHandlerFunc()) diff --git a/cmd/jiralert/telemetry.go b/cmd/jiralert/telemetry.go index 7eac82a..dfbf023 100644 --- a/cmd/jiralert/telemetry.go +++ b/cmd/jiralert/telemetry.go @@ -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() { diff --git a/pkg/config/config.go b/pkg/config/config.go index f72809c..2dbbdc9 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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 @@ -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) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 3bc7de7..4a23360 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -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") @@ -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. @@ -186,7 +183,6 @@ func TestRequiredReceiverConfigKeys(t *testing.T) { } configErrorTestRunner(t, config, test.errorMessage) } - } // Auth keys error scenarios. @@ -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: @@ -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) } @@ -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) } @@ -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) { @@ -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) { @@ -457,5 +455,4 @@ func TestAutoResolveConfigDefault(t *testing.T) { } configErrorTestRunner(t, config, "bad config in defaults section: state cannot be empty") - } diff --git a/pkg/notify/notify.go b/pkg/notify/notify.go index 0030157..00356b7 100644 --- a/pkg/notify/notify.go +++ b/pkg/notify/notify.go @@ -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) { @@ -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) - }