From 6615a26f89c4066cfecb4914fac33d5b3697ff19 Mon Sep 17 00:00:00 2001 From: sivchari Date: Mon, 6 Jan 2025 13:22:57 +0900 Subject: [PATCH 1/2] add enable_section option for UserGroup API Signed-off-by: sivchari --- usergroups.go | 52 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/usergroups.go b/usergroups.go index 0c2ec4c9a..a3eccb517 100644 --- a/usergroups.go +++ b/usergroups.go @@ -3,6 +3,7 @@ package slack import ( "context" "net/url" + "strconv" "strings" ) @@ -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(¶ms) + } + 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} } @@ -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. @@ -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 From 9bca9dee876433597b03fee509965535b24fc885 Mon Sep 17 00:00:00 2001 From: sivchari Date: Wed, 29 Jan 2025 10:20:04 +0900 Subject: [PATCH 2/2] make CreateUserGroupParam private Signed-off-by: sivchari --- usergroups.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/usergroups.go b/usergroups.go index a3eccb517..256f7bd37 100644 --- a/usergroups.go +++ b/usergroups.go @@ -51,18 +51,18 @@ 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 +// createUserGroupParams contains arguments for CreateUserGroup method call +type createUserGroupParams struct { + enableSection bool } // CreateUserGroupOption options for the CreateUserGroup method call. -type CreateUserGroupOption func(*CreateUserGroupParams) +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 + return func(params *createUserGroupParams) { + params.enableSection = enableSection } } @@ -75,7 +75,7 @@ func (api *Client) CreateUserGroup(userGroup UserGroup, options ...CreateUserGro // 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, options ...CreateUserGroupOption) (UserGroup, error) { - params := CreateUserGroupParams{} + params := createUserGroupParams{} for _, opt := range options { opt(¶ms) @@ -86,8 +86,8 @@ func (api *Client) CreateUserGroupContext(ctx context.Context, userGroup UserGro "name": {userGroup.Name}, } - if params.EnableSection { - values["enable_section"] = []string{strconv.FormatBool(params.EnableSection)} + if params.enableSection { + values["enable_section"] = []string{strconv.FormatBool(params.enableSection)} } if userGroup.TeamID != "" {