Skip to content

Commit

Permalink
feat: make slack workspace config to be map[string]interface{}
Browse files Browse the repository at this point in the history
  • Loading branch information
bsushmith committed Sep 12, 2023
1 parent 590a181 commit 60c02db
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
29 changes: 22 additions & 7 deletions plugins/notifiers/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package notifiers

import (
"errors"
"fmt"
"github.com/mitchellh/mapstructure"
"net/http"
"time"

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
}

Expand Down
34 changes: 19 additions & 15 deletions plugins/notifiers/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},
},
Expand Down Expand Up @@ -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'",
},
},
},
},
Expand Down
4 changes: 4 additions & 0 deletions plugins/notifiers/slack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 60c02db

Please sign in to comment.