From a54a6a346fe49f3508f00afed89f26d80c44a1a8 Mon Sep 17 00:00:00 2001 From: sushmith Date: Tue, 12 Sep 2023 11:33:34 +0530 Subject: [PATCH] refactor: use pointer for workspace in slack cache item --- plugins/notifiers/slack/client.go | 16 ++++++------ plugins/notifiers/slack/client_test.go | 35 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/plugins/notifiers/slack/client.go b/plugins/notifiers/slack/client.go index c49ce94ed..28adf431d 100644 --- a/plugins/notifiers/slack/client.go +++ b/plugins/notifiers/slack/client.go @@ -84,12 +84,12 @@ func (n *Notifier) Notify(items []domain.Notification) []error { slackID = n.slackIDCache[item.User].SlackID ws = n.slackIDCache[item.User].Workspace } else { - ws, err := n.getSlackWorkspaceForUser(item.User) + ws, err := n.GetSlackWorkspaceForUser(item.User) if err != nil { errs = append(errs, fmt.Errorf("%v | %w", labelSlice, err)) continue } - slackID, err = n.findSlackIDByEmail(item.User, ws) + slackID, err = n.findSlackIDByEmail(item.User, *ws) if err != nil { errs = append(errs, fmt.Errorf("%v | %w", labelSlice, err)) continue @@ -98,7 +98,7 @@ func (n *Notifier) Notify(items []domain.Notification) []error { // cache n.slackIDCache[item.User] = &slackIDCacheItem{ SlackID: slackID, - Workspace: &ws, + Workspace: ws, } } @@ -143,8 +143,8 @@ func (n *Notifier) sendMessage(workspace SlackWorkspace, channel, messageBlock s return err } -func (n *Notifier) getSlackWorkspaceForUser(email string) (SlackWorkspace, error) { - var ws SlackWorkspace +func (n *Notifier) GetSlackWorkspaceForUser(email string) (*SlackWorkspace, error) { + var ws *SlackWorkspace for _, workspace := range n.workspaces { v, err := evaluator.Expression(workspace.Criteria).EvaluateWithVars(map[string]interface{}{ "email": email, @@ -157,13 +157,13 @@ func (n *Notifier) getSlackWorkspaceForUser(email string) (SlackWorkspace, error if match, ok := v.(bool); !ok { return ws, errors.New("notifier expression did not evaluate to a boolean") } else if match { - ws = workspace + ws = &workspace break } } - if ws.WorkspaceName == "" { - return ws, errors.New(fmt.Sprintf("no workspace found for user %s", email)) + if ws == nil { + return ws, errors.New(fmt.Sprintf("no slack workspace found for user: %s", email)) } return ws, nil diff --git a/plugins/notifiers/slack/client_test.go b/plugins/notifiers/slack/client_test.go index 77cfd181a..bf2197439 100644 --- a/plugins/notifiers/slack/client_test.go +++ b/plugins/notifiers/slack/client_test.go @@ -125,6 +125,41 @@ func (s *ClientTestSuite) TestParseMessage() { }) } +func (s *ClientTestSuite) TestGetSlackWorkspaceForUser() { + s.setup() + s.Run("should return slack workspace 1 for user", func() { + userEmail := "example-user@abc.com" + expectedWs := &slack.SlackWorkspace{ + WorkspaceName: "ws-1", + AccessToken: "XXXXX-TOKEN-1-XXXXX", + Criteria: "$email contains '@abc'", + } + actualWs, err := s.notifier.GetSlackWorkspaceForUser(userEmail) + s.Nil(err) + s.Equal(expectedWs, actualWs) + }) + + s.Run("should return slack workspace 2 for user", func() { + userEmail := "example-user@xyz.com" + expectedWs := &slack.SlackWorkspace{ + WorkspaceName: "ws-2", + AccessToken: "XXXXX-TOKEN-2-XXXXX", + Criteria: "$email contains '@xyz'", + } + actualWs, err := s.notifier.GetSlackWorkspaceForUser(userEmail) + s.Nil(err) + s.Equal(expectedWs, actualWs) + }) + + s.Run("should return error if slack workspace not found for user", func() { + userEmail := "example-user@def.com" + expectedErr := fmt.Errorf("no slack workspace found for user: %s", userEmail) + _, actualErr := s.notifier.GetSlackWorkspaceForUser(userEmail) + s.Equal(expectedErr, actualErr) + }) + +} + func TestClient(t *testing.T) { suite.Run(t, new(ClientTestSuite)) }