Skip to content

Commit

Permalink
MM-15101 Adding /jira transition (#46)
Browse files Browse the repository at this point in the history
* Adding /jira transition

* Moving transitionJiraIssue to a better home.
  • Loading branch information
crspeller committed May 21, 2019
1 parent aeedd9b commit 62915b2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
19 changes: 18 additions & 1 deletion server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
const helpText = "###### Mattermost Jira Plugin - Slash Command Help\n" +
"* `/jira connect` - Connect your Mattermost account to your Jira account and subscribe to events\n" +
"* `/jira disconnect` - Disonnect your Mattermost account from your Jira account\n" +
"* `/jira transition <issue-key> <state>` - Changes the state of a Jira issue.\n" +
"* `/jira instance [add/list/select/delete]` - Manage connected Jira instances\n" +
" * `add server <URL>` - Add a Jira Server instance\n" +
" * `add cloud` - Add a Jira Cloud instance\n" +
Expand All @@ -27,7 +28,7 @@ func getCommand() *model.Command {
DisplayName: "Jira",
Description: "Integration with Jira.",
AutoComplete: true,
AutoCompleteDesc: "Available commands: connect, disconnect, help",
AutoCompleteDesc: "Available commands: connect, disconnect, transition, instance, help",
AutoCompleteHint: "[command]",
}
}
Expand All @@ -49,6 +50,8 @@ func (p *Plugin) ExecuteCommand(c *plugin.Context, commandArgs *model.CommandArg
return executeDisconnect(p, c, args), nil
case "instance":
return executeInstance(p, c, args), nil
case "transition":
return executeTransition(p, c, args, commandArgs.UserId), nil
}

return responsef("Action %v is not supported.", action), nil
Expand Down Expand Up @@ -250,6 +253,20 @@ func executeInstanceDelete(p *Plugin, c *plugin.Context, args ...string) *model.
return executeInstanceSelect(p, c, "1")
}

func executeTransition(p *Plugin, c *plugin.Context, args []string, userId string) *model.CommandResponse {
if len(args) < 2 {
return responsef("Please specify both an issue key and state in the form `/jira transition <issue-key> <state>`")
}
issueKey := args[0]
toState := strings.Join(args[1:], " ")

if err := p.transitionJiraIssue(userId, issueKey, toState); err != nil {
return responsef("%v", err)
}

return responsef("Transition completed.")
}

func responsef(format string, args ...interface{}) *model.CommandResponse {
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
Expand Down
45 changes: 45 additions & 0 deletions server/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/andygrunwald/go-jira"
"github.com/pkg/errors"
Expand Down Expand Up @@ -179,3 +180,47 @@ func httpAPIGetCreateIssueMetadata(ji Instance, w http.ResponseWriter, r *http.R

return http.StatusOK, nil
}

func (p *Plugin) transitionJiraIssue(mmUserId, issueKey, toState string) error {
ji, err := p.LoadCurrentJIRAInstance()
if err != nil {
return err
}

jiraUser, err := ji.GetPlugin().LoadJIRAUser(ji, mmUserId)
if err != nil {
return err
}

jiraClient, err := ji.GetJIRAClient(jiraUser)
if err != nil {
return err
}

transitions, _, err := jiraClient.Issue.GetTransitions(issueKey)
if err != nil {
return fmt.Errorf("We couldn't find the issue key. Please confirm the issue key and try again. You may not have permissions to access this issue.")
}

if len(transitions) < 1 {
return fmt.Errorf("You do not have the appropriate permissions to perform this action. Please contact your Jira administrator.")
}

var transitionToUse *jira.Transition
for _, transition := range transitions {
if strings.Contains(strings.ToLower(transition.To.Name), strings.ToLower(toState)) {
transitionToUse = &transition
break
}
}

if transitionToUse == nil {
return fmt.Errorf("We couldn't find the state. Please use a Jira state such as 'done' and try again.")
}

if _, err := jiraClient.Issue.DoTransition(issueKey, transitionToUse.ID); err != nil {
return err
}

return nil
}

0 comments on commit 62915b2

Please sign in to comment.