Skip to content

Commit

Permalink
feat(notifiers): add backward compatibility for slack notifier changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bsushmith committed Sep 11, 2023
1 parent 72ceb18 commit 05322e0
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 5 deletions.
45 changes: 41 additions & 4 deletions plugins/notifiers/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,58 @@ type Config struct {
Provider string `mapstructure:"provider" validate:"omitempty,oneof=slack"`

// slack
Workspaces []slack.SlackWorkspace `mapstructure:"workspaces" validate:"required_if=Provider slack,dive"`
AccessToken string `mapstructure:"access_token" validate:"required_without=Workspaces"`
Workspaces []slack.SlackWorkspace `mapstructure:"workspaces" validate:"required_without=AccessToken,dive"`

// custom messages
Messages domain.NotificationMessages
}

func NewClient(config *Config) (Client, error) {
if config.Provider == ProviderTypeSlack {
slackConfig := &slack.Config{
Workspaces: config.Workspaces,
Messages: config.Messages,

slackConfig, err := NewSlackConfig(config)
if err != nil {
return nil, err
}

httpClient := &http.Client{Timeout: 10 * time.Second}
return slack.NewNotifier(slackConfig, httpClient), nil
}

return nil, errors.New("invalid notifier provider type")
}

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 != "" && len(config.Workspaces) != 0 {
return nil, errors.New("slack access token and workspaces cannot be provided at the same time")
}

var slackConfig *slack.Config
if config.AccessToken != "" {
workspaces := []slack.SlackWorkspace{
{
WorkspaceName: "default",
AccessToken: config.AccessToken,
Criteria: "1==1",
},
}
slackConfig = &slack.Config{
Workspaces: workspaces,
Messages: config.Messages,
}
return slackConfig, nil
}

slackConfig = &slack.Config{
Workspaces: config.Workspaces,
Messages: config.Messages,
}

return slackConfig, nil
}
111 changes: 111 additions & 0 deletions plugins/notifiers/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package notifiers

import (
"github.com/goto/guardian/plugins/notifiers/slack"
"reflect"
"testing"
)

func TestNewSlackConfig(t *testing.T) {
type args struct {
config *Config
}
tests := []struct {
name string
args args
want *slack.Config
wantErr bool
}{
{
name: "should return error when no access token or workspaces are provided",
args: args{
config: &Config{
Provider: ProviderTypeSlack,
},
},
want: nil,
wantErr: true,
}, {
name: "should return error when both access token and workspaces are provided",
args: args{
config: &Config{
Provider: ProviderTypeSlack,
AccessToken: "foo",
Workspaces: []slack.SlackWorkspace{
{
WorkspaceName: "default",
AccessToken: "bar",
Criteria: "1==1",
},
},
},
},
want: nil,
wantErr: true,
}, {
name: "should return slack config when access token is provided",
args: args{
config: &Config{
Provider: ProviderTypeSlack,
AccessToken: "foo",
},
},
want: &slack.Config{
Workspaces: []slack.SlackWorkspace{
{
WorkspaceName: "default",
AccessToken: "foo",
Criteria: "1==1",
},
},
},
wantErr: false,
}, {
name: "should return slack config when workspaces are provided",
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'",
},
},
},
},
want: &slack.Config{
Workspaces: []slack.SlackWorkspace{
{
WorkspaceName: "A",
AccessToken: "foo",
Criteria: "$email contains '@abc'",
},
{
WorkspaceName: "B",
AccessToken: "bar",
Criteria: "$email contains '@xyz'",
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewSlackConfig(tt.args.config)
if (err != nil) != tt.wantErr {
t.Errorf("NewSlackConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewSlackConfig() got = %v, want %v", got, tt.want)
}
})
}
}
1 change: 0 additions & 1 deletion plugins/notifiers/slack/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func (s *ClientTestSuite) TestNotify() {
s.mockHttpClient.On("Do", mock.Anything).Return(resp, nil)
expectedErrs := []error{
fmt.Errorf("[appeal_id=test-appeal-id] | %w", errors.New("error finding slack id for email [email protected] in workspace: ws-1 - users_not_found")),
fmt.Errorf("[appeal_id=test-appeal-id] | error sending message to user:[email protected] in workspace: | %w", errors.New("EOF")),
}

notifications := []domain.Notification{
Expand Down

0 comments on commit 05322e0

Please sign in to comment.