X Tutup
Skip to content

Commit 6671106

Browse files
committed
WIP works, probably some title/body input edge cases
1 parent 6fde02d commit 6671106

File tree

6 files changed

+792
-673
lines changed

6 files changed

+792
-673
lines changed

pkg/cmd/issue/create/create.go

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ import (
77

88
"github.com/MakeNowJust/heredoc"
99
"github.com/cli/cli/api"
10-
"github.com/cli/cli/git"
1110
"github.com/cli/cli/internal/config"
1211
"github.com/cli/cli/internal/ghrepo"
1312
prShared "github.com/cli/cli/pkg/cmd/pr/shared"
1413
"github.com/cli/cli/pkg/cmdutil"
15-
"github.com/cli/cli/pkg/githubtemplate"
1614
"github.com/cli/cli/pkg/iostreams"
1715
"github.com/cli/cli/utils"
1816
"github.com/spf13/cobra"
@@ -101,14 +99,7 @@ func createRun(opts *CreateOptions) error {
10199
return err
102100
}
103101

104-
var nonLegacyTemplateFiles []string
105-
if opts.RootDirOverride != "" {
106-
nonLegacyTemplateFiles = githubtemplate.FindNonLegacy(opts.RootDirOverride, "ISSUE_TEMPLATE")
107-
} else if opts.RepoOverride == "" {
108-
if rootDir, err := git.ToplevelDir(); err == nil {
109-
nonLegacyTemplateFiles = githubtemplate.FindNonLegacy(rootDir, "ISSUE_TEMPLATE")
110-
}
111-
}
102+
templateFiles, legacyTemplate := prShared.FindTemplates(opts.RootDirOverride, "ISSUE_TEMPLATE")
112103

113104
isTerminal := opts.IO.IsStdoutTTY()
114105

@@ -117,14 +108,24 @@ func createRun(opts *CreateOptions) error {
117108
milestones = []string{opts.Milestone}
118109
}
119110

111+
tb := prShared.IssueMetadataState{
112+
Type: prShared.IssueMetadata,
113+
Assignees: opts.Assignees,
114+
Labels: opts.Labels,
115+
Projects: opts.Projects,
116+
Milestones: milestones,
117+
Title: opts.Title,
118+
Body: opts.Body,
119+
}
120+
120121
if opts.WebMode {
121122
openURL := ghrepo.GenerateRepoURL(baseRepo, "issues/new")
122123
if opts.Title != "" || opts.Body != "" {
123-
openURL, err = prShared.WithPrAndIssueQueryParams(openURL, opts.Title, opts.Body, opts.Assignees, opts.Labels, opts.Projects, milestones)
124+
openURL, err = prShared.WithPrAndIssueQueryParams(openURL, tb)
124125
if err != nil {
125126
return err
126127
}
127-
} else if len(nonLegacyTemplateFiles) > 1 {
128+
} else if len(templateFiles) > 1 {
128129
openURL += "/choose"
129130
}
130131
if isTerminal {
@@ -146,59 +147,65 @@ func createRun(opts *CreateOptions) error {
146147
}
147148

148149
action := prShared.SubmitAction
149-
tb := prShared.IssueMetadataState{
150-
Type: prShared.IssueMetadata,
151-
Assignees: opts.Assignees,
152-
Labels: opts.Labels,
153-
Projects: opts.Projects,
154-
Milestones: milestones,
155-
}
156-
157-
title := opts.Title
158-
body := opts.Body
159150

160151
if opts.Interactive {
161-
var legacyTemplateFile *string
162-
if opts.RepoOverride == "" {
163-
if rootDir, err := git.ToplevelDir(); err == nil {
164-
// TODO: figure out how to stub this in tests
165-
legacyTemplateFile = githubtemplate.FindLegacy(rootDir, "ISSUE_TEMPLATE")
166-
}
152+
editorCommand, err := cmdutil.DetermineEditor(opts.Config)
153+
if err != nil {
154+
return err
167155
}
168156

169-
editorCommand, err := cmdutil.DetermineEditor(opts.Config)
157+
err = prShared.TitleSurvey(&tb)
170158
if err != nil {
171159
return err
172160
}
173161

174-
err = prShared.TitleBodySurvey(opts.IO, editorCommand, &tb, apiClient, baseRepo, title, body, prShared.Defaults{}, nonLegacyTemplateFiles, legacyTemplateFile, false, repo.ViewerCanTriage())
162+
templateContent := ""
163+
164+
templateContent, err = prShared.TemplateSurvey(templateFiles, legacyTemplate, tb)
175165
if err != nil {
176-
return fmt.Errorf("could not collect title and/or body: %w", err)
166+
return err
177167
}
178168

179-
action = tb.Action
169+
err = prShared.BodySurvey(&tb, templateContent, editorCommand)
170+
if err != nil {
171+
return err
172+
}
180173

181-
if tb.Action == prShared.CancelAction {
182-
fmt.Fprintln(opts.IO.ErrOut, "Discarding.")
174+
if tb.Body == "" {
175+
tb.Body = templateContent
176+
}
183177

184-
return nil
178+
action, err := prShared.ConfirmSubmission(!tb.HasMetadata(), repo.ViewerCanTriage())
179+
if err != nil {
180+
return fmt.Errorf("unable to confirm: %w", err)
185181
}
186182

187-
if title == "" {
188-
title = tb.Title
183+
if action == prShared.MetadataAction {
184+
err = prShared.MetadataSurvey(opts.IO, apiClient, baseRepo, &tb)
185+
if err != nil {
186+
return err
187+
}
188+
189+
action, err = prShared.ConfirmSubmission(!tb.HasMetadata(), false)
190+
if err != nil {
191+
return err
192+
}
189193
}
190-
if body == "" {
191-
body = tb.Body
194+
195+
if action == prShared.CancelAction {
196+
fmt.Fprintln(opts.IO.ErrOut, "Discarding.")
197+
198+
return nil
192199
}
193200
} else {
194-
if title == "" {
201+
if tb.Title == "" {
195202
return fmt.Errorf("title can't be blank")
196203
}
197204
}
198205

199206
if action == prShared.PreviewAction {
200207
openURL := ghrepo.GenerateRepoURL(baseRepo, "issues/new")
201-
openURL, err = prShared.WithPrAndIssueQueryParams(openURL, title, body, tb.Assignees, tb.Labels, tb.Projects, tb.Milestones)
208+
openURL, err = prShared.WithPrAndIssueQueryParams(openURL, tb)
202209
if err != nil {
203210
return err
204211
}
@@ -208,8 +215,8 @@ func createRun(opts *CreateOptions) error {
208215
return utils.OpenInBrowser(openURL)
209216
} else if action == prShared.SubmitAction {
210217
params := map[string]interface{}{
211-
"title": title,
212-
"body": body,
218+
"title": tb.Title,
219+
"body": tb.Body,
213220
}
214221

215222
err = prShared.AddMetadataToIssueParams(apiClient, baseRepo, params, &tb)

0 commit comments

Comments
 (0)
X Tutup