diff --git a/plugins/notifiers/client.go b/plugins/notifiers/client.go index 683c0b02c..eb053e2cd 100644 --- a/plugins/notifiers/client.go +++ b/plugins/notifiers/client.go @@ -2,6 +2,8 @@ package notifiers import ( "errors" + "fmt" + "github.com/mitchellh/mapstructure" "net/http" "time" @@ -17,12 +19,20 @@ const ( ProviderTypeSlack = "slack" ) +// slackConfig is a map of workspace name to config +type slackConfig map[string]interface{} + +func (c slackConfig) Decode(v interface{}) error { + return mapstructure.Decode(c, v) +} + type Config struct { Provider string `mapstructure:"provider" validate:"omitempty,oneof=slack"` // slack - AccessToken string `mapstructure:"access_token" validate:"required_without=Workspaces"` - Workspaces []slack.SlackWorkspace `mapstructure:"workspaces" validate:"required_without=AccessToken,dive"` + AccessToken string `mapstructure:"access_token" validate:"required_without=slackConfig"` + //Workspaces []slack.SlackWorkspace `mapstructure:"workspaces" validate:"required_without=AccessToken,dive"` + slackConfig slackConfig `mapstructure:"workspace_config" validate:"omitempty,dive"` // custom messages Messages domain.NotificationMessages @@ -46,11 +56,11 @@ func NewClient(config *Config) (Client, error) { func NewSlackConfig(config *Config) (*slack.Config, error) { // validation - if config.AccessToken == "" && len(config.Workspaces) == 0 { - return nil, errors.New("slack access token or workspaces must be provided") + if config.AccessToken == "" && config.slackConfig == nil { + return nil, errors.New("slack access token or workSpaceConfig must be provided") } - if config.AccessToken != "" && len(config.Workspaces) != 0 { - return nil, errors.New("slack access token and workspaces cannot be provided at the same time") + if config.AccessToken != "" && config.slackConfig != nil { + return nil, errors.New("slack access token and workSpaceConfig cannot be provided at the same time") } var slackConfig *slack.Config @@ -69,8 +79,13 @@ func NewSlackConfig(config *Config) (*slack.Config, error) { return slackConfig, nil } + var workSpaceConfig slack.WorkSpaceConfig + if err := config.slackConfig.Decode(&workSpaceConfig); err != nil { + return nil, fmt.Errorf("invalid slack workspace config: %w", err) + } + slackConfig = &slack.Config{ - Workspaces: config.Workspaces, + Workspaces: workSpaceConfig.Workspaces, Messages: config.Messages, } diff --git a/plugins/notifiers/client_test.go b/plugins/notifiers/client_test.go index c30cf12bd..7f13f9907 100644 --- a/plugins/notifiers/client_test.go +++ b/plugins/notifiers/client_test.go @@ -31,11 +31,13 @@ func TestNewSlackConfig(t *testing.T) { config: &Config{ Provider: ProviderTypeSlack, AccessToken: "foo", - Workspaces: []slack.SlackWorkspace{ - { - WorkspaceName: "default", - AccessToken: "bar", - Criteria: "1==1", + slackConfig: slackConfig{ + "workspaces": []slack.SlackWorkspace{ + { + WorkspaceName: "default", + AccessToken: "bar", + Criteria: "1==1", + }, }, }, }, @@ -65,16 +67,18 @@ func TestNewSlackConfig(t *testing.T) { args: args{ config: &Config{ Provider: ProviderTypeSlack, - Workspaces: []slack.SlackWorkspace{ - { - WorkspaceName: "A", - AccessToken: "foo", - Criteria: "$email contains '@abc'", - }, - { - WorkspaceName: "B", - AccessToken: "bar", - Criteria: "$email contains '@xyz'", + slackConfig: slackConfig{ + "workspaces": []slack.SlackWorkspace{ + { + WorkspaceName: "A", + AccessToken: "foo", + Criteria: "$email contains '@abc'", + }, + { + WorkspaceName: "B", + AccessToken: "bar", + Criteria: "$email contains '@xyz'", + }, }, }, }, diff --git a/plugins/notifiers/slack/client.go b/plugins/notifiers/slack/client.go index 3a11c795e..60a8f2c07 100644 --- a/plugins/notifiers/slack/client.go +++ b/plugins/notifiers/slack/client.go @@ -34,6 +34,10 @@ type userResponse struct { Error string `json:"error"` } +type WorkSpaceConfig struct { + Workspaces []SlackWorkspace `mapstructure:"workspaces"` +} + type SlackWorkspace struct { WorkspaceName string `mapstructure:"workspace" validate:"required"` AccessToken string `mapstructure:"access_token" validate:"required"`