Skip to content

Commit

Permalink
use bool* instead of NullBool for both UpdateInComment and AddGroupLa…
Browse files Browse the repository at this point in the history
…bels

Signed-off-by: Jason Wells <[email protected]>
  • Loading branch information
twotired committed Feb 4, 2024
1 parent 5de4ce3 commit 28c7421
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 43 deletions.
39 changes: 6 additions & 33 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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
}
}
Expand Down
21 changes: 14 additions & 7 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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 {
Expand All @@ -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"}},
} {
Expand Down Expand Up @@ -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
Expand All @@ -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" {
Expand Down
4 changes: 2 additions & 2 deletions pkg/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/notify/notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,15 @@ 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 }}`,
ReopenDuration: &reopen,
ReopenState: "reopened",
Description: `{{ .Alerts.Firing | len }}`,
WontFixResolution: "won't-fix",
UpdateInComment: config.NullBool{Valid: true, Bool: true},
UpdateInComment: &updateInCommentValue,
}
}

Expand Down

0 comments on commit 28c7421

Please sign in to comment.