Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only Update Slack User Group via Slack API when Membership changes #360

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions adapters/slack/usergroup/usergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ func (u *UserGroup) Add(ctx context.Context, emails []string) error {
// The updatedUserGroup is existing users + new users.
updatedUserGroup := make([]string, 0, len(u.cache)+len(emails))

isUserGroupUpdated := false

// Prefill the updatedUserGroup with everyone currently in the group.
for _, id := range u.cache {
updatedUserGroup = append(updatedUserGroup, id)
Expand All @@ -183,25 +185,35 @@ func (u *UserGroup) Add(ctx context.Context, emails []string) error {
return fmt.Errorf("slack.usergroup.add.getuserbyemail(%s) -> %w", email, err)
}

// Add the new email user IDs to the list.
updatedUserGroup = append(updatedUserGroup, user.ID)
_, ok := u.cache[email]
if !ok {
// Add the new email user IDs to the list.
updatedUserGroup = append(updatedUserGroup, user.ID)

// Add the new email user ID to the cache
u.cache[email] = user.ID

// Add the new email user ID to the cache
u.cache[email] = user.ID
// Set flag so we know to update Slack API
isUserGroupUpdated = true
}

// Calls to GetUserByEmail are heavily rate limited, so sleep to avoid this.
time.Sleep(2 * time.Second) //nolint:gomnd
}

// Add the members to the Slack UserGroup.
joinedSlackIds := strings.Join(updatedUserGroup, ",")
if isUserGroupUpdated {
// Add the members to the Slack UserGroup.
joinedSlackIds := strings.Join(updatedUserGroup, ",")

_, err := u.client.UpdateUserGroupMembersContext(ctx, u.userGroupID, joinedSlackIds)
if err != nil {
return fmt.Errorf("slack.usergroup.add.updateusergroupmembers(%s) -> %w", u.userGroupID, err)
}
_, err := u.client.UpdateUserGroupMembersContext(ctx, u.userGroupID, joinedSlackIds)
if err != nil {
return fmt.Errorf("slack.usergroup.add.updateusergroupmembers(%s) -> %w", u.userGroupID, err)
}

u.Logger.Println("Finished adding accounts successfully")
u.Logger.Println("Finished adding accounts successfully")
} else {
u.Logger.Println("No change to UserGroup")
}

return nil
}
Expand Down
23 changes: 23 additions & 0 deletions adapters/slack/usergroup/usergroup_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,29 @@ func TestUserGroup_Add(t *testing.T) {
assert.Contains(t, adapter.cache, "fizz@email")
assert.Contains(t, adapter.cache, "buzz@email")
})

t.Run("Doesn't call API if no changes", func(t *testing.T) {
t.Parallel()

slackClient := newMockISlackUserGroup(t)

adapter := &UserGroup{
client: slackClient,
userGroupID: "test",
Logger: log.New(os.Stdout, "", log.LstdFlags),
}

slackClient.EXPECT().GetUserByEmailContext(ctx, "foo@email").Return(&slack.User{ID: "foo"}, nil)
slackClient.EXPECT().GetUserByEmailContext(ctx, "bar@email").Return(&slack.User{ID: "bar"}, nil)

adapter.cache = map[string]string{"foo@email": "foo", "bar@email": "bar"}
err := adapter.Add(ctx, []string{"foo@email", "bar@email"})

require.NoError(t, err)
assert.Contains(t, adapter.cache, "foo@email")
assert.Contains(t, adapter.cache, "bar@email")
slackClient.AssertNotCalled(t, "UpdateUserGroupMembersContext")
})
}

func TestUserGroup_Remove(t *testing.T) {
Expand Down
Loading