-
-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathcreate.go
More file actions
285 lines (258 loc) · 8.2 KB
/
Copy pathcreate.go
File metadata and controls
285 lines (258 loc) · 8.2 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package cmdcommon
import (
"fmt"
"strings"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ankitpokhrel/jira-cli/api"
"github.com/ankitpokhrel/jira-cli/internal/cmdutil"
"github.com/ankitpokhrel/jira-cli/pkg/jira"
)
const (
// ActionSubmit is a submit action.
ActionSubmit = "Submit"
// ActionCancel is a cancel action.
ActionCancel = "Cancel"
// ActionMetadata is an add metadata action.
ActionMetadata = "Add metadata"
)
// CreateParams holds parameters for create command.
type CreateParams struct {
Name string
IssueType string
ParentIssueKey string
Summary string
Body string
Priority string
Reporter string
Assignee string
Labels []string
Components []string
FixVersions []string
AffectsVersions []string
OriginalEstimate string
DueDate string
CustomFields map[string]string
Template string
NoInput bool
Debug bool
}
// SetCreateFlags sets flags supported by create command.
func SetCreateFlags(cmd *cobra.Command, prefix string) {
custom := make(map[string]string)
cmd.Flags().SortFlags = false
if prefix == "Epic" {
cmd.Flags().StringP("name", "n", "", "Epic name")
} else {
cmd.Flags().StringP("type", "t", "", "Issue type")
cmd.Flags().StringP("parent", "P", "", `Parent issue key can be used to attach epic to an issue.
And, this field is mandatory when creating a sub-task.`)
}
cmd.Flags().StringP("summary", "s", "", prefix+" summary or title")
cmd.Flags().StringP("body", "b", "", prefix+" description")
cmd.Flags().StringP("priority", "y", "", prefix+" priority")
cmd.Flags().StringP("reporter", "r", "", prefix+" reporter (username, email or display name)")
cmd.Flags().StringP("assignee", "a", "", prefix+" assignee (username, email or display name)")
cmd.Flags().StringArrayP("label", "l", []string{}, prefix+" labels")
cmd.Flags().StringArrayP("component", "C", []string{}, prefix+" components")
cmd.Flags().StringArray("fix-version", []string{}, "Release info (fixVersions)")
cmd.Flags().StringArray("affects-version", []string{}, "Release info (affectsVersions)")
cmd.Flags().StringP("original-estimate", "e", "", prefix+" Original estimate")
cmd.Flags().String("due-date", "", prefix+" due date (format: YYYY-MM-DD)")
cmd.Flags().StringToString("custom", custom, "Set custom fields")
cmd.Flags().StringP("template", "T", "", "Path to a file to read body/description from")
cmd.Flags().Bool("web", false, "Open in web browser after successful creation")
cmd.Flags().Bool("no-input", false, "Disable prompt for non-required fields")
}
// GetNextAction provide user an option to select next action.
func GetNextAction() *survey.Question {
return &survey.Question{
Name: "action",
Prompt: &survey.Select{
Message: "What's next?",
Options: []string{
ActionSubmit,
ActionMetadata,
ActionCancel,
},
},
Validate: survey.Required,
}
}
// GetMetadata gathers a list of metadata users wants to add.
func GetMetadata() []*survey.Question {
return []*survey.Question{
{
Name: "metadata",
Prompt: &survey.MultiSelect{
Message: "What would you like to add?",
Options: []string{"Priority", "Components", "Labels", "FixVersions", "AffectsVersions"},
},
},
}
}
// GetMetadataQuestions prepares metadata question to input from user.
func GetMetadataQuestions(cat []string) []*survey.Question {
var qs []*survey.Question
for _, c := range cat {
switch c {
case "Priority":
qs = append(qs, &survey.Question{
Name: "priority",
Prompt: &survey.Input{Message: "Priority"},
})
case "Components":
qs = append(qs, &survey.Question{
Name: "components",
Prompt: &survey.Input{
Message: "Components",
Help: "Comma separated list of valid components. For eg: BE,FE",
},
})
case "Labels":
qs = append(qs, &survey.Question{
Name: "labels",
Prompt: &survey.Input{
Message: "Labels",
Help: "Comma separated list of labels. For eg: backend,urgent",
},
})
case "FixVersions":
qs = append(qs, &survey.Question{
Name: "fixversions",
Prompt: &survey.Input{
Message: "Fix Versions",
Help: "Comma separated list of fixVersions. For eg: v1.0-beta,v2.0",
},
})
case "AffectsVersions":
qs = append(qs, &survey.Question{
Name: "affectsversions",
Prompt: &survey.Input{
Message: "Affects Versions",
Help: "Comma separated list of affectsVersions. For eg: v1.0-beta,v2.0",
},
})
}
}
return qs
}
// HandleNoInput handles operations for --no-input flag.
func HandleNoInput(params *CreateParams) error {
answer := struct{ Action string }{}
for answer.Action != ActionSubmit {
err := survey.Ask([]*survey.Question{GetNextAction()}, &answer)
if err != nil {
return err
}
switch answer.Action {
case ActionCancel:
cmdutil.Failed("Action aborted")
case ActionMetadata:
ans := struct{ Metadata []string }{}
err := survey.Ask(GetMetadata(), &ans)
if err != nil {
return err
}
if len(ans.Metadata) > 0 {
qs := GetMetadataQuestions(ans.Metadata)
ans := struct {
Priority string
Labels string
Components string
FixVersions string
AffectsVersions string
}{}
err := survey.Ask(qs, &ans)
if err != nil {
return err
}
if ans.Priority != "" {
params.Priority = ans.Priority
}
if len(ans.Labels) > 0 {
params.Labels = strings.Split(ans.Labels, ",")
}
if len(ans.Components) > 0 {
params.Components = strings.Split(ans.Components, ",")
}
if len(ans.FixVersions) > 0 {
params.FixVersions = strings.Split(ans.FixVersions, ",")
}
if len(ans.AffectsVersions) > 0 {
params.AffectsVersions = strings.Split(ans.AffectsVersions, ",")
}
}
}
}
return nil
}
// GetRelevantUser finds and returns a valid user name or account ID based on user input.
func GetRelevantUser(client *jira.Client, project string, user string) string {
if user == "" {
return ""
}
u, err := api.ProxyUserSearch(client, &jira.UserSearchOptions{
Query: user,
Project: project,
})
if err != nil || len(u) == 0 {
cmdutil.Failed("Unable to find associated user for %s", user)
}
return GetUserKeyForConfiguredInstallation(u[0])
}
// GetUserKeyForConfiguredInstallation returns either the user name or account ID based on jira installation type.
func GetUserKeyForConfiguredInstallation(user *jira.User) string {
it := viper.GetString("installation")
if it == jira.InstallationTypeLocal {
return user.Name
}
return user.AccountID
}
// GetConfiguredCustomFields returns the custom fields configured by the user.
func GetConfiguredCustomFields() ([]jira.IssueTypeField, error) {
var configuredFields []jira.IssueTypeField
err := viper.UnmarshalKey("issue.fields.custom", &configuredFields)
if err != nil {
return nil, err
}
return configuredFields, nil
}
// ValidateDueDate validates that the due date is in YYYY-MM-DD format.
func ValidateDueDate(dueDate string) error {
if dueDate == "" {
return nil
}
if _, err := time.Parse("2006-01-02", dueDate); err != nil {
return fmt.Errorf("invalid due date %q: expected format YYYY-MM-DD", dueDate)
}
return nil
}
// ValidateCustomFields validates custom fields.
// TODO: Fail with error instead of warning in future release.
func ValidateCustomFields(fields map[string]string, configuredFields []jira.IssueTypeField) {
if len(fields) == 0 {
return
}
fieldsMap := make(map[string]string)
for _, configured := range configuredFields {
identifier := strings.ReplaceAll(strings.ToLower(strings.TrimSpace(configured.Name)), " ", "-")
fieldsMap[identifier] = configured.Name
}
invalidCustomFields := make([]string, 0, len(fields))
for key := range fields {
if _, ok := fieldsMap[key]; !ok {
invalidCustomFields = append(invalidCustomFields, key)
}
}
if len(invalidCustomFields) > 0 {
cmdutil.Warn(`
Some custom fields are not configured and will be ignored. This will fail with error in the future release.
Please make sure that the passed custom fields are valid and configured accordingly in the config file.
Invalid custom fields used in the command: %s`,
strings.Join(invalidCustomFields, ", "),
)
}
}