Rename secret BaseRepo func

This commit is contained in:
William Martin 2024-12-20 14:33:47 +01:00
parent 870da79886
commit 4da4c82090
9 changed files with 23 additions and 23 deletions

View file

@ -69,6 +69,7 @@ func (rr *remoteResolver) Resolver() func() (context.Remotes, error) {
sort.Sort(resolvedRemotes)
// Filter remotes by hosts
// Note that this is not caching correctly: https://github.com/cli/cli/issues/10103
cachedRemotes := resolvedRemotes.FilterByHosts(hosts)
// Filter again by default host if one is set

View file

@ -56,7 +56,7 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
// But if we are able to prompt, then we will wrap that up in a BaseRepoFunc that can prompt the user to
// resolve the ambiguity.
if opts.IO.CanPrompt() {
opts.BaseRepo = shared.PromptWhenMultipleRemotesBaseRepoFunc(opts.BaseRepo, f.Prompter)
opts.BaseRepo = shared.PromptWhenAmbiguousBaseRepoFunc(opts.BaseRepo, f.Prompter)
}
}

View file

@ -156,7 +156,7 @@ func TestNewCmdDeleteBaseRepoFuncs(t *testing.T) {
{
name: "when there is no repo flag provided, and no prompting, the base func requiring no ambiguity is used",
args: "SECRET_NAME",
wantErr: shared.MultipleRemotesError{
wantErr: shared.AmbiguousBaseRepoError{
Remotes: remotes,
},
},

View file

@ -77,7 +77,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
// But if we are able to prompt, then we will wrap that up in a BaseRepoFunc that can prompt the user to
// resolve the ambiguity.
if opts.IO.CanPrompt() {
opts.BaseRepo = shared.PromptWhenMultipleRemotesBaseRepoFunc(opts.BaseRepo, f.Prompter)
opts.BaseRepo = shared.PromptWhenAmbiguousBaseRepoFunc(opts.BaseRepo, f.Prompter)
}
}

View file

@ -136,7 +136,7 @@ func TestNewCmdListBaseRepoFuncs(t *testing.T) {
{
name: "when there is no repo flag provided, and no prompting, the base func requiring no ambiguity is used",
args: "",
wantErr: shared.MultipleRemotesError{
wantErr: shared.AmbiguousBaseRepoError{
Remotes: remotes,
},
},

View file

@ -114,7 +114,7 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
// But if we are able to prompt, then we will wrap that up in a BaseRepoFunc that can prompt the user to
// resolve the ambiguity.
if opts.IO.CanPrompt() {
opts.BaseRepo = shared.PromptWhenMultipleRemotesBaseRepoFunc(opts.BaseRepo, f.Prompter)
opts.BaseRepo = shared.PromptWhenAmbiguousBaseRepoFunc(opts.BaseRepo, f.Prompter)
}
}

View file

@ -255,7 +255,7 @@ func TestNewCmdSetBaseRepoFuncs(t *testing.T) {
{
name: "when there is no repo flag provided, and no prompting, the base func requiring no ambiguity is used",
args: "SECRET_NAME",
wantErr: shared.MultipleRemotesError{
wantErr: shared.AmbiguousBaseRepoError{
Remotes: remotes,
},
},

View file

@ -8,28 +8,27 @@ import (
"github.com/cli/cli/v2/internal/prompter"
)
type MultipleRemotesError struct {
type AmbiguousBaseRepoError struct {
Remotes ghContext.Remotes
}
func (e MultipleRemotesError) Error() string {
func (e AmbiguousBaseRepoError) Error() string {
return "multiple remotes detected. please specify which repo to use by providing the -R or --repo argument"
}
type baseRepoFn func() (ghrepo.Interface, error)
type remotesFn func() (ghContext.Remotes, error)
func PromptWhenMultipleRemotesBaseRepoFunc(baseRepoFn baseRepoFn, prompter prompter.Prompter) baseRepoFn {
func PromptWhenAmbiguousBaseRepoFunc(baseRepoFn baseRepoFn, prompter prompter.Prompter) baseRepoFn {
return func() (ghrepo.Interface, error) {
baseRepo, err := baseRepoFn()
if err != nil {
var multipleRemotesError MultipleRemotesError
if !errors.As(err, &multipleRemotesError) {
var ambiguousBaseRepoErr AmbiguousBaseRepoError
if !errors.As(err, &ambiguousBaseRepoErr) {
return nil, err
}
// prompt for the base repo
baseRepo, err = promptForRepo(baseRepo, multipleRemotesError.Remotes, prompter)
baseRepo, err = promptForRepo(baseRepo, ambiguousBaseRepoErr.Remotes, prompter)
if err != nil {
return nil, err
}
@ -40,7 +39,7 @@ func PromptWhenMultipleRemotesBaseRepoFunc(baseRepoFn baseRepoFn, prompter promp
}
// RequireNoAmbiguityBaseRepoFunc returns a function to resolve the base repo, ensuring that
// there was only one remote.
// there was only one option, regardless of whether the base repo had been set.
func RequireNoAmbiguityBaseRepoFunc(baseRepo baseRepoFn, remotes remotesFn) baseRepoFn {
return func() (ghrepo.Interface, error) {
// TODO: Is this really correct? Some remotes may not be in the same network. We probably need to resolve the
@ -51,7 +50,7 @@ func RequireNoAmbiguityBaseRepoFunc(baseRepo baseRepoFn, remotes remotesFn) base
}
if remotes.Len() > 1 {
return nil, MultipleRemotesError{Remotes: remotes}
return nil, AmbiguousBaseRepoError{Remotes: remotes}
}
return baseRepo()

View file

@ -40,7 +40,7 @@ func TestRequireNoAmbiguityBaseRepoFunc(t *testing.T) {
_, err := baseRepoFn()
// It succeeds and returns the inner base repo
var multipleRemotesError shared.MultipleRemotesError
var multipleRemotesError shared.AmbiguousBaseRepoError
require.ErrorAs(t, err, &multipleRemotesError)
require.Equal(t, ghContext.Remotes{
{
@ -92,7 +92,7 @@ func TestPromptWhenMultipleRemotesBaseRepoFunc(t *testing.T) {
t.Parallel()
// Given the base repo function succeeds
baseRepoFn := shared.PromptWhenMultipleRemotesBaseRepoFunc(baseRepoStubFn, nil)
baseRepoFn := shared.PromptWhenAmbiguousBaseRepoFunc(baseRepoStubFn, nil)
// When fetching the base repo
baseRepo, err := baseRepoFn()
@ -117,7 +117,7 @@ func TestPromptWhenMultipleRemotesBaseRepoFunc(t *testing.T) {
)
// Given the wrapped base repo func returns a specific error
baseRepoFn := shared.PromptWhenMultipleRemotesBaseRepoFunc(errMultipleRemotesStubFn, pm)
baseRepoFn := shared.PromptWhenAmbiguousBaseRepoFunc(errMultipleRemotesStubFn, pm)
// When fetching the base repo
baseRepo, err := baseRepoFn()
@ -141,7 +141,7 @@ func TestPromptWhenMultipleRemotesBaseRepoFunc(t *testing.T) {
)
// Given the wrapped base repo func returns a specific error
baseRepoFn := shared.PromptWhenMultipleRemotesBaseRepoFunc(errMultipleRemotesStubFn, pm)
baseRepoFn := shared.PromptWhenAmbiguousBaseRepoFunc(errMultipleRemotesStubFn, pm)
// When fetching the base repo
_, err := baseRepoFn()
@ -154,7 +154,7 @@ func TestPromptWhenMultipleRemotesBaseRepoFunc(t *testing.T) {
t.Parallel()
// Given the wrapped base repo func returns a non-specific error
baseRepoFn := shared.PromptWhenMultipleRemotesBaseRepoFunc(errBaseRepoStubFn, nil)
baseRepoFn := shared.PromptWhenAmbiguousBaseRepoFunc(errBaseRepoStubFn, nil)
// When fetching the base repo
_, err := baseRepoFn()
@ -165,8 +165,8 @@ func TestPromptWhenMultipleRemotesBaseRepoFunc(t *testing.T) {
}
func TestMultipleRemotesErrorMessage(t *testing.T) {
err := shared.MultipleRemotesError{}
require.EqualError(t, err, "multiple remotes detected. please specify which repo to use by providing the -R or --repo argument")
err := shared.AmbiguousBaseRepoError{}
require.EqualError(t, err, "multiple remotes detected. please specify which repo to use by providing the -R, --repo argument")
}
func errMultipleRemotesStubFn() (ghrepo.Interface, error) {
@ -185,7 +185,7 @@ func errMultipleRemotesStubFn() (ghrepo.Interface, error) {
Repo: ghrepo.New("owner", "repo"),
}
return nil, shared.MultipleRemotesError{
return nil, shared.AmbiguousBaseRepoError{
Remotes: ghContext.Remotes{
remote1,
remote2,