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

[MM-803]: Added a slash command to set a default jira instance in case of multiple instances #1111

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
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
41 changes: 40 additions & 1 deletion server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var jiraCommandHandler = CommandHandler{
"instance/settings": executeSettings,
"instance/uninstall": executeInstanceUninstall,
"instance/v2": executeInstanceV2Legacy,
"instance/default": executeDefaultInstance,
"issue/assign": executeAssign,
"issue/transition": executeTransition,
"issue/unassign": executeUnassign,
Expand Down Expand Up @@ -94,6 +95,7 @@ const sysAdminHelpText = "\n###### For System Administrators:\n" +
"* `/jira instance alias [URL] [alias-name]` - assign an alias to an instance\n" +
"* `/jira instance unalias [alias-name]` - remve an alias from an instance\n" +
"* `/jira instance v2 <jiraURL>` - Set the Jira instance to process \"v2\" webhooks and subscriptions (not prefixed with the instance ID)\n" +
"* `/jira instance default <jiraURL>` - Set a default instance in case of multiple Jira instances\n" +
"* `/jira webhook [--instance=<jiraURL>]` - Show the Mattermost webhook to receive JQL queries\n" +
"* `/jira v2revert ` - Revert to V2 jira plugin data model\n" +
""
Expand Down Expand Up @@ -167,12 +169,13 @@ func addSubCommands(jira *model.AutocompleteData, optInstance bool) {

func createInstanceCommand(optInstance bool) *model.AutocompleteData {
instance := model.NewAutocompleteData(
"instance", "[alias|connect|disconnect|settings|unalias]", "View and manage installed Jira instances; more commands available to system administrators")
"instance", "[alias|connect|disconnect|settings|unalias|default]", "View and manage installed Jira instances; more commands available to system administrators")
instance.AddCommand(createAliasCommand())
instance.AddCommand(createUnAliasCommand())
instance.AddCommand(createConnectCommand())
instance.AddCommand(createSettingsCommand(optInstance))
instance.AddCommand(createDisconnectCommand())
instance.AddCommand(createDefaultInstanceCommand())

jiraTypes := []model.AutocompleteListItem{
{HelpText: "Jira Server or Datacenter", Item: "server"},
Expand Down Expand Up @@ -254,6 +257,13 @@ func createDisconnectCommand() *model.AutocompleteData {
return disconnect
}

func createDefaultInstanceCommand() *model.AutocompleteData {
defaultInstance := model.NewAutocompleteData(
"default", "[Jira URL]", "Set a default instance in case of multiple Jira instances")
defaultInstance.AddDynamicListArgument("Jira URL", makeAutocompleteRoute(routeAutocompleteInstalledInstanceWithAlias), true)
return defaultInstance
}

func createSettingsCommand(optInstance bool) *model.AutocompleteData {
settings := model.NewAutocompleteData(
"settings", "[list|notifications]", "View or update your user settings")
Expand Down Expand Up @@ -422,6 +432,35 @@ func executeDisconnect(p *Plugin, c *plugin.Context, header *model.CommandArgs,
return p.responsef(header, "You have successfully disconnected your Jira account (**%s**).", disconnected.DisplayName)
}

func executeDefaultInstance(p *Plugin, c *plugin.Context, header *model.CommandArgs, args ...string) *model.CommandResponse {
if len(args) < 1 {
return p.responsef(header, "Please specify the jira instance URL")
wiggin77 marked this conversation as resolved.
Show resolved Hide resolved
}

if len(args) > 1 {
return p.help(header)
}
raghavaggarwal2308 marked this conversation as resolved.
Show resolved Hide resolved
jiraURL := ""
if len(args) > 0 {
jiraURL = args[0]
}
raghavaggarwal2308 marked this conversation as resolved.
Show resolved Hide resolved
instances, err := p.instanceStore.LoadInstances()
if err != nil {
return p.responsef(header, "Failed to load instances. Error: %v.", err)
}

instance := instances.getByAlias(jiraURL)
if instance != nil {
jiraURL = instance.InstanceID.String()
}

if err := p.SetDefaultInstance(jiraURL, types.ID(header.UserId)); err != nil {
return p.responsef(header, "Could not complete the **default instance** request. Error: %v", err)
}

return p.responsef(header, "You have successfully set a default instance for your Jira account (**%s**).", jiraURL)
}

func executeConnect(p *Plugin, c *plugin.Context, header *model.CommandArgs, args ...string) *model.CommandResponse {
if len(args) > 1 {
return p.help(header)
Expand Down
2 changes: 1 addition & 1 deletion server/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func (p *Plugin) resolveUserInstanceURL(user *User, instanceURL string) (types.I
if user.ConnectedInstances.Len() == 1 {
return user.ConnectedInstances.IDs()[0], nil
}
return "", errors.Wrap(kvstore.ErrNotFound, "unable to pick the default Jira instance")
return "", errors.New("default jira instance not found, please run `/jira instance default <jiraURL>` to set one")
}

func (p *Plugin) httpAutocompleteConnect(w http.ResponseWriter, r *http.Request) (int, error) {
Expand Down
19 changes: 19 additions & 0 deletions server/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,25 @@ func (p *Plugin) DisconnectUser(instanceURL string, mattermostUserID types.ID) (
return p.disconnectUser(instance, user)
}

func (p *Plugin) SetDefaultInstance(instanceURL string, mattermostUserID types.ID) error {
user, instance, err := p.LoadUserInstance(mattermostUserID, instanceURL)
if err != nil {
return err
}

if !user.ConnectedInstances.Contains(instance.GetID()) {
return errors.Wrapf(kvstore.ErrNotFound, "user is not connected to %q", instance.GetID())
}

user.DefaultInstanceID = instance.GetID()

if err := p.userStore.StoreUser(user); err != nil {
return err
}

return nil
}

func (p *Plugin) disconnectUser(instance Instance, user *User) (*Connection, error) {
if !user.ConnectedInstances.Contains(instance.GetID()) {
return nil, errors.Wrapf(kvstore.ErrNotFound, "user is not connected to %q", instance.GetID())
Expand Down
Loading