Skip to content

Commit 9a20719

Browse files
author
Nate Smith
authored
Merge pull request cli#2386 from cli/create-refactor
Refactor pr/issue creation code
2 parents fed4df2 + a686455 commit 9a20719

File tree

10 files changed

+825
-750
lines changed

10 files changed

+825
-750
lines changed

pkg/cmd/issue/create/create.go

Lines changed: 56 additions & 45 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,69 @@ 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-
}
167-
}
168-
169152
editorCommand, err := cmdutil.DetermineEditor(opts.Config)
170153
if err != nil {
171154
return err
172155
}
173156

174-
err = prShared.TitleBodySurvey(opts.IO, editorCommand, &tb, apiClient, baseRepo, title, body, prShared.Defaults{}, nonLegacyTemplateFiles, legacyTemplateFile, false, repo.ViewerCanTriage())
175-
if err != nil {
176-
return fmt.Errorf("could not collect title and/or body: %w", err)
157+
if tb.Title == "" {
158+
err = prShared.TitleSurvey(&tb)
159+
if err != nil {
160+
return err
161+
}
177162
}
178163

179-
action = tb.Action
164+
if tb.Body == "" {
165+
templateContent := ""
180166

181-
if tb.Action == prShared.CancelAction {
182-
fmt.Fprintln(opts.IO.ErrOut, "Discarding.")
167+
templateContent, err = prShared.TemplateSurvey(templateFiles, legacyTemplate, tb)
168+
if err != nil {
169+
return err
170+
}
183171

184-
return nil
172+
err = prShared.BodySurvey(&tb, templateContent, editorCommand)
173+
if err != nil {
174+
return err
175+
}
176+
177+
if tb.Body == "" {
178+
tb.Body = templateContent
179+
}
180+
}
181+
182+
action, err := prShared.ConfirmSubmission(!tb.HasMetadata(), repo.ViewerCanTriage())
183+
if err != nil {
184+
return fmt.Errorf("unable to confirm: %w", err)
185185
}
186186

187-
if title == "" {
188-
title = tb.Title
187+
if action == prShared.MetadataAction {
188+
err = prShared.MetadataSurvey(opts.IO, apiClient, baseRepo, &tb)
189+
if err != nil {
190+
return err
191+
}
192+
193+
action, err = prShared.ConfirmSubmission(!tb.HasMetadata(), false)
194+
if err != nil {
195+
return err
196+
}
189197
}
190-
if body == "" {
191-
body = tb.Body
198+
199+
if action == prShared.CancelAction {
200+
fmt.Fprintln(opts.IO.ErrOut, "Discarding.")
201+
202+
return nil
192203
}
193204
} else {
194-
if title == "" {
205+
if tb.Title == "" {
195206
return fmt.Errorf("title can't be blank")
196207
}
197208
}
198209

199210
if action == prShared.PreviewAction {
200211
openURL := ghrepo.GenerateRepoURL(baseRepo, "issues/new")
201-
openURL, err = prShared.WithPrAndIssueQueryParams(openURL, title, body, tb.Assignees, tb.Labels, tb.Projects, tb.Milestones)
212+
openURL, err = prShared.WithPrAndIssueQueryParams(openURL, tb)
202213
if err != nil {
203214
return err
204215
}
@@ -208,8 +219,8 @@ func createRun(opts *CreateOptions) error {
208219
return utils.OpenInBrowser(openURL)
209220
} else if action == prShared.SubmitAction {
210221
params := map[string]interface{}{
211-
"title": title,
212-
"body": body,
222+
"title": tb.Title,
223+
"body": tb.Body,
213224
}
214225

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

pkg/cmd/issue/create/create_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,17 @@ func TestIssueCreate_nonLegacyTemplate(t *testing.T) {
144144

145145
as, teardown := prompt.InitAskStubber()
146146
defer teardown()
147+
148+
// tmeplate
147149
as.Stub([]*prompt.QuestionStub{
148150
{
149151
Name: "index",
150152
Value: 1,
151153
},
152154
})
153-
as.Stub([]*prompt.QuestionStub{
154-
{
155-
Name: "body",
156-
Default: true,
157-
},
158-
})
155+
// body
156+
as.StubOneDefault()
157+
// confirm
159158
as.Stub([]*prompt.QuestionStub{
160159
{
161160
Name: "confirmation",

0 commit comments

Comments
 (0)