diff --git a/pkg/config/config.go b/pkg/config/config.go index 448a1d2..7fc63c4 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -47,36 +47,6 @@ func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error { return unmarshal((*plain)(s)) } -// NullBool will be used for boolean configuration values that should not -// default to false when undefined and the default setting is true. -type NullBool struct { - Bool bool - Valid bool // Valid is true if Bool is defined -} - -// MarshalYAML implements the yaml.Marshaler interface. -func (nb NullBool) MarshalYAML() (interface{}, error) { - if nb.Valid { - return nb.Bool, nil - } - return false, nil -} - -// UnmarshalYAML implements the yaml.Unmarshaler interface -func (nb *NullBool) UnmarshalYAML(unmarshal func(interface{}) error) error { - var b bool - - err := unmarshal(&b) - if err != nil { - return err - } - - nb.Bool = b - nb.Valid = true - - return nil -} - // Load parses the YAML input into a Config. func Load(s string) (*Config, error) { cfg := &Config{} @@ -178,10 +148,10 @@ type ReceiverConfig struct { StaticLabels []string `yaml:"static_labels" json:"static_labels"` // Label copy settings - AddGroupLabels bool `yaml:"add_group_labels" json:"add_group_labels"` + AddGroupLabels *bool `yaml:"add_group_labels" json:"add_group_labels"` // Flag to enable updates in comments. - UpdateInComment NullBool `yaml:"update_in_comment" json:"update_in_comment"` + UpdateInComment *bool `yaml:"update_in_comment" json:"update_in_comment"` // Flag to auto-resolve opened issue when the alert is resolved. AutoResolve *AutoResolve `yaml:"auto_resolve" json:"auto_resolve"` @@ -348,7 +318,10 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { if len(c.Defaults.OtherProjects) > 0 { rc.OtherProjects = append(rc.OtherProjects, c.Defaults.OtherProjects...) } - if !rc.UpdateInComment.Valid && c.Defaults.UpdateInComment.Valid { + if rc.AddGroupLabels == nil { + rc.AddGroupLabels = c.Defaults.AddGroupLabels + } + if rc.UpdateInComment == nil { rc.UpdateInComment = c.Defaults.UpdateInComment } } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 11448cb..e0bd0c9 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -129,8 +129,8 @@ type receiverTestConfig struct { Priority string `yaml:"priority,omitempty"` Description string `yaml:"description,omitempty"` WontFixResolution string `yaml:"wont_fix_resolution,omitempty"` - AddGroupLabels bool `yaml:"add_group_labels,omitempty"` - UpdateInComment NullBool `yaml:"update_in_comment,omitempty"` + AddGroupLabels *bool `yaml:"add_group_labels,omitempty"` + UpdateInComment *bool `yaml:"update_in_comment,omitempty"` StaticLabels []string `yaml:"static_labels" json:"static_labels"` AutoResolve *AutoResolve `yaml:"auto_resolve" json:"auto_resolve"` @@ -316,6 +316,10 @@ func TestReceiverOverrides(t *testing.T) { fifteenHoursToDuration, err := ParseDuration("15h") autoResolve := AutoResolve{State: "Done"} require.NoError(t, err) + addGroupLabelsTrueVal := true + addGroupLabelsFalseVal := false + updateInCommentTrueVal := true + updateInCommentFalseVal := false // We'll override one key at a time and check the value in the receiver. for _, test := range []struct { @@ -332,9 +336,10 @@ func TestReceiverOverrides(t *testing.T) { {"Priority", "Critical", "Critical"}, {"Description", "A nice description", "A nice description"}, {"WontFixResolution", "Won't Fix", "Won't Fix"}, - {"AddGroupLabels", false, false}, - {"UpdateInComment", NullBool{Bool: false, Valid: true}, NullBool{Bool: false, Valid: true}}, - {"UpdateInComment", NullBool{Bool: true, Valid: true}, NullBool{Bool: true, Valid: true}}, + {"AddGroupLabels", &addGroupLabelsFalseVal, &addGroupLabelsFalseVal}, + {"AddGroupLabels", &addGroupLabelsTrueVal, &addGroupLabelsTrueVal}, + {"UpdateInComment", &updateInCommentFalseVal, &updateInCommentFalseVal}, + {"UpdateInComment", &updateInCommentTrueVal, &updateInCommentTrueVal}, {"AutoResolve", &AutoResolve{State: "Done"}, &autoResolve}, {"StaticLabels", []string{"somelabel"}, []string{"somelabel"}}, } { @@ -372,6 +377,8 @@ func TestReceiverOverrides(t *testing.T) { // Creates a receiverTestConfig struct with default values. func newReceiverTestConfig(mandatory []string, optional []string) *receiverTestConfig { r := receiverTestConfig{} + addGroupLabelsDefaultVal := true + updateInCommentDefaultVal := true for _, name := range mandatory { var value reflect.Value @@ -389,9 +396,9 @@ func newReceiverTestConfig(mandatory []string, optional []string) *receiverTestC for _, name := range optional { var value reflect.Value if name == "AddGroupLabels" { - value = reflect.ValueOf(true) + value = reflect.ValueOf(&addGroupLabelsDefaultVal) } else if name == "UpdateInComment" { - value = reflect.ValueOf(NullBool{Bool: false, Valid: false}) + value = reflect.ValueOf(&updateInCommentDefaultVal) } else if name == "AutoResolve" { value = reflect.ValueOf(&AutoResolve{State: "Done"}) } else if name == "StaticLabels" { diff --git a/pkg/notify/notify.go b/pkg/notify/notify.go index 85157de..e4b96a2 100644 --- a/pkg/notify/notify.go +++ b/pkg/notify/notify.go @@ -104,7 +104,7 @@ func (r *Receiver) Notify(data *alertmanager.Data, hashJiraLabel bool, updateSum } } - if r.conf.UpdateInComment.Valid && r.conf.UpdateInComment.Bool { + if r.conf.UpdateInComment != nil && *r.conf.UpdateInComment { numComments := 0 if issue.Fields.Comments != nil { numComments = len(issue.Fields.Comments.Comments) @@ -215,7 +215,7 @@ func (r *Receiver) Notify(data *alertmanager.Data, hashJiraLabel bool, updateSum } } - if r.conf.AddGroupLabels { + if r.conf.AddGroupLabels != nil && *r.conf.AddGroupLabels { for k, v := range data.GroupLabels { issue.Fields.Labels = append(issue.Fields.Labels, fmt.Sprintf("%s=%.200q", k, v)) } diff --git a/pkg/notify/notify_test.go b/pkg/notify/notify_test.go index d434c95..49d686f 100644 --- a/pkg/notify/notify_test.go +++ b/pkg/notify/notify_test.go @@ -185,6 +185,7 @@ func testReceiverConfig2() *config.ReceiverConfig { func testReceiverConfigAddComments() *config.ReceiverConfig { reopen := config.Duration(1 * time.Hour) + updateInCommentValue := true return &config.ReceiverConfig{ Project: "abc", Summary: `[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .GroupLabels.SortedPairs.Values | join " " }} {{ if gt (len .CommonLabels) (len .GroupLabels) }}({{ with .CommonLabels.Remove .GroupLabels.Names }}{{ .Values | join " " }}{{ end }}){{ end }}`, @@ -192,7 +193,7 @@ func testReceiverConfigAddComments() *config.ReceiverConfig { ReopenState: "reopened", Description: `{{ .Alerts.Firing | len }}`, WontFixResolution: "won't-fix", - UpdateInComment: config.NullBool{Valid: true, Bool: true}, + UpdateInComment: &updateInCommentValue, } }