From e2efc0b8a338daa8bbcd7962c33239cbee0e263e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 23 Sep 2020 19:53:27 +0200 Subject: [PATCH] Fix `pr create` when branch was already pushed to a non-base remote --- pkg/cmd/pr/create/create.go | 11 +++--- pkg/cmd/pr/create/create_test.go | 66 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/pr/create/create.go b/pkg/cmd/pr/create/create.go index 3259b5561..ad54d83dc 100644 --- a/pkg/cmd/pr/create/create.go +++ b/pkg/cmd/pr/create/create.go @@ -181,13 +181,12 @@ func createRun(opts *CreateOptions) error { // determine whether the head branch is already pushed to a remote if pushedTo := determineTrackingBranch(remotes, headBranch); pushedTo != nil { isPushEnabled = false - for _, r := range remotes { - if r.Name != pushedTo.RemoteName { - continue - } + if r, err := remotes.FindByName(pushedTo.RemoteName); err == nil { headRepo = r headRemote = r - break + if !ghrepo.IsSame(baseRepo, headRepo) { + headBranchLabel = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch) + } } } } @@ -315,7 +314,7 @@ func createRun(opts *CreateOptions) error { if isTerminal { fmt.Fprintf(opts.IO.ErrOut, message, - utils.Cyan(headBranch), + utils.Cyan(headBranchLabel), utils.Cyan(baseBranch), ghrepo.FullName(baseRepo)) if (title == "" || body == "") && defaultsErr != nil { diff --git a/pkg/cmd/pr/create/create_test.go b/pkg/cmd/pr/create/create_test.go index 935675f5d..fc8b0badd 100644 --- a/pkg/cmd/pr/create/create_test.go +++ b/pkg/cmd/pr/create/create_test.go @@ -9,10 +9,12 @@ import ( "strings" "testing" + "github.com/MakeNowJust/heredoc" "github.com/cli/cli/context" "github.com/cli/cli/git" "github.com/cli/cli/internal/config" "github.com/cli/cli/internal/ghrepo" + "github.com/cli/cli/internal/run" "github.com/cli/cli/pkg/cmdutil" "github.com/cli/cli/pkg/httpmock" "github.com/cli/cli/pkg/iostreams" @@ -288,6 +290,70 @@ func TestPRCreate_createFork(t *testing.T) { assert.Equal(t, "https://github.com/OWNER/REPO/pull/12\n", output.String()) } +func TestPRCreate_pushToNonBaseRepo(t *testing.T) { + remotes := context.Remotes{ + { + Remote: &git.Remote{ + Name: "upstream", + Resolved: "base", + }, + Repo: ghrepo.New("OWNER", "REPO"), + }, + { + Remote: &git.Remote{ + Name: "origin", + Resolved: "base", + }, + Repo: ghrepo.New("monalisa", "REPO"), + }, + } + + http := initFakeHTTP() + defer http.Verify(t) + + http.StubRepoInfoResponse("OWNER", "REPO", "master") + http.Register( + httpmock.GraphQL(`query PullRequestForBranch\b`), + httpmock.StringResponse(` + { "data": { "repository": { "pullRequests": { "nodes" : [ + ] } } } } + `)) + http.Register( + httpmock.GraphQL(`mutation PullRequestCreate\b`), + httpmock.GraphQLMutation(` + { "data": { "createPullRequest": { "pullRequest": { + "URL": "https://github.com/OWNER/REPO/pull/12" + } } } } + `, func(input map[string]interface{}) { + assert.Equal(t, "REPOID", input["repositoryId"].(string)) + assert.Equal(t, "master", input["baseRefName"].(string)) + assert.Equal(t, "monalisa:feature", input["headRefName"].(string)) + })) + + cs, cmdTeardown := run.Stub() + defer cmdTeardown(t) + + cs.Register("git status", 0, "") + cs.Register(`git config --get-regexp \^branch\\\.feature\\\.`, 1, "") // determineTrackingBranch + cs.Register("git show-ref --verify", 0, heredoc.Doc(` + deadbeef HEAD + deadb00f refs/remotes/upstream/feature + deadbeef refs/remotes/origin/feature + `)) // determineTrackingBranch + cs.Register("git .+ log", 1, "", func(args []string) { + assert.Equal(t, "upstream/master...feature", args[len(args)-1]) + }) + + _, cleanupAsk := prompt.InitAskStubber() + defer cleanupAsk() + + output, err := runCommand(http, remotes, "feature", true, `-t title -b body`) + require.NoError(t, err) + + assert.Equal(t, "\nCreating pull request for monalisa:feature into master in OWNER/REPO\n\n", output.Stderr()) + assert.Equal(t, "https://github.com/OWNER/REPO/pull/12\n", output.String()) +} + func TestPRCreate_nonLegacyTemplate(t *testing.T) { http := initFakeHTTP() defer http.Verify(t)