use MultiSelect for metadata survey in pr, issue create

This commit is contained in:
Nate Smith 2023-08-17 14:43:08 -05:00
parent f04e3398ed
commit a3539d4f24
4 changed files with 27 additions and 30 deletions

View file

@ -268,7 +268,7 @@ func createRun(opts *CreateOptions) (err error) {
Repo: baseRepo,
State: &tb,
}
err = prShared.MetadataSurvey(opts.IO, baseRepo, fetcher, &tb)
err = prShared.MetadataSurvey(opts.Prompter, opts.IO, baseRepo, fetcher, &tb)
if err != nil {
return
}

View file

@ -361,7 +361,7 @@ func createRun(opts *CreateOptions) (err error) {
Repo: ctx.BaseRepo,
State: state,
}
err = shared.MetadataSurvey(opts.IO, ctx.BaseRepo, fetcher, state)
err = shared.MetadataSurvey(opts.Prompter, opts.IO, ctx.BaseRepo, fetcher, state)
if err != nil {
return
}

View file

@ -37,6 +37,7 @@ type Prompt interface {
Select(string, string, []string) (int, error)
MarkdownEditor(string, string, bool) (string, error)
Confirm(string, bool) (bool, error)
MultiSelect(string, []string, []string) ([]int, error)
}
func ConfirmIssueSubmission(p Prompt, allowPreview bool, allowMetadata bool) (Action, error) {
@ -142,7 +143,7 @@ type RepoMetadataFetcher interface {
RepoMetadataFetch(api.RepoMetadataInput) (*api.RepoMetadataResult, error)
}
func MetadataSurvey(io *iostreams.IOStreams, baseRepo ghrepo.Interface, fetcher RepoMetadataFetcher, state *IssueMetadataState) error {
func MetadataSurvey(p Prompt, io *iostreams.IOStreams, baseRepo ghrepo.Interface, fetcher RepoMetadataFetcher, state *IssueMetadataState) error {
isChosen := func(m string) bool {
for _, c := range state.Metadata {
if m == c {
@ -160,18 +161,12 @@ func MetadataSurvey(io *iostreams.IOStreams, baseRepo ghrepo.Interface, fetcher
}
extraFieldsOptions = append(extraFieldsOptions, "Assignees", "Labels", "Projects", "Milestone")
//nolint:staticcheck // SA1019: prompt.SurveyAsk is deprecated: use Prompter
err := prompt.SurveyAsk([]*survey.Question{
{
Name: "metadata",
Prompt: &survey.MultiSelect{
Message: "What would you like to add?",
Options: extraFieldsOptions,
},
},
}, state)
selected, err := p.MultiSelect("What would you like to add?", nil, extraFieldsOptions)
if err != nil {
return fmt.Errorf("could not prompt: %w", err)
return err
}
for _, i := range selected {
state.Metadata = append(state.Metadata, extraFieldsOptions[i])
}
metadataInput := api.RepoMetadataInput{

View file

@ -5,6 +5,7 @@ import (
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/prompter"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/cli/v2/pkg/prompt"
"github.com/stretchr/testify/assert"
@ -43,17 +44,17 @@ func TestMetadataSurvey_selectAll(t *testing.T) {
},
}
pm := prompter.NewMockPrompter(t)
pm.RegisterMultiSelect("What would you like to add?",
[]string{}, []string{"Reviewers", "Assignees", "Labels", "Projects", "Milestone"}, func(_ string, _, _ []string) ([]int, error) {
// []string{"Labels", "Projects", "Assignees", "Reviewers", "Milestone"},
return []int{0, 1, 2, 3, 4}, nil
})
//nolint:staticcheck // SA1019: prompt.InitAskStubber is deprecated: use NewAskStubber
as, restoreAsk := prompt.InitAskStubber()
defer restoreAsk()
//nolint:staticcheck // SA1019: as.Stub is deprecated: use StubPrompt
as.Stub([]*prompt.QuestionStub{
{
Name: "metadata",
Value: []string{"Labels", "Projects", "Assignees", "Reviewers", "Milestone"},
},
})
//nolint:staticcheck // SA1019: as.Stub is deprecated: use StubPrompt
as.Stub([]*prompt.QuestionStub{
{
@ -80,8 +81,9 @@ func TestMetadataSurvey_selectAll(t *testing.T) {
state := &IssueMetadataState{
Assignees: []string{"hubot"},
Type: PRMetadata,
}
err := MetadataSurvey(ios, repo, fetcher, state)
err := MetadataSurvey(pm, ios, repo, fetcher, state)
assert.NoError(t, err)
assert.Equal(t, "", stdout.String())
@ -112,17 +114,17 @@ func TestMetadataSurvey_keepExisting(t *testing.T) {
},
}
pm := prompter.NewMockPrompter(t)
pm.RegisterMultiSelect("What would you like to add?", []string{}, []string{"Assignees", "Labels", "Projects", "Milestone"}, func(_ string, _, _ []string) ([]int, error) {
return []int{1, 2}, nil
})
//nolint:staticcheck // SA1019: prompt.InitAskStubber is deprecated: use NewAskStubber
as, restoreAsk := prompt.InitAskStubber()
defer restoreAsk()
//nolint:staticcheck // SA1019: as.Stub is deprecated: use StubPrompt
as.Stub([]*prompt.QuestionStub{
{
Name: "metadata",
Value: []string{"Labels", "Projects"},
},
})
//nolint:staticcheck // SA1019: as.Stub is deprecated: use StubPrompt
as.Stub([]*prompt.QuestionStub{
{
@ -138,7 +140,7 @@ func TestMetadataSurvey_keepExisting(t *testing.T) {
state := &IssueMetadataState{
Assignees: []string{"hubot"},
}
err := MetadataSurvey(ios, repo, fetcher, state)
err := MetadataSurvey(pm, ios, repo, fetcher, state)
assert.NoError(t, err)
assert.Equal(t, "", stdout.String())