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

add enable_section option for UserGroup API #1364

Merged
merged 2 commits into from
Feb 1, 2025
Merged
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
52 changes: 45 additions & 7 deletions usergroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package slack
import (
"context"
"net/url"
"strconv"
"strings"
)

Expand Down Expand Up @@ -50,20 +51,45 @@ func (api *Client) userGroupRequest(ctx context.Context, path string, values url
return response, response.Err()
}

// createUserGroupParams contains arguments for CreateUserGroup method call
type createUserGroupParams struct {
enableSection bool
}

// CreateUserGroupOption options for the CreateUserGroup method call.
type CreateUserGroupOption func(*createUserGroupParams)

// CreateUserGroupOptionEnableSection enable the section for the user group (default: false)
func CreateUserGroupOptionEnableSection(enableSection bool) CreateUserGroupOption {
return func(params *createUserGroupParams) {
params.enableSection = enableSection
}
}

// CreateUserGroup creates a new user group.
// For more information see the CreateUserGroupContext documentation.
func (api *Client) CreateUserGroup(userGroup UserGroup) (UserGroup, error) {
return api.CreateUserGroupContext(context.Background(), userGroup)
func (api *Client) CreateUserGroup(userGroup UserGroup, options ...CreateUserGroupOption) (UserGroup, error) {
return api.CreateUserGroupContext(context.Background(), userGroup, options...)
}

// CreateUserGroupContext creates a new user group with a custom context.
// Slack API docs: https://api.slack.com/methods/usergroups.create
func (api *Client) CreateUserGroupContext(ctx context.Context, userGroup UserGroup) (UserGroup, error) {
func (api *Client) CreateUserGroupContext(ctx context.Context, userGroup UserGroup, options ...CreateUserGroupOption) (UserGroup, error) {
params := createUserGroupParams{}

for _, opt := range options {
opt(&params)
}

values := url.Values{
"token": {api.token},
"name": {userGroup.Name},
}

if params.enableSection {
values["enable_section"] = []string{strconv.FormatBool(params.enableSection)}
}

if userGroup.TeamID != "" {
values["team_id"] = []string{userGroup.TeamID}
}
Expand Down Expand Up @@ -236,12 +262,20 @@ func UpdateUserGroupsOptionChannels(channels []string) UpdateUserGroupsOption {
}
}

// UpdateUserGroupsOptionEnableSection enable the section for the user group (default: false)
func UpdateUserGroupsOptionEnableSection(enableSection bool) UpdateUserGroupsOption {
return func(params *UpdateUserGroupsParams) {
params.EnableSection = enableSection
}
}

// UpdateUserGroupsParams contains arguments for UpdateUserGroup method call
type UpdateUserGroupsParams struct {
Name string
Handle string
Description *string
Channels *[]string
Name string
Handle string
Description *string
Channels *[]string
EnableSection bool
}

// UpdateUserGroup will update an existing user group.
Expand Down Expand Up @@ -280,6 +314,10 @@ func (api *Client) UpdateUserGroupContext(ctx context.Context, userGroupID strin
values["channels"] = []string{strings.Join(*params.Channels, ",")}
}

if params.EnableSection {
values["enable_section"] = []string{strconv.FormatBool(params.EnableSection)}
}

response, err := api.userGroupRequest(ctx, "usergroups.update", values)
if err != nil {
return UserGroup{}, err
Expand Down