This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
issue_create.go
329 lines (288 loc) · 9.17 KB
/
issue_create.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package create
import (
"errors"
"fmt"
"net/url"
"strings"
"github.com/profclems/glab/internal/config"
"github.com/profclems/glab/internal/glrepo"
"github.com/profclems/glab/internal/utils"
"github.com/AlecAivazis/survey/v2"
"github.com/profclems/glab/commands/cmdutils"
"github.com/profclems/glab/commands/issue/issueutils"
"github.com/profclems/glab/pkg/api"
"github.com/profclems/glab/pkg/prompt"
"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
)
type CreateOpts struct {
Title string
Description string
Labels []string
Assignees []string
Weight int
MileStone int
LinkedMR int
NoEditor bool
IsConfidential bool
IsInteractive bool
OpenInWeb bool
Yes bool
IO *utils.IOStreams
BaseRepo func() (glrepo.Interface, error)
HTTPClient func() (*gitlab.Client, error)
Remotes func() (glrepo.Remotes, error)
Config func() (config.Config, error)
BaseProject *gitlab.Project
}
func NewCmdCreate(f *cmdutils.Factory) *cobra.Command {
opts := &CreateOpts{
IO: f.IO,
BaseRepo: f.BaseRepo,
HTTPClient: f.HttpClient,
Remotes: f.Remotes,
Config: f.Config,
}
var issueCreateCmd = &cobra.Command{
Use: "create [flags]",
Short: `Create an issue`,
Long: ``,
Aliases: []string{"new"},
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
apiClient, err := opts.HTTPClient()
if err != nil {
return err
}
repo, err := opts.BaseRepo()
if err != nil {
return err
}
hasTitle := cmd.Flags().Changed("title")
hasDescription := cmd.Flags().Changed("description")
// disable interactive mode if title and description are explicitly defined
opts.IsInteractive = !(hasTitle && hasDescription)
if opts.IsInteractive && !opts.IO.PromptEnabled() {
return &cmdutils.FlagError{Err: errors.New("--title and --description required for non-interactive mode")}
}
opts.BaseProject, err = api.GetProject(apiClient, repo.FullName())
if err != nil {
return err
}
if !opts.BaseProject.IssuesEnabled {
fmt.Fprintf(opts.IO.StdErr, "Issues are disabled for %q\n", opts.BaseProject.PathWithNamespace)
return cmdutils.SilentError
}
return createRun(opts)
},
}
issueCreateCmd.Flags().StringVarP(&opts.Title, "title", "t", "", "Supply a title for issue")
issueCreateCmd.Flags().StringVarP(&opts.Description, "description", "d", "", "Supply a description for issue")
issueCreateCmd.Flags().StringSliceVarP(&opts.Labels, "label", "l", []string{}, "Add label by name. Multiple labels should be comma separated")
issueCreateCmd.Flags().StringSliceVarP(&opts.Assignees, "assignee", "a", []string{}, "Assign issue to people by their `usernames`")
issueCreateCmd.Flags().IntVarP(&opts.MileStone, "milestone", "m", 0, "The global ID of a milestone to assign issue")
issueCreateCmd.Flags().BoolVarP(&opts.IsConfidential, "confidential", "c", false, "Set an issue to be confidential. Default is false")
issueCreateCmd.Flags().IntVarP(&opts.LinkedMR, "linked-mr", "", 0, "The IID of a merge request in which to resolve all issues")
issueCreateCmd.Flags().IntVarP(&opts.Weight, "weight", "w", 0, "The weight of the issue. Valid values are greater than or equal to 0.")
issueCreateCmd.Flags().BoolVarP(&opts.NoEditor, "no-editor", "", false, "Don't open editor to enter description. If set to true, uses prompt. Default is false")
issueCreateCmd.Flags().BoolVarP(&opts.Yes, "yes", "y", false, "Don't prompt for confirmation to submit the issue")
return issueCreateCmd
}
func createRun(opts *CreateOpts) error {
apiClient, err := opts.HTTPClient()
if err != nil {
return err
}
repo, err := opts.BaseRepo()
if err != nil {
return err
}
var templateName string
var templateContents string
issueCreateOpts := &gitlab.CreateIssueOptions{}
if opts.IsInteractive {
if opts.Description == "" {
if opts.NoEditor {
err = prompt.AskMultiline(&opts.Description, "Description:", "")
if err != nil {
return err
}
} else {
templateResponse := struct {
Index int
}{}
templateNames, err := cmdutils.ListGitLabTemplates(cmdutils.IssueTemplate)
if err != nil {
return fmt.Errorf("error getting templates: %w", err)
}
templateNames = append(templateNames, "Open a blank Issue")
selectQs := []*survey.Question{
{
Name: "index",
Prompt: &survey.Select{
Message: "Choose a template",
Options: templateNames,
},
},
}
if err := prompt.Ask(selectQs, &templateResponse); err != nil {
return fmt.Errorf("could not prompt: %w", err)
}
if templateResponse.Index != len(templateNames) {
templateName = templateNames[templateResponse.Index]
templateContents, err = cmdutils.LoadGitLabTemplate(cmdutils.IssueTemplate, templateName)
if err != nil {
return fmt.Errorf("failed to get template contents: %w", err)
}
}
}
}
if opts.Title == "" {
err = prompt.AskQuestionWithInput(&opts.Title, "Title", "", true)
if err != nil {
return err
}
}
if opts.Description == "" {
if opts.NoEditor {
err = prompt.AskMultiline(&opts.Description, "Description:", "")
if err != nil {
return err
}
} else {
editor, err := cmdutils.GetEditor(opts.Config)
if err != nil {
return err
}
err = cmdutils.DescriptionPrompt(&opts.Description, templateContents, editor)
if err != nil {
return err
}
}
}
if len(opts.Labels) == 0 {
remotes, err := opts.Remotes()
if err != nil {
return err
}
repoRemote, err := remotes.FindByRepo(repo.RepoOwner(), repo.RepoName())
if err != nil {
return err
}
err = cmdutils.LabelsPrompt(&opts.Labels, apiClient, repoRemote)
if err != nil {
return err
}
}
} else if opts.Title == "" {
return fmt.Errorf("title can't be blank")
}
var action cmdutils.Action
// submit without prompting for non interactive mode
if !opts.IsInteractive || opts.Yes {
action = cmdutils.SubmitAction
}
if action == cmdutils.NoAction {
action, err = cmdutils.ConfirmSubmission(true)
if err != nil {
return fmt.Errorf("unable to confirm: %w", err)
}
}
if action == cmdutils.CancelAction {
fmt.Fprintln(opts.IO.StdErr, "Discarded.")
return nil
}
if action == cmdutils.PreviewAction {
return previewIssue(opts)
}
if action == cmdutils.SubmitAction {
issueCreateOpts.Title = gitlab.String(opts.Title)
issueCreateOpts.Labels = opts.Labels
issueCreateOpts.Description = &opts.Description
if opts.IsConfidential {
issueCreateOpts.Confidential = gitlab.Bool(opts.IsConfidential)
}
if opts.Weight != 0 {
issueCreateOpts.Weight = gitlab.Int(opts.Weight)
}
if opts.LinkedMR != 0 {
issueCreateOpts.MergeRequestToResolveDiscussionsOf = gitlab.Int(opts.LinkedMR)
}
if opts.MileStone != 0 {
issueCreateOpts.MilestoneID = gitlab.Int(opts.MileStone)
}
if len(opts.Assignees) > 0 {
users, err := api.UsersByNames(apiClient, opts.Assignees)
if err != nil {
return err
}
issueCreateOpts.AssigneeIDs = cmdutils.IDsFromUsers(users)
}
fmt.Fprintln(opts.IO.StdErr, "\n- Creating issue in", repo.FullName())
issue, err := api.CreateIssue(apiClient, repo.FullName(), issueCreateOpts)
if err != nil {
return err
}
fmt.Fprintln(opts.IO.StdOut, issueutils.DisplayIssue(issue))
return nil
}
return errors.New("expected to cancel, preview in browser, or submit")
}
func previewIssue(opts *CreateOpts) error {
repo, err := opts.BaseRepo()
if err != nil {
return err
}
cfg, err := opts.Config()
if err != nil {
return err
}
openURL, err := generateIssueWebURL(opts, repo)
if err != nil {
return err
}
if opts.IO.IsOutputTTY() {
fmt.Fprintf(opts.IO.StdErr, "Opening %s in your browser.\n", utils.DisplayURL(openURL))
}
browser, _ := cfg.Get(repo.RepoHost(), "browser")
return utils.OpenInBrowser(openURL, browser)
}
func generateIssueWebURL(opts *CreateOpts, repo glrepo.Interface) (string, error) {
description := opts.Description
if len(opts.Labels) > 0 {
// this uses the slash commands to add labels to the description
// See https://docs.gitlab.com/ee/user/project/quick_actions.html
// See also https://gitlab.com/gitlab-org/gitlab-foss/-/issues/19731#note_32550046
description += "\n/label "
for _, label := range opts.Labels {
description += fmt.Sprintf("~%q", label)
}
}
if len(opts.Assignees) > 0 {
// this uses the slash commands to add assignees to the description
description += fmt.Sprintf("\n/assign %s", strings.Join(opts.Assignees, ", "))
}
if opts.MileStone != 0 {
// this uses the slash commands to add milestone to the description
description += fmt.Sprintf("\n/milestone %%%d", opts.MileStone)
}
if opts.Weight != 0 {
// this uses the slash commands to add weight to the description
description += fmt.Sprintf("\n/weight %%%d", opts.Weight)
}
if opts.IsConfidential {
// this uses the slash commands to add confidential to the description
description += "\n/confidential"
}
u, err := url.Parse(opts.BaseProject.WebURL)
if err != nil {
return "", err
}
u.Path += "/-/issues/new"
u.RawQuery = fmt.Sprintf(
"utf8=✓&issue[title]=%s&issue[description]=%s",
opts.Title,
url.QueryEscape(description))
return u.String(), nil
}