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 fallback strategy to get MM team #1214

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Changes from 1 commit
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
46 changes: 32 additions & 14 deletions pkg/bot/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
Expand Down Expand Up @@ -102,26 +103,20 @@ func NewMattermost(ctx context.Context, log logrus.FieldLogger, commGroupName st

client := model.NewAPIv4Client(mmURL)
client.SetOAuthToken(cfg.Token)
botTeams, _, err := client.SearchTeams(ctx, &model.TeamSearch{
Term: cfg.Team,
})

// In Mattermost v7.0+, what we see in MM Console is `display_name` of a team.
// We need `name` of the team to make the rest of the business logic work.
team, err := getMattermostTeam(ctx, client, cfg.Team)
if err != nil {
return nil, fmt.Errorf("while getting team by name: %w", err)
return nil, fmt.Errorf("while getting team details: %w", err)
}

if len(botTeams) == 0 {
return nil, fmt.Errorf("team %q not found", cfg.Team)
}
botTeam := botTeams[0]
// In Mattermost v7.0+, what we see in MM Console is `display_name` of team.
// We need `name` of team to make rest of the business logic work.
cfg.Team = botTeam.Name
channelsByIDCfg, err := mattermostChannelsCfgFrom(ctx, client, botTeam.Id, cfg.Channels)
channelsByIDCfg, err := mattermostChannelsCfgFrom(ctx, client, team.Id, cfg.Channels)
if err != nil {
return nil, fmt.Errorf("while producing channels configuration map by ID: %w", err)
}

botUserID, err := getBotUserID(ctx, client, botTeam.Id, cfg.BotName)
botUserID, err := getBotUserID(ctx, client, team.Id, cfg.BotName)
if err != nil {
return nil, fmt.Errorf("while getting bot user ID: %w", err)
}
Expand All @@ -133,7 +128,7 @@ func NewMattermost(ctx context.Context, log logrus.FieldLogger, commGroupName st
serverURL: cfg.URL,
botName: cfg.BotName,
botUserID: botUserID,
teamName: cfg.Team,
teamName: team.Name,
apiClient: client,
webSocketURL: webSocketURL,
commGroupName: commGroupName,
Expand Down Expand Up @@ -528,6 +523,29 @@ func getBotUserID(ctx context.Context, client *model.Client4, teamID, botName st
return teamMember.UserId, nil
}

func getMattermostTeam(ctx context.Context, client *model.Client4, name string) (*model.Team, error) {
botTeams, r, err := client.SearchTeams(ctx, &model.TeamSearch{
Term: name, // the search term to match against the name or display name of teams
})
if err != nil {
return nil, fmt.Errorf("while searching team by term: %w", err)
}

if r.StatusCode == http.StatusNotImplemented || len(botTeams) == 0 {
// try to check if we can get a given team directly
botTeam, _, err := client.GetTeamByName(ctx, name, "")
if err != nil {
return nil, fmt.Errorf("while getting team by name: %w", err)
}
botTeams = append(botTeams, botTeam)
}
if len(botTeams) == 0 {
return nil, fmt.Errorf("team %q not found", name)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we handle a case when there are multiple botTeams? Maybe return an error that we found multiple (printing theirs name + displayName)?

return botTeams[0], err
}

func mattermostChannelsCfgFrom(ctx context.Context, client *model.Client4, teamID string, channelsCfg config.IdentifiableMap[config.ChannelBindingsByName]) (map[string]channelConfigByID, error) {
res := make(map[string]channelConfigByID)
for channAlias, channCfg := range channelsCfg {
Expand Down
Loading