From 3d48fded42db991310d13bb4ea661c2c93190938 Mon Sep 17 00:00:00 2001 From: Goir Date: Fri, 21 May 2021 15:57:13 +0200 Subject: [PATCH] allow configration of Mattermost Username (#455) ##### ISSUE TYPE - Bug fix Pull Request ##### SUMMARY Allow Configuration of Mattermost user and fixes BUG with getUser() if its not the "BotKube" default user. Fixes #245 --- helm/botkube/values.yaml | 1 + pkg/bot/mattermost.go | 12 ++++++------ pkg/config/config.go | 1 + 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/helm/botkube/values.yaml b/helm/botkube/values.yaml index a3d6076..4524783 100644 --- a/helm/botkube/values.yaml +++ b/helm/botkube/values.yaml @@ -283,6 +283,7 @@ communications: # Settings for Mattermost mattermost: enabled: false + botName: 'BotKube' # User in mattermost which belongs the Personal Access token to url: 'MATTERMOST_SERVER_URL' # URL where Mattermost is running. e.g https://example.com:9243 token: 'MATTERMOST_TOKEN' # Personal Access token generated by BotKube user team: 'MATTERMOST_TEAM' # Mattermost Team to configure with BotKube diff --git a/pkg/bot/mattermost.go b/pkg/bot/mattermost.go index 4fbe49a..47c0916 100644 --- a/pkg/bot/mattermost.go +++ b/pkg/bot/mattermost.go @@ -39,8 +39,6 @@ const ( ) const ( - // BotName stores BotKube details - BotName = "botkube" // WebSocketProtocol stores protocol initials for web socket WebSocketProtocol = "ws://" // WebSocketSecureProtocol stores protocol initials for web socket @@ -50,6 +48,7 @@ const ( // MMBot listens for user's message, execute commands and sends back the response type MMBot struct { Token string + BotName string TeamName string ChannelName string ClusterName string @@ -75,6 +74,7 @@ type mattermostMessage struct { func NewMattermostBot(c *config.Config) Bot { return &MMBot{ ServerURL: c.Communications.Mattermost.URL, + BotName: c.Communications.Mattermost.BotName, Token: c.Communications.Mattermost.Token, TeamName: c.Communications.Mattermost.Team, ChannelName: c.Communications.Mattermost.Channel, @@ -135,7 +135,7 @@ func (mm *mattermostMessage) handleMessage(b MMBot) { if channelType == mmChannelPrivate || channelType == mmChannelPublic { // Message posted in a channel // Serve only if starts with mention - if !strings.HasPrefix(post.Message, "@"+BotName+" ") { + if !strings.HasPrefix(post.Message, "@"+b.BotName+" ") { return } } @@ -147,7 +147,7 @@ func (mm *mattermostMessage) handleMessage(b MMBot) { log.Debugf("Received mattermost event: %+v", mm.Event.Data) // Trim the @BotKube prefix if exists - mm.Request = strings.TrimPrefix(post.Message, "@"+BotName+" ") + mm.Request = strings.TrimPrefix(post.Message, "@"+b.BotName+" ") e := execute.NewDefaultExecutor(mm.Request, b.AllowKubectl, b.RestrictAccess, b.DefaultNamespace, b.ClusterName, config.MattermostBot, b.ChannelName, mm.IsAuthChannel) @@ -209,9 +209,9 @@ func (b MMBot) getTeam() *model.Team { // Check if BotKube user exists in Mattermost func (b MMBot) getUser() *model.User { - users, resp := b.APIClient.AutocompleteUsersInTeam(b.getTeam().Id, BotName, 1, "") + users, resp := b.APIClient.AutocompleteUsersInTeam(b.getTeam().Id, b.BotName, 1, "") if resp.Error != nil { - log.Fatalf("There was a problem finding Mattermost user %s. %s", BotName, resp.Error) + log.Fatalf("There was a problem finding Mattermost user %s. %s", b.BotName, resp.Error) } return users.Users[0] } diff --git a/pkg/config/config.go b/pkg/config/config.go index d150da6..63790d4 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -176,6 +176,7 @@ type Index struct { // Mattermost configuration to authentication and send notifications type Mattermost struct { Enabled bool + BotName string `yaml:"botName"` URL string Token string Team string