Skip to content

Commit

Permalink
fix(observability-lib): notification policy matchers check
Browse files Browse the repository at this point in the history
  • Loading branch information
Atrax1 committed Nov 5, 2024
1 parent 12a6266 commit 6afb436
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
23 changes: 22 additions & 1 deletion observability-lib/api/notification-policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
46 changes: 46 additions & 0 deletions observability-lib/api/notification-policy_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}

0 comments on commit 6afb436

Please sign in to comment.