From 5d3a8e380aa4bffb1734e79ef4fde58a622c872f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 28 Oct 2022 17:58:04 +0200 Subject: [PATCH 1/2] Fix double import --- pkg/cmd/pr/create/create_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/pr/create/create_test.go b/pkg/cmd/pr/create/create_test.go index 0764ffe3b..b44725bd6 100644 --- a/pkg/cmd/pr/create/create_test.go +++ b/pkg/cmd/pr/create/create_test.go @@ -17,7 +17,6 @@ import ( "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/internal/run" "github.com/cli/cli/v2/pkg/cmd/pr/shared" - prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/httpmock" "github.com/cli/cli/v2/pkg/iostreams" @@ -779,7 +778,7 @@ func Test_createRun(t *testing.T) { setup: func(opts *CreateOptions, t *testing.T) func() { tmpfile, err := os.CreateTemp(t.TempDir(), "testrecover*") assert.NoError(t, err) - state := prShared.IssueMetadataState{ + state := shared.IssueMetadataState{ Title: "recovered title", Body: "recovered body", Reviewers: []string{"jillValentine"}, @@ -997,7 +996,7 @@ func Test_generateCompareURL(t *testing.T) { tests := []struct { name string ctx CreateContext - state prShared.IssueMetadataState + state shared.IssueMetadataState want string wantErr bool }{ @@ -1018,7 +1017,7 @@ func Test_generateCompareURL(t *testing.T) { BaseBranch: "a", HeadBranchLabel: "b", }, - state: prShared.IssueMetadataState{ + state: shared.IssueMetadataState{ Labels: []string{"one", "two three"}, }, want: "https://github.com/OWNER/REPO/compare/a...b?body=&expand=1&labels=one%2Ctwo+three", From 8c32ca925cd500949b50eb454bc993eb65eb989a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 28 Oct 2022 18:06:30 +0200 Subject: [PATCH 2/2] Enable `gh pr create --repo ` from outside of a local git repository When failing to read information from the local git repository, silence that failure if `--repo` was given. --- pkg/cmd/pr/create/create.go | 7 ++++++- pkg/cmd/pr/create/create_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/pr/create/create.go b/pkg/cmd/pr/create/create.go index 75d4b58f1..1882488a8 100644 --- a/pkg/cmd/pr/create/create.go +++ b/pkg/cmd/pr/create/create.go @@ -480,9 +480,14 @@ func NewCreateContext(opts *CreateOptions) (*CreateContext, error) { } client := api.NewClientFromHTTP(httpClient) + // TODO: consider obtaining remotes from GitClient instead remotes, err := opts.Remotes() if err != nil { - return nil, err + // When a repo override value is given, ignore errors when fetching git remotes + // to support using this command outside of git repos. + if opts.RepoOverride == "" { + return nil, err + } } repoContext, err := ghContext.ResolveRemotesToRepos(remotes, client, opts.RepoOverride) diff --git a/pkg/cmd/pr/create/create_test.go b/pkg/cmd/pr/create/create_test.go index b44725bd6..1a709ee35 100644 --- a/pkg/cmd/pr/create/create_test.go +++ b/pkg/cmd/pr/create/create_test.go @@ -2,6 +2,7 @@ package create import ( "encoding/json" + "errors" "fmt" "net/http" "os" @@ -810,6 +811,31 @@ func Test_createRun(t *testing.T) { }, wantErr: "cannot open in browser: maximum URL length exceeded", }, + { + name: "no local git repo", + setup: func(opts *CreateOptions, t *testing.T) func() { + opts.Title = "My PR" + opts.TitleProvided = true + opts.Body = "" + opts.BodyProvided = true + opts.HeadBranch = "feature" + opts.RepoOverride = "OWNER/REPO" + opts.Remotes = func() (context.Remotes, error) { + return nil, errors.New("not a git repository") + } + return func() {} + }, + httpStubs: func(reg *httpmock.Registry, t *testing.T) { + reg.Register( + httpmock.GraphQL(`mutation PullRequestCreate\b`), + httpmock.StringResponse(` + { "data": { "createPullRequest": { "pullRequest": { + "URL": "https://github.com/OWNER/REPO/pull/12" + } } } } + `)) + }, + expectedOut: "https://github.com/OWNER/REPO/pull/12\n", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {