From 3f0bcc4043560c90b1650b77335648c42558d10a Mon Sep 17 00:00:00 2001 From: sivchari Date: Mon, 6 Jan 2025 13:22:57 +0900 Subject: [PATCH] add enable_section option for UserGroup API Signed-off-by: sivchari --- usergroups.go | 52 +++++++++++++++++++++++++++++++++++++++------- usergroups_test.go | 2 +- 2 files changed, 46 insertions(+), 8 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 diff --git a/usergroups_test.go b/usergroups_test.go index 9586cf167..9077f1b1f 100644 --- a/usergroups_test.go +++ b/usergroups_test.go @@ -81,7 +81,7 @@ func TestCreateUserGroup(t *testing.T) { for i, test := range tests { rh = newUserGroupsHandler() - _, err := api.CreateUserGroup(test.userGroup) + _, err := api.CreateUserGroup(test.userGroup, test.createUserGroupOption...) if err != nil { t.Fatalf("%d: Unexpected error: %s", i, err) }