diff --git a/actions.go b/actions.go index 860f5ad..59d8770 100644 --- a/actions.go +++ b/actions.go @@ -337,21 +337,43 @@ func ActionSetStatus(_ []string) { printError("No issue selected", nil) return } + issueKey := GetIssueKey() - issueTypes, err := GetAllStatuses(GetProjectId()) + issue, err := GetIssue(issueKey) if err != nil { - printError("Failed to get sprint statuses", err) + printError("Failed to fetch issue", err) return } - options := []huh.Option[string]{} - for _, status := range (*issueTypes)[0].Statuses { - name := fmt.Sprintf("%s (%s)", status.Name, status.ID) - options = append(options, huh.Option[string]{Key: name, Value: status.ID}) + + transitions, err := GetIssueTransitions(issueKey) + if err != nil { + printError("Could not fetch issue transitions", err) + return } - huh.NewSelect[string](). + + options := []huh.Option[string]{} + var selected string + for _, transition := range transitions { + options = append(options, huh.Option[string]{Key: transition.Name, Value: transition.ID}) + if transition.To.ID == issue.Fields.Status.ID { + selected = transition.ID + } + } + + err = huh.NewSelect[string](). Title("Select Option"). Options(options...). + Value(&selected). Run() + if err != nil { + printError("Failed to select status", err) + return + } + + err = UpdateIssueTransition(issueKey, selected) + if err != nil { + printError("Failed to update status", err) + } } func ActionSetTitle(_ []string) { diff --git a/commands.go b/commands.go index 4d01f59..5e6731e 100644 --- a/commands.go +++ b/commands.go @@ -56,9 +56,10 @@ var CommandTree = []CommandArg{ command("comments", ActionViewComments).WithHelp("View comments of an issue"), ).WithHelp("View data about something"), option("set", - command("status", ActionSetStatus).WithHelp("Set status of a ticket"), + command("status", ActionSetStatus).WithHelp("Transition a ticket"), command("title", ActionSetTitle).WithHelp("Set the title of a ticket"), ).WithHelp("Change something"), + command("transition", ActionSetStatus).WithHelp("Transition a ticket"), command("exit", ActionExit).WithHelp("Exit jirashell"), command("logout", ActionLogout).WithHelp("Logout from Jira"), command("back", ActionBack).WithHelp("Go back"), diff --git a/jira.go b/jira.go index c0120b7..3d3e2ed 100644 --- a/jira.go +++ b/jira.go @@ -124,3 +124,25 @@ func UpdateIssueTitle(issue string, title string) error { return resp.Body.Close() }) } + +func GetIssueTransitions(issue string) ([]jira.Transition, error) { + r, err := RunSpinner("Getting issue transitions...", func(ctx context.Context) (*[]jira.Transition, any, error) { + transitions, response, err := jiraClient.Issue.GetTransitions(ctx, issue) + return &transitions, response, err + }) + if r == nil { + return nil, err + } else { + return *r, err + } +} + +func UpdateIssueTransition(issue string, transition string) error { + return RunSpinnerRaw("Transitioning issue...", func(ctx context.Context) error { + resp, err := jiraClient.Issue.DoTransition(ctx, issue, transition) + if err != nil { + return err + } + return resp.Body.Close() + }) +}