Skip to content

Commit

Permalink
add enable_section option for UserGroup API (#1364)
Browse files Browse the repository at this point in the history
close #1363 

##### Pull Request Guidelines

These are recommendations for pull requests.
They are strictly guidelines to help manage expectations.

##### PR preparation
Run `make pr-prep` from the root of the repository to run formatting,
linting and tests.

##### Should this be an issue instead
- [x] is it a convenience method? (no new functionality, streamlines
some use case)
- [x] exposes a previously private type, const, method, etc.
- [x] is it application specific (caching, retry logic, rate limiting,
etc)
- [x] is it performance related.

##### API changes

Since API changes have to be maintained they undergo a more detailed
review and are more likely to require changes.

- no tests, if you're adding to the API include at least a single test
of the happy case.
- If you can accomplish your goal without changing the API, then do so.
- dependency changes. updates are okay. adding/removing need
justification.

###### Examples of API changes that do not meet guidelines:
- in library cache for users. caches are use case specific.
- Convenience methods for Sending Messages, update, post, ephemeral,
etc. consider opening an issue instead.
  • Loading branch information
nlopes authored Feb 1, 2025
2 parents 1c78410 + 9bca9de commit 76e627a
Showing 1 changed file with 45 additions and 7 deletions.
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

0 comments on commit 76e627a

Please sign in to comment.