diff --git a/observability-lib/api/notification-policy.go b/observability-lib/api/notification-policy.go index 61c24ad97..5792c1a8c 100644 --- a/observability-lib/api/notification-policy.go +++ b/observability-lib/api/notification-policy.go @@ -8,11 +8,32 @@ import ( "github.com/grafana/grafana-foundation-sdk/go/alerting" ) +func objectMatchersEqual(a alerting.ObjectMatchers, b alerting.ObjectMatchers) bool { + if len(a) != len(b) { + return false + } + + for i := range a { + foundMatch := false + for j := range b { + if reflect.DeepEqual(a[i], b[j]) { + foundMatch = true + break + } + } + if !foundMatch { + return false + } + } + + return true +} + func policyExist(parent alerting.NotificationPolicy, newNotificationPolicy alerting.NotificationPolicy) bool { for _, notificationPolicy := range parent.Routes { matchersEqual := false if notificationPolicy.ObjectMatchers != nil { - matchersEqual = reflect.DeepEqual(notificationPolicy.ObjectMatchers, newNotificationPolicy.ObjectMatchers) + matchersEqual = objectMatchersEqual(*notificationPolicy.ObjectMatchers, *newNotificationPolicy.ObjectMatchers) } receiversEqual := reflect.DeepEqual(notificationPolicy.Receiver, newNotificationPolicy.Receiver) if matchersEqual && receiversEqual { diff --git a/observability-lib/api/notification-policy_test.go b/observability-lib/api/notification-policy_test.go new file mode 100644 index 000000000..16ead9063 --- /dev/null +++ b/observability-lib/api/notification-policy_test.go @@ -0,0 +1,46 @@ +package api + +import ( + "testing" + + "github.com/grafana/grafana-foundation-sdk/go/alerting" + "github.com/stretchr/testify/require" +) + +func TestObjectMatchersEqual(t *testing.T) { + t.Run("returns true if the two object matchers are equal", func(t *testing.T) { + a := alerting.ObjectMatchers{{"team", "=", "chainlink"}} + b := alerting.ObjectMatchers{{"team", "=", "chainlink"}} + + result := objectMatchersEqual(a, b) + require.True(t, result) + }) + + t.Run("returns true if the two object matchers with multiple matches are equal", func(t *testing.T) { + a := alerting.ObjectMatchers{ + {"team", "=", "chainlink"}, + {"severity", "=", "critical"}, + } + b := alerting.ObjectMatchers{ + {"severity", "=", "critical"}, + {"team", "=", "chainlink"}, + } + + result := objectMatchersEqual(a, b) + require.True(t, result) + }) + + t.Run("returns false if the two object matchers with multiple matches are different", func(t *testing.T) { + a := alerting.ObjectMatchers{ + {"team", "=", "chainlink"}, + {"severity", "=", "critical"}, + } + b := alerting.ObjectMatchers{ + {"severity", "=", "warning"}, + {"team", "=", "chainlink"}, + } + + result := objectMatchersEqual(a, b) + require.False(t, result) + }) +}