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
Fixes updateUserGroup cannot clear the description and the default channels #1082
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package slack | |
import ( | ||
"context" | ||
"net/url" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
|
@@ -183,32 +184,77 @@ func (api *Client) GetUserGroupsContext(ctx context.Context, options ...GetUserG | |
return response.UserGroups, nil | ||
} | ||
|
||
// UpdateUserGroupsOption options for the UpdateUserGroup method call. | ||
type UpdateUserGroupsOption func(*UpdateUserGroupsParams) | ||
|
||
// UpdateUserGroupsOptionName change the name of the User Group (default: empty, so it's no-op) | ||
func UpdateUserGroupsOptionName(name string) UpdateUserGroupsOption { | ||
return func(params *UpdateUserGroupsParams) { | ||
params.Name = name | ||
} | ||
} | ||
|
||
// UpdateUserGroupsOptionHandle change the handle of the User Group (default: empty, so it's no-op) | ||
func UpdateUserGroupsOptionHandle(handle string) UpdateUserGroupsOption { | ||
return func(params *UpdateUserGroupsParams) { | ||
params.Handle = handle | ||
} | ||
} | ||
|
||
// UpdateUserGroupsOptionDescription change the description of the User Group. (default: nil, so it's no-op) | ||
func UpdateUserGroupsOptionDescription(description *string) UpdateUserGroupsOption { | ||
return func(params *UpdateUserGroupsParams) { | ||
params.Description = description | ||
} | ||
} | ||
|
||
// UpdateUserGroupsOptionChannels change the default channels of the User Group. (default: nil, so it's no-op) | ||
func UpdateUserGroupsOptionChannels(channels *[]string) UpdateUserGroupsOption { | ||
return func(params *UpdateUserGroupsParams) { | ||
params.Channels = channels | ||
} | ||
} | ||
|
||
// UpdateUserGroupsParams contains arguments for UpdateUserGroup method call | ||
type UpdateUserGroupsParams struct { | ||
Name string | ||
Handle string | ||
Description *string | ||
Channels *[]string | ||
} | ||
|
||
// UpdateUserGroup will update an existing user group | ||
func (api *Client) UpdateUserGroup(userGroup UserGroup) (UserGroup, error) { | ||
return api.UpdateUserGroupContext(context.Background(), userGroup) | ||
func (api *Client) UpdateUserGroup(userGroupID string, options ...UpdateUserGroupsOption) (UserGroup, error) { | ||
return api.UpdateUserGroupContext(context.Background(), userGroupID, options...) | ||
} | ||
|
||
// UpdateUserGroupContext will update an existing user group with a custom context | ||
func (api *Client) UpdateUserGroupContext(ctx context.Context, userGroup UserGroup) (UserGroup, error) { | ||
func (api *Client) UpdateUserGroupContext(ctx context.Context, userGroupID string, options ...UpdateUserGroupsOption) (UserGroup, error) { | ||
params := UpdateUserGroupsParams{} | ||
|
||
for _, opt := range options { | ||
opt(¶ms) | ||
} | ||
|
||
values := url.Values{ | ||
"token": {api.token}, | ||
"usergroup": {userGroup.ID}, | ||
"usergroup": {userGroupID}, | ||
} | ||
|
||
if userGroup.Name != "" { | ||
values["name"] = []string{userGroup.Name} | ||
if params.Name != "" { | ||
values["name"] = []string{params.Name} | ||
} | ||
|
||
if userGroup.Handle != "" { | ||
values["handle"] = []string{userGroup.Handle} | ||
if params.Handle != "" { | ||
values["handle"] = []string{params.Handle} | ||
} | ||
|
||
if userGroup.Description != "" { | ||
values["description"] = []string{userGroup.Description} | ||
if !reflect.ValueOf(params.Description).IsNil() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [question] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was my misunderstanding. Thanks! |
||
values["description"] = []string{*params.Description} | ||
} | ||
|
||
if len(userGroup.Prefs.Channels) > 0 { | ||
values["channels"] = []string{strings.Join(userGroup.Prefs.Channels, ",")} | ||
if !reflect.ValueOf(params.Channels).IsNil() { | ||
values["channels"] = []string{strings.Join(*params.Channels, ",")} | ||
} | ||
|
||
response, err := api.userGroupRequest(ctx, "usergroups.update", values) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the type of channels should be
[]string
.