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
This commit is contained in:
Goir 2021-05-21 15:57:13 +02:00 committed by GitHub
parent 5c40418b56
commit 3d48fded42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 6 deletions

View File

@ -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

View File

@ -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]
}

View File

@ -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