Skip to content

Commit

Permalink
Merge pull request #10 from Brightscout/MI-801
Browse files Browse the repository at this point in the history
MI-801 Added confluence bot profile image
  • Loading branch information
chetanyakan authored Feb 5, 2020
2 parents 7e5c42c + 46a5fba commit d4771b3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
19 changes: 16 additions & 3 deletions server/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ const (
specifyAlias = "Please specify alias."
subscriptionDeleteSuccess = "Subscription with alias **%s** deleted successfully."
noChannelSubscription = "No subscription found for this channel."
helpText = "###### Mattermost Confluence Plugin - Slash Command Help\n" +
"\n* `/confluence subscribe` - Subscribe the current channel to receive notifications from Confluence.\n" +
"* `/confluence unsubscribe \"<alias>\"` - Unsubscribe notifications for the current channel for a given subscription alias.\n" +
"* `/confluence list` - List all subscriptions configured for the current channel.\n" +
"* `/confluence edit \"<alias>\"` - Edit the subscribed events for the given subscription alias for the current channel.\n"
)

var ConfluenceCommandHandler = Handler{
handlers: map[string]HandlerFunc{
"list": listChannelSubscription,
"unsubscribe": deleteSubscription,
"edit": editSubscription,
"help": confluenceHelp,
},
defaultHandler: executeConfluenceDefault,
}
Expand All @@ -39,16 +45,15 @@ func GetCommand() *model.Command {
DisplayName: "Confluence",
Description: "Integration with Confluence.",
AutoComplete: true,
AutoCompleteDesc: "Available commands: subscribe, list, unsubscribe \"<alias>\"",
AutoCompleteDesc: "Available commands: subscribe, list, unsubscribe \"<alias>\", edit \"<alias>\", help.",
AutoCompleteHint: "[command]",
}
}

// TODO : Show help text instead of invalid command.
func executeConfluenceDefault(context *model.CommandArgs, args ...string) *model.CommandResponse {
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
Text: "Invalid command",
Text: "Invalid command parameters. Please use `/confluence help` for more information.",
}
}

Expand All @@ -62,6 +67,9 @@ func postCommandResponse(context *model.CommandArgs, text string) {
}

func (ch Handler) Handle(context *model.CommandArgs, args ...string) *model.CommandResponse {
if len(args) == 0 {
return ch.handlers["help"](context, "")
}
for n := len(args); n > 0; n-- {
h := ch.handlers[strings.Join(args[:n], "/")]
if h != nil {
Expand Down Expand Up @@ -112,3 +120,8 @@ func editSubscription(context *model.CommandArgs, args ...string) *model.Command
}
return &model.CommandResponse{}
}

func confluenceHelp(context *model.CommandArgs, args ...string) *model.CommandResponse {
postCommandResponse(context, helpText)
return &model.CommandResponse{}
}
45 changes: 36 additions & 9 deletions server/plugin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package main

import (
"io/ioutil"
"net/http"
"path/filepath"

"github.com/pkg/errors"

"github.com/Brightscout/mattermost-plugin-confluence/server/command"
"github.com/Brightscout/mattermost-plugin-confluence/server/util"
Expand All @@ -25,16 +29,10 @@ type Plugin struct {

func (p *Plugin) OnActivate() error {
config.Mattermost = p.API
botUserID, err := p.Helpers.EnsureBot(&model.Bot{
Username: botUserName,
DisplayName: botDisplayName,
Description: botDescription,
})
if err != nil {
config.Mattermost.LogError("Error in setting up bot user", "Error", err.Error())
return err

if err := p.setUpBotUser(); err != nil {
config.Mattermost.LogError("Failed to create a bot user", "Error", err.Error())
}
config.BotUserID = botUserID

if err := p.OnConfigurationChange(); err != nil {
return err
Expand Down Expand Up @@ -73,6 +71,35 @@ func (p *Plugin) OnConfigurationChange() error {
return nil
}

func (p *Plugin) setUpBotUser() error {
botUserID, err := p.Helpers.EnsureBot(&model.Bot{
Username: botUserName,
DisplayName: botDisplayName,
Description: botDescription,
})
if err != nil {
config.Mattermost.LogError("Error in setting up bot user", "Error", err.Error())
return err
}

bundlePath, err := p.API.GetBundlePath()
if err != nil {
return err
}

profileImage, err := ioutil.ReadFile(filepath.Join(bundlePath, "assets", "logo.png"))
if err != nil {
return err
}

if appErr := p.API.SetProfileImage(botUserID, profileImage); appErr != nil {
return errors.Wrap(appErr, "couldn't set profile image")
}

config.BotUserID = botUserID
return nil
}

func (p *Plugin) ExecuteCommand(context *plugin.Context, commandArgs *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
args, argErr := util.SplitArgs(commandArgs.Command)
if argErr != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const initialState = {
alias: '',
baseURL: '',
spaceKey: '',
events: [],
events: Constants.CONFLUENCE_EVENTS,
error: '',
saving: false,
};
Expand Down

0 comments on commit d4771b3

Please sign in to comment.