Skip to content

Commit

Permalink
refactor: use pointer for workspace in slack cache item
Browse files Browse the repository at this point in the history
  • Loading branch information
bsushmith committed Sep 12, 2023
1 parent 2a1113f commit a54a6a3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
16 changes: 8 additions & 8 deletions plugins/notifiers/slack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -98,7 +98,7 @@ func (n *Notifier) Notify(items []domain.Notification) []error {
// cache
n.slackIDCache[item.User] = &slackIDCacheItem{
SlackID: slackID,
Workspace: &ws,
Workspace: ws,
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
35 changes: 35 additions & 0 deletions plugins/notifiers/slack/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := "[email protected]"
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 := "[email protected]"
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 := "[email protected]"
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))
}

0 comments on commit a54a6a3

Please sign in to comment.