From 78fb90956c71aeefbb47b4e8382abe47992b5257 Mon Sep 17 00:00:00 2001 From: Robin Neatherway Date: Tue, 7 Feb 2023 20:05:38 +0000 Subject: [PATCH] Teach gh cs create to use current repo as default (#6596) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mislav Marohnić --- pkg/cmd/codespace/code_test.go | 2 +- pkg/cmd/codespace/common.go | 5 ++++- pkg/cmd/codespace/create.go | 13 +++++++++++++ pkg/cmd/codespace/delete_test.go | 2 +- pkg/cmd/codespace/edit_test.go | 2 +- pkg/cmd/codespace/logs_test.go | 2 +- pkg/cmd/codespace/ports_test.go | 2 +- pkg/cmd/codespace/rebuild_test.go | 2 +- pkg/cmd/codespace/select_test.go | 2 +- pkg/cmd/codespace/ssh_test.go | 2 +- pkg/cmd/root/root.go | 1 + 11 files changed, 26 insertions(+), 9 deletions(-) diff --git a/pkg/cmd/codespace/code_test.go b/pkg/cmd/codespace/code_test.go index 26aa05d4c..f43d8a20c 100644 --- a/pkg/cmd/codespace/code_test.go +++ b/pkg/cmd/codespace/code_test.go @@ -97,7 +97,7 @@ func TestPendingOperationDisallowsCode(t *testing.T) { func testingCodeApp() *App { ios, _, _, _ := iostreams.Test() - return NewApp(ios, nil, testCodeApiMock(), nil) + return NewApp(ios, nil, testCodeApiMock(), nil, nil) } func testCodeApiMock() *apiClientMock { diff --git a/pkg/cmd/codespace/common.go b/pkg/cmd/codespace/common.go index 5c5c28551..24235ed27 100644 --- a/pkg/cmd/codespace/common.go +++ b/pkg/cmd/codespace/common.go @@ -13,6 +13,7 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2/terminal" + clicontext "github.com/cli/cli/v2/context" "github.com/cli/cli/v2/internal/browser" "github.com/cli/cli/v2/internal/codespaces" "github.com/cli/cli/v2/internal/codespaces/api" @@ -32,9 +33,10 @@ type App struct { errLogger *log.Logger executable executable browser browser.Browser + remotes func() (clicontext.Remotes, error) } -func NewApp(io *iostreams.IOStreams, exe executable, apiClient apiClient, browser browser.Browser) *App { +func NewApp(io *iostreams.IOStreams, exe executable, apiClient apiClient, browser browser.Browser, remotes func() (clicontext.Remotes, error)) *App { errLogger := log.New(io.ErrOut, "", 0) return &App{ @@ -43,6 +45,7 @@ func NewApp(io *iostreams.IOStreams, exe executable, apiClient apiClient, browse errLogger: errLogger, executable: exe, browser: browser, + remotes: remotes, } } diff --git a/pkg/cmd/codespace/create.go b/pkg/cmd/codespace/create.go index a1e353872..a387aade1 100644 --- a/pkg/cmd/codespace/create.go +++ b/pkg/cmd/codespace/create.go @@ -10,6 +10,7 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/cli/cli/v2/internal/codespaces" "github.com/cli/cli/v2/internal/codespaces/api" + "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/internal/text" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/spf13/cobra" @@ -119,12 +120,24 @@ func (a *App) Create(ctx context.Context, opts createOptions) error { promptForRepoAndBranch := userInputs.Repository == "" if promptForRepoAndBranch { + var defaultRepo string + if remotes, _ := a.remotes(); remotes != nil { + 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]) + } + } + repoQuestions := []*survey.Question{ { Name: "repository", Prompt: &survey.Input{ Message: "Repository:", Help: "Search for repos by name. To search within an org or user, or to see private repos, enter at least ':user/'.", + Default: defaultRepo, Suggest: func(toComplete string) []string { return getRepoSuggestions(ctx, a.apiClient, toComplete) }, diff --git a/pkg/cmd/codespace/delete_test.go b/pkg/cmd/codespace/delete_test.go index 1b381369a..ca6fe989e 100644 --- a/pkg/cmd/codespace/delete_test.go +++ b/pkg/cmd/codespace/delete_test.go @@ -266,7 +266,7 @@ func TestDelete(t *testing.T) { ios, _, stdout, stderr := iostreams.Test() ios.SetStdinTTY(true) ios.SetStdoutTTY(true) - app := NewApp(ios, nil, apiMock, nil) + app := NewApp(ios, nil, apiMock, nil, nil) err := app.Delete(context.Background(), opts) if (err != nil) != tt.wantErr { t.Errorf("delete() error = %v, wantErr %v", err, tt.wantErr) diff --git a/pkg/cmd/codespace/edit_test.go b/pkg/cmd/codespace/edit_test.go index b5e0e3cd1..886d9e455 100644 --- a/pkg/cmd/codespace/edit_test.go +++ b/pkg/cmd/codespace/edit_test.go @@ -88,7 +88,7 @@ func TestEdit(t *testing.T) { } ios, _, stdout, stderr := iostreams.Test() - a := NewApp(ios, nil, apiMock, nil) + a := NewApp(ios, nil, apiMock, nil, nil) var err error if tt.cliArgs == nil { diff --git a/pkg/cmd/codespace/logs_test.go b/pkg/cmd/codespace/logs_test.go index bd4ea02f8..161657b4d 100644 --- a/pkg/cmd/codespace/logs_test.go +++ b/pkg/cmd/codespace/logs_test.go @@ -36,5 +36,5 @@ func testingLogsApp() *App { } ios, _, _, _ := iostreams.Test() - return NewApp(ios, nil, apiMock, nil) + return NewApp(ios, nil, apiMock, nil, nil) } diff --git a/pkg/cmd/codespace/ports_test.go b/pkg/cmd/codespace/ports_test.go index 3d3c87d95..ea61b11a5 100644 --- a/pkg/cmd/codespace/ports_test.go +++ b/pkg/cmd/codespace/ports_test.go @@ -263,5 +263,5 @@ func testingPortsApp() *App { ios, _, _, _ := iostreams.Test() - return NewApp(ios, nil, apiMock, nil) + return NewApp(ios, nil, apiMock, nil, nil) } diff --git a/pkg/cmd/codespace/rebuild_test.go b/pkg/cmd/codespace/rebuild_test.go index ec8b112de..f2496d089 100644 --- a/pkg/cmd/codespace/rebuild_test.go +++ b/pkg/cmd/codespace/rebuild_test.go @@ -32,5 +32,5 @@ func testingRebuildApp(mockCodespace api.Codespace) *App { } ios, _, _, _ := iostreams.Test() - return NewApp(ios, nil, apiMock, nil) + return NewApp(ios, nil, apiMock, nil, nil) } diff --git a/pkg/cmd/codespace/select_test.go b/pkg/cmd/codespace/select_test.go index c21a876fb..e97a7720e 100644 --- a/pkg/cmd/codespace/select_test.go +++ b/pkg/cmd/codespace/select_test.go @@ -47,7 +47,7 @@ func TestApp_Select(t *testing.T) { ios, _, stdout, stderr := iostreams.Test() ios.SetStdinTTY(true) ios.SetStdoutTTY(true) - a := NewApp(ios, nil, testSelectApiMock(), nil) + a := NewApp(ios, nil, testSelectApiMock(), nil, nil) opts := selectOptions{} if tt.outputToFile { diff --git a/pkg/cmd/codespace/ssh_test.go b/pkg/cmd/codespace/ssh_test.go index 2c59539da..1740b00f7 100644 --- a/pkg/cmd/codespace/ssh_test.go +++ b/pkg/cmd/codespace/ssh_test.go @@ -278,5 +278,5 @@ func testingSSHApp() *App { } ios, _, _, _ := iostreams.Test() - return NewApp(ios, nil, apiMock, nil) + return NewApp(ios, nil, apiMock, nil, nil) } diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index 70d49b450..ce0a09cec 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -167,6 +167,7 @@ func newCodespaceCmd(f *cmdutil.Factory) *cobra.Command { &lazyLoadedHTTPClient{factory: f}, ), f.Browser, + f.Remotes, ) cmd := codespaceCmd.NewRootCmd(app) cmd.Use = "codespace"