-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
107 lines (88 loc) · 2.24 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package issue
import (
"errors"
"fmt"
"log"
"github.com/AlecAivazis/survey/v2"
"github.com/andygrunwald/go-jira"
"github.com/briandowns/spinner"
"github.com/locona/jira/pkg/issue"
"github.com/locona/jira/pkg/prompt"
"github.com/spf13/cobra"
)
func NewCommandStatus() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
RunE: func(cmd *cobra.Command, args []string) error {
return Status()
},
}
return cmd
}
type StatusCommand struct {
Issue *jira.Issue
Transition *jira.Transition
}
func (cmd *StatusCommand) Request(s *spinner.Spinner) error {
label := issue.Label(*cmd.Issue)
suffixFormat := "%v : Status To `%v`"
suffixMsg := fmt.Sprintf(suffixFormat, label, cmd.Transition.Name)
var suf = make([]byte, 100)
copy(suf, suffixMsg)
s.Suffix = string(suf)
s.FinalMSG = fmt.Sprintf("%v %v \n", prompt.IconClear, suffixMsg)
err := issue.ChangeTransition(cmd.Issue.Key, cmd.Transition.ID)
if err != nil {
return err
}
return nil
}
func (cmd *StatusCommand) Response() error {
return nil
}
func Status() error {
selectedIssueSlice, err := multiSelectIssue("Select the issue status you whose status you want to change.")
if err != nil {
return err
}
if len(selectedIssueSlice) < 1 {
return errors.New("Required Select Issue")
}
selectedTransition, err := selectTransition(selectedIssueSlice[0].Key)
if err != nil {
return err
}
for idx := range selectedIssueSlice {
err := prompt.Progress(&StatusCommand{
Issue: selectedIssueSlice[idx],
Transition: selectedTransition,
})
if err != nil {
log.Println(err)
}
}
return nil
}
func selectTransition(issueID string) (*jira.Transition, error) {
transitions, err := issue.TransitionList(issueID)
if err != nil {
return nil, err
}
options := make([]string, 0)
mapNameToTransition := make(map[string]jira.Transition, 0)
for _, transition := range transitions {
mapNameToTransition[transition.Name] = transition
options = append(options, transition.Name)
}
prompt := &survey.Select{
Message: "Select the status you want to change.",
Options: options,
}
var targetTransition string
err = survey.AskOne(prompt, &targetTransition, nil)
if err != nil {
return nil, err
}
res := mapNameToTransition[targetTransition]
return &res, nil
}