Secret base repo prompting should not use resolved remote

This is because the secret commands don't use the SmartBaseRepo behaviour, and therefore
don't care about the resolved remote.
This commit is contained in:
William Martin 2025-01-15 14:11:24 +01:00
parent ce47fabc27
commit a47327aee6
2 changed files with 16 additions and 38 deletions

View file

@ -28,10 +28,22 @@ func PromptWhenAmbiguousBaseRepoFunc(baseRepoFn baseRepoFn, prompter prompter.Pr
return nil, err
}
baseRepo, err = promptForRepo(baseRepo, ambiguousBaseRepoErr.Remotes, prompter)
baseRepoOptions := make([]string, len(ambiguousBaseRepoErr.Remotes))
for i, remote := range ambiguousBaseRepoErr.Remotes {
baseRepoOptions[i] = ghrepo.FullName(remote)
}
selectedBaseRepo, err := prompter.Select("Select a base repo", baseRepoOptions[0], baseRepoOptions)
if err != nil {
return nil, err
}
selectedRepo, err := ghrepo.FromFullName(baseRepoOptions[selectedBaseRepo])
if err != nil {
return nil, err
}
return selectedRepo, nil
}
return baseRepo, nil
@ -54,35 +66,3 @@ func RequireNoAmbiguityBaseRepoFunc(baseRepo baseRepoFn, remotes remotesFn) base
return baseRepo()
}
}
// REVIEW WARNING: I have not had a close look at this function yet, I do not vouch for it.
func promptForRepo(baseRepo ghrepo.Interface, remotes ghContext.Remotes, prompter prompter.Prompter) (ghrepo.Interface, error) {
var defaultRepo string
var remoteArray []string
// TODO: consider whether we should just go with the default order of remotes because then
// users that are familiar can just hit enter and achieve the behaviour they had before.
if defaultRemote, _ := remotes.ResolvedRemote(); defaultRemote != nil {
// this is a remote explicitly chosen via `repo set-default`
defaultRepo = ghrepo.FullName(defaultRemote)
} else if len(remotes) > 0 {
// as a fallback, just pick the first remote
defaultRepo = ghrepo.FullName(remotes[0])
}
for _, remote := range remotes {
remoteArray = append(remoteArray, ghrepo.FullName(remote))
}
baseRepoInput, errInput := prompter.Select("Select a base repo", defaultRepo, remoteArray)
if errInput != nil {
return baseRepo, errInput
}
selectedRepo, errSelectedRepo := ghrepo.FromFullName(remoteArray[baseRepoInput])
if errSelectedRepo != nil {
return baseRepo, errSelectedRepo
}
return selectedRepo, nil
}

View file

@ -102,7 +102,7 @@ func TestPromptWhenMultipleRemotesBaseRepoFunc(t *testing.T) {
require.True(t, ghrepo.IsSame(ghrepo.New("owner", "repo"), baseRepo))
})
t.Run("when the wrapped base repo func returns a specific error, then the prompter is used for disambiguation, with the resolved remote as the default", func(t *testing.T) {
t.Run("when the wrapped base repo func returns a specific error, then the prompter is used for disambiguation, with the remote ordering remaining unchanged", func(t *testing.T) {
t.Parallel()
pm := prompter.NewMockPrompter(t)
@ -110,8 +110,7 @@ func TestPromptWhenMultipleRemotesBaseRepoFunc(t *testing.T) {
"Select a base repo",
[]string{"owner/fork", "owner/repo"},
func(_, def string, opts []string) (int, error) {
t.Helper()
require.Equal(t, "owner/repo", def)
require.Equal(t, "owner/fork", def)
return prompter.IndexFor(opts, "owner/repo")
},
)
@ -179,8 +178,7 @@ func errMultipleRemotesStubFn() (ghrepo.Interface, error) {
remote2 := &ghContext.Remote{
Remote: &git.Remote{
Name: "upstream",
Resolved: "base",
Name: "upstream",
},
Repo: ghrepo.New("owner", "repo"),
}