github.com/kubeshop/testkube@v1.17.23/pkg/slack/config.go (about) 1 package slack 2 3 import ( 4 "github.com/kubeshop/testkube/pkg/api/v1/testkube" 5 ) 6 7 type NotificationsConfig struct { 8 ChannelID string `json:"channelID,omitempty"` 9 Selector map[string]string `json:"selector,omitempty"` 10 TestNames []string `json:"testName,omitempty"` 11 TestSuiteNames []string `json:"testSuiteName,omitempty"` 12 Events []testkube.EventType `json:"events,omitempty"` 13 } 14 15 type NotificationsConfigSet struct { 16 ChannelID string 17 Selector map[string]string 18 TestNames *set[string] 19 TestSuiteNames *set[string] 20 Events *set[testkube.EventType] 21 } 22 23 type Config struct { 24 NotificationsConfigSet []NotificationsConfigSet 25 hasChannelsDefined bool 26 } 27 28 func NewConfigSet(config NotificationsConfig) NotificationsConfigSet { 29 return NotificationsConfigSet{ 30 ChannelID: config.ChannelID, 31 Selector: config.Selector, 32 TestNames: NewSetFromArray(config.TestNames), 33 TestSuiteNames: NewSetFromArray(config.TestSuiteNames), 34 Events: NewSetFromArray(config.Events), 35 } 36 } 37 38 func NewConfig(config []NotificationsConfig) *Config { 39 cf := Config{} 40 cf.NotificationsConfigSet = make([]NotificationsConfigSet, len(config)) 41 for i, c := range config { 42 if c.ChannelID != "" { 43 cf.hasChannelsDefined = true 44 } 45 cf.NotificationsConfigSet[i] = NewConfigSet(c) 46 } 47 return &cf 48 } 49 50 func (c *Config) HasChannelsDefined() bool { 51 return c.hasChannelsDefined 52 } 53 54 func (c *Config) NeedsSending(event *testkube.Event) ([]string, bool) { 55 channels := []string{} 56 var labels map[string]string 57 if event.TestExecution != nil { 58 labels = event.TestExecution.Labels 59 } 60 if event.TestSuiteExecution != nil { 61 labels = event.TestSuiteExecution.Labels 62 } 63 64 testName := "" 65 if event.TestExecution != nil { 66 testName = event.TestExecution.TestName 67 } 68 testSuiteName := "" 69 if event.TestSuiteExecution != nil { 70 testSuiteName = event.TestSuiteExecution.TestSuite.Name 71 } 72 73 for _, config := range c.NotificationsConfigSet { 74 75 hasMatch := false 76 if config.Events != nil && 77 config.Events.Contains(event.Type()) && 78 config.TestSuiteNames.IsEmpty() && 79 config.TestNames.IsEmpty() && 80 len(config.Selector) == 0 { 81 hasMatch = true 82 } 83 84 if config.Selector != nil { 85 for k, v := range config.Selector { 86 if labels != nil && labels[k] == v { 87 hasMatch = true 88 } 89 } 90 } 91 if config.TestNames != nil { 92 if config.TestNames.Contains(testName) { 93 hasMatch = true 94 } 95 } 96 if config.TestSuiteNames != nil { 97 if config.TestSuiteNames.Contains(testSuiteName) { 98 hasMatch = true 99 } 100 } 101 if config.Events != nil && config.Events.Contains(event.Type()) && hasMatch { 102 if config.ChannelID != "" { 103 channels = append(channels, config.ChannelID) 104 } else { 105 return channels, true 106 } 107 108 } 109 } 110 if len(channels) > 0 { 111 return channels, true 112 } 113 return channels, false 114 }