@@ -7,12 +7,13 @@ import (
7
7
8
8
sprig "github.com/go-task/slim-sprig"
9
9
"github.com/sirupsen/logrus"
10
- "k8s.io/kubectl/pkg/util/slice "
10
+ "golang.org/x/exp/slices "
11
11
12
12
"github.com/kubeshop/botkube/internal/source/kubernetes/commander"
13
13
"github.com/kubeshop/botkube/internal/source/kubernetes/config"
14
14
"github.com/kubeshop/botkube/internal/source/kubernetes/event"
15
15
"github.com/kubeshop/botkube/pkg/api"
16
+ multierrx "github.com/kubeshop/botkube/pkg/multierror"
16
17
)
17
18
18
19
var emojiForLevel = map [config.Level ]string {
@@ -53,12 +54,12 @@ func (m *MessageBuilder) FromEvent(event event.Event, actions []config.ExtraButt
53
54
54
55
cmdSection , err := m .getCommandSelectIfShould (event )
55
56
if err != nil {
56
- return api. Message {}, err
57
+ m . log . Errorf ( "Failed to get commands buttons assigned to %q event. Those buttons will be omitted. Issues: \n %s" , event . Type . String (), err )
57
58
}
58
59
59
- btns , err := m .getExternalActions (actions , event )
60
+ btns , err := m .getExtraButtonsAssignedToEvent (actions , event )
60
61
if err != nil {
61
- return api. Message {}, err
62
+ m . log . Errorf ( "Failed to convert extra buttons assigned to %q event. Those buttons will be omitted. Issues: \n %s" , event . Type . String (), err )
62
63
}
63
64
if cmdSection != nil || len (btns ) > 0 {
64
65
msg .Sections = append (msg .Sections , api.Section {
@@ -70,24 +71,33 @@ func (m *MessageBuilder) FromEvent(event event.Event, actions []config.ExtraButt
70
71
return msg , nil
71
72
}
72
73
73
- func (m * MessageBuilder ) getExternalActions (actions []config.ExtraButtons , e event.Event ) (api.Buttons , error ) {
74
+ func (m * MessageBuilder ) getExtraButtonsAssignedToEvent (actions []config.ExtraButtons , e event.Event ) (api.Buttons , error ) {
74
75
var actBtns api.Buttons
75
- for _ , act := range actions {
76
+ issues := multierrx .New ()
77
+ for idx , act := range actions {
76
78
if ! act .Enabled {
77
79
continue
78
80
}
79
- if ! slice .ContainsString (act .Trigger .Type , e .Type .String (), nil ) {
81
+
82
+ err := act .NormalizeAndValidate ()
83
+ if err != nil {
84
+ issues = multierrx .Append (issues , fmt .Errorf ("invalid extraButtons[%d]: %s" , idx , err ))
85
+ continue
86
+ }
87
+
88
+ if ! slices .Contains (act .Trigger .Type , e .Type ) {
80
89
continue
81
90
}
82
91
83
92
btn , err := m .renderActionButton (act , e )
84
93
if err != nil {
85
- return nil , err
94
+ issues = multierrx .Append (issues , fmt .Errorf ("invalid extraButtons[%d].commandTpl: %s" , idx , err ))
95
+ continue
86
96
}
87
97
actBtns = append (actBtns , btn )
88
98
}
89
99
90
- return actBtns , nil
100
+ return actBtns , issues . ErrorOrNil ()
91
101
}
92
102
93
103
func ptrSection (s * api.Selects ) api.Selects {
@@ -175,7 +185,7 @@ func (m *MessageBuilder) appendBulletListIfNotEmpty(bulletLists api.BulletLists,
175
185
}
176
186
177
187
func (m * MessageBuilder ) renderActionButton (act config.ExtraButtons , e event.Event ) (api.Button , error ) {
178
- tmpl , err := template .New ("example" ).Funcs (sprig .FuncMap ()).Parse (act .Button .CommandTpl )
188
+ tmpl , err := template .New (act . Button . DisplayName ).Funcs (sprig .FuncMap ()).Parse (act .Button .CommandTpl )
179
189
if err != nil {
180
190
return api.Button {}, err
181
191
}
0 commit comments