56 lines
1.1 KiB
Go

package plugin
import (
"encoding/json"
"fmt"
"github.com/mattermost/mattermost/server/public/model"
)
func (p *Plugin) ensureBot() error {
const botUsername = "sentry"
user, appErr := p.API.GetUserByUsername(botUsername)
if appErr == nil && user != nil {
if !user.IsBot {
return fmt.Errorf("user @%s exists but is not a bot", botUsername)
}
p.botUserID = user.Id
return nil
}
bot := &model.Bot{
Username: botUsername,
DisplayName: "Sentry",
Description: "Sentry notifications bot",
}
createdBot, appErr := p.API.CreateBot(bot)
if appErr != nil {
p.API.LogError("Failed to create Sentry bot", "error", appErr.Error())
return appErr
}
p.botUserID = createdBot.UserId
return nil
}
func (p *Plugin) getChannelForProject(projectID string) (string, error) {
key := "ru.loop.plugin.sentry:project:" + projectID
data, appErr := p.API.KVGet(key)
if appErr != nil {
return "", appErr
}
if data == nil {
return "", nil
}
var project LinkedProject
if err := json.Unmarshal(data, &project); err != nil {
return "", err
}
return project.ChannelID, nil
}