Skip to content

Commit

Permalink
Merge pull request cli#6527 from cli/pr-create-no-git
Browse files Browse the repository at this point in the history
Enable `gh pr create` from outside of a local git repo
  • Loading branch information
mislav authored Nov 1, 2022
2 parents fe485c1 + 8c32ca9 commit eeff886
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
7 changes: 6 additions & 1 deletion pkg/cmd/pr/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 29 additions & 4 deletions pkg/cmd/pr/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package create

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
Expand All @@ -17,7 +18,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"
Expand Down Expand Up @@ -779,7 +779,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"},
Expand Down Expand Up @@ -811,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) {
Expand Down Expand Up @@ -997,7 +1022,7 @@ func Test_generateCompareURL(t *testing.T) {
tests := []struct {
name string
ctx CreateContext
state prShared.IssueMetadataState
state shared.IssueMetadataState
want string
wantErr bool
}{
Expand All @@ -1018,7 +1043,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",
Expand Down

0 comments on commit eeff886

Please sign in to comment.