use Prompter in template manager

This commit is contained in:
vilmibm 2022-10-17 17:23:47 -07:00
parent a3b1bb7fb2
commit 47eebe0c64
6 changed files with 45 additions and 54 deletions

View file

@ -154,7 +154,7 @@ func createRun(opts *CreateOptions) (err error) {
}
}
tpl := shared.NewTemplateManager(httpClient, baseRepo, opts.RootDirOverride, !opts.HasRepoOverride, false)
tpl := shared.NewTemplateManager(httpClient, baseRepo, opts.Prompter, opts.RootDirOverride, !opts.HasRepoOverride, false)
if opts.WebMode {
var openURL string

View file

@ -21,7 +21,6 @@ import (
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/cli/v2/pkg/prompt"
"github.com/cli/cli/v2/test"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
@ -489,12 +488,6 @@ func TestIssueCreate_nonLegacyTemplate(t *testing.T) {
}),
)
//nolint:staticcheck // SA1019: prompt.NewAskStubber is deprecated: use PrompterMock
as := prompt.NewAskStubber(t)
// TODO fix
as.StubPrompt("Choose a template").AnswerWith("Submit a request")
pm := &prompter.PrompterMock{}
pm.MarkdownEditorFunc = func(p, d string, ba bool) (string, error) {
if p == "Body" {
@ -504,9 +497,12 @@ func TestIssueCreate_nonLegacyTemplate(t *testing.T) {
}
}
pm.SelectFunc = func(p, _ string, opts []string) (int, error) {
if p == "What's next?" {
switch p {
case "What's next?":
return prompter.IndexFor(opts, "Submit")
} else {
case "Choose a template":
return prompter.IndexFor(opts, "Submit a request")
default:
return -1, prompter.NoSuchPromptErr(p)
}
}

View file

@ -281,7 +281,7 @@ func createRun(opts *CreateOptions) (err error) {
if !opts.BodyProvided {
templateContent := ""
if opts.RecoverFile == "" {
tpl := shared.NewTemplateManager(client.HTTP(), ctx.BaseRepo, opts.RootDirOverride, opts.RepoOverride == "", true)
tpl := shared.NewTemplateManager(client.HTTP(), ctx.BaseRepo, opts.Prompter, opts.RootDirOverride, opts.RepoOverride == "", true)
var template shared.Template
template, err = tpl.Choose()
if err != nil {

View file

@ -22,7 +22,6 @@ import (
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/cli/v2/pkg/prompt"
"github.com/cli/cli/v2/test"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
@ -188,7 +187,6 @@ func Test_createRun(t *testing.T) {
name string
setup func(*CreateOptions, *testing.T) func()
cmdStubs func(*run.CommandStubber)
askStubs func(*prompt.AskStubber) // TODO eventually migrate to PrompterMock
promptStubs func(*prompter.PrompterMock)
httpStubs func(*httpmock.Registry, *testing.T)
expectedOut string
@ -500,11 +498,6 @@ func Test_createRun(t *testing.T) {
cmdStubs: func(cs *run.CommandStubber) {
cs.Register(`git( .+)? log( .+)? origin/master\.\.\.feature`, 0, "1234567890,commit 0\n2345678901,commit 1")
},
askStubs: func(as *prompt.AskStubber) {
as.StubPrompt("Choose a template").
AssertOptions([]string{"template1", "template2", "Open a blank pull request"}).
AnswerWith("template1")
},
promptStubs: func(pm *prompter.PrompterMock) {
pm.MarkdownEditorFunc = func(p, d string, ba bool) (string, error) {
if p == "Body" {
@ -514,9 +507,12 @@ func Test_createRun(t *testing.T) {
}
}
pm.SelectFunc = func(p, _ string, opts []string) (int, error) {
if p == "What's next?" {
switch p {
case "What's next?":
return 0, nil
} else {
case "Choose a template":
return prompter.IndexFor(opts, "template1")
default:
return -1, prompter.NoSuchPromptErr(p)
}
}
@ -765,9 +761,6 @@ func Test_createRun(t *testing.T) {
cs.Register(`git -c log.ShowSignature=false log --pretty=format:%H,%s --cherry origin/master...feature`, 0, "")
cs.Register(`git rev-parse --show-toplevel`, 0, "")
},
askStubs: func(as *prompt.AskStubber) {
as.StubPrompt("Choose a template").AnswerDefault()
},
promptStubs: func(pm *prompter.PrompterMock) {
pm.MarkdownEditorFunc = func(p, d string, ba bool) (string, error) {
if p == "Body" {
@ -777,9 +770,12 @@ func Test_createRun(t *testing.T) {
}
}
pm.SelectFunc = func(p, _ string, opts []string) (int, error) {
if p == "What's next?" {
switch p {
case "What's next?":
return prompter.IndexFor(opts, "Submit as draft")
} else {
case "Choose a template":
return 0, nil
default:
return -1, prompter.NoSuchPromptErr(p)
}
}
@ -893,13 +889,6 @@ func Test_createRun(t *testing.T) {
tt.httpStubs(reg, t)
}
//nolint:staticcheck // SA1019: prompt.InitAskStubber is deprecated: use NewAskStubber
ask, cleanupAsk := prompt.InitAskStubber()
defer cleanupAsk()
if tt.askStubs != nil {
tt.askStubs(ask)
}
pm := &prompter.PrompterMock{}
if tt.promptStubs != nil {

View file

@ -6,13 +6,11 @@ import (
"net/http"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/git"
fd "github.com/cli/cli/v2/internal/featuredetection"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/githubtemplate"
"github.com/cli/cli/v2/pkg/prompt"
"github.com/shurcooL/githubv4"
)
@ -112,6 +110,10 @@ type Template interface {
Body() []byte
}
type iprompter interface {
Select(string, string, []string) (int, error)
}
type templateManager struct {
repo ghrepo.Interface
rootDir string
@ -119,6 +121,7 @@ type templateManager struct {
isPR bool
httpClient *http.Client
detector fd.Detector
prompter iprompter
templates []Template
legacyTemplate Template
@ -127,7 +130,7 @@ type templateManager struct {
fetchError error
}
func NewTemplateManager(httpClient *http.Client, repo ghrepo.Interface, dir string, allowFS bool, isPR bool) *templateManager {
func NewTemplateManager(httpClient *http.Client, repo ghrepo.Interface, p iprompter, dir string, allowFS bool, isPR bool) *templateManager {
cachedClient := api.NewCachedHTTPClient(httpClient, time.Hour*24)
return &templateManager{
repo: repo,
@ -135,6 +138,7 @@ func NewTemplateManager(httpClient *http.Client, repo ghrepo.Interface, dir stri
allowFS: allowFS,
isPR: isPR,
httpClient: httpClient,
prompter: p,
detector: fd.NewDetector(cachedClient, repo.RepoHost()),
}
}
@ -184,12 +188,7 @@ func (m *templateManager) Choose() (Template, error) {
blankOption = "Open a blank pull request"
}
var selectedOption int
//nolint:staticcheck // SA1019: prompt.SurveyAskOne is deprecated: use Prompter
err := prompt.SurveyAskOne(&survey.Select{
Message: "Choose a template",
Options: append(names, blankOption),
}, &selectedOption)
selectedOption, err := m.prompter.Select("Choose a template", "", append(names, blankOption))
if err != nil {
return nil, fmt.Errorf("could not prompt: %w", err)
}

View file

@ -8,8 +8,8 @@ import (
fd "github.com/cli/cli/v2/internal/featuredetection"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/prompter"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/prompt"
"github.com/stretchr/testify/assert"
)
@ -32,6 +32,15 @@ func TestTemplateManager_hasAPI(t *testing.T) {
]
}}}`))
pm := &prompter.PrompterMock{}
pm.SelectFunc = func(p, _ string, opts []string) (int, error) {
if p == "Choose a template" {
return prompter.IndexFor(opts, "Feature request")
} else {
return -1, prompter.NoSuchPromptErr(p)
}
}
m := templateManager{
repo: ghrepo.NewWithHost("OWNER", "REPO", "example.com"),
rootDir: rootDir,
@ -39,6 +48,7 @@ func TestTemplateManager_hasAPI(t *testing.T) {
isPR: false,
httpClient: httpClient,
detector: &fd.EnabledDetectorMock{},
prompter: pm,
}
hasTemplates, err := m.HasTemplates()
@ -47,12 +57,6 @@ func TestTemplateManager_hasAPI(t *testing.T) {
assert.Equal(t, "LEGACY", string(m.LegacyBody()))
//nolint:staticcheck // SA1019: prompt.NewAskStubber is deprecated: use PrompterMock
as := prompt.NewAskStubber(t)
as.StubPrompt("Choose a template").
AssertOptions([]string{"Bug report", "Feature request", "Open a blank issue"}).
AnswerWith("Feature request")
tpl, err := m.Choose()
assert.NoError(t, err)
@ -79,6 +83,14 @@ func TestTemplateManager_hasAPI_PullRequest(t *testing.T) {
]
}}}`))
pm := &prompter.PrompterMock{}
pm.SelectFunc = func(p, _ string, opts []string) (int, error) {
if p == "Choose a template" {
return prompter.IndexFor(opts, "bug_pr.md")
} else {
return -1, prompter.NoSuchPromptErr(p)
}
}
m := templateManager{
repo: ghrepo.NewWithHost("OWNER", "REPO", "example.com"),
rootDir: rootDir,
@ -86,6 +98,7 @@ func TestTemplateManager_hasAPI_PullRequest(t *testing.T) {
isPR: true,
httpClient: httpClient,
detector: &fd.EnabledDetectorMock{},
prompter: pm,
}
hasTemplates, err := m.HasTemplates()
@ -94,12 +107,6 @@ func TestTemplateManager_hasAPI_PullRequest(t *testing.T) {
assert.Equal(t, "LEGACY", string(m.LegacyBody()))
//nolint:staticcheck // SA1019: prompt.NewAskStubber is deprecated: use PrompterMock
as := prompt.NewAskStubber(t)
as.StubPrompt("Choose a template").
AssertOptions([]string{"bug_pr.md", "feature_pr.md", "Open a blank pull request"}).
AnswerWith("bug_pr.md")
tpl, err := m.Choose()
assert.NoError(t, err)