Skip to content

Commit e2efc0b

Browse files
committed
Fix pr create when branch was already pushed to a non-base remote
1 parent f9783fe commit e2efc0b

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

pkg/cmd/pr/create/create.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,12 @@ func createRun(opts *CreateOptions) error {
181181
// determine whether the head branch is already pushed to a remote
182182
if pushedTo := determineTrackingBranch(remotes, headBranch); pushedTo != nil {
183183
isPushEnabled = false
184-
for _, r := range remotes {
185-
if r.Name != pushedTo.RemoteName {
186-
continue
187-
}
184+
if r, err := remotes.FindByName(pushedTo.RemoteName); err == nil {
188185
headRepo = r
189186
headRemote = r
190-
break
187+
if !ghrepo.IsSame(baseRepo, headRepo) {
188+
headBranchLabel = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch)
189+
}
191190
}
192191
}
193192
}
@@ -315,7 +314,7 @@ func createRun(opts *CreateOptions) error {
315314

316315
if isTerminal {
317316
fmt.Fprintf(opts.IO.ErrOut, message,
318-
utils.Cyan(headBranch),
317+
utils.Cyan(headBranchLabel),
319318
utils.Cyan(baseBranch),
320319
ghrepo.FullName(baseRepo))
321320
if (title == "" || body == "") && defaultsErr != nil {

pkg/cmd/pr/create/create_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"strings"
1010
"testing"
1111

12+
"github.com/MakeNowJust/heredoc"
1213
"github.com/cli/cli/context"
1314
"github.com/cli/cli/git"
1415
"github.com/cli/cli/internal/config"
1516
"github.com/cli/cli/internal/ghrepo"
17+
"github.com/cli/cli/internal/run"
1618
"github.com/cli/cli/pkg/cmdutil"
1719
"github.com/cli/cli/pkg/httpmock"
1820
"github.com/cli/cli/pkg/iostreams"
@@ -288,6 +290,70 @@ func TestPRCreate_createFork(t *testing.T) {
288290
assert.Equal(t, "https://github.com/OWNER/REPO/pull/12\n", output.String())
289291
}
290292

293+
func TestPRCreate_pushToNonBaseRepo(t *testing.T) {
294+
remotes := context.Remotes{
295+
{
296+
Remote: &git.Remote{
297+
Name: "upstream",
298+
Resolved: "base",
299+
},
300+
Repo: ghrepo.New("OWNER", "REPO"),
301+
},
302+
{
303+
Remote: &git.Remote{
304+
Name: "origin",
305+
Resolved: "base",
306+
},
307+
Repo: ghrepo.New("monalisa", "REPO"),
308+
},
309+
}
310+
311+
http := initFakeHTTP()
312+
defer http.Verify(t)
313+
314+
http.StubRepoInfoResponse("OWNER", "REPO", "master")
315+
http.Register(
316+
httpmock.GraphQL(`query PullRequestForBranch\b`),
317+
httpmock.StringResponse(`
318+
{ "data": { "repository": { "pullRequests": { "nodes" : [
319+
] } } } }
320+
`))
321+
http.Register(
322+
httpmock.GraphQL(`mutation PullRequestCreate\b`),
323+
httpmock.GraphQLMutation(`
324+
{ "data": { "createPullRequest": { "pullRequest": {
325+
"URL": "https://github.com/OWNER/REPO/pull/12"
326+
} } } }
327+
`, func(input map[string]interface{}) {
328+
assert.Equal(t, "REPOID", input["repositoryId"].(string))
329+
assert.Equal(t, "master", input["baseRefName"].(string))
330+
assert.Equal(t, "monalisa:feature", input["headRefName"].(string))
331+
}))
332+
333+
cs, cmdTeardown := run.Stub()
334+
defer cmdTeardown(t)
335+
336+
cs.Register("git status", 0, "")
337+
cs.Register(`git config --get-regexp \^branch\\\.feature\\\.`, 1, "") // determineTrackingBranch
338+
cs.Register("git show-ref --verify", 0, heredoc.Doc(`
339+
deadbeef HEAD
340+
deadb00f refs/remotes/upstream/feature
341+
deadbeef refs/remotes/origin/feature
342+
`)) // determineTrackingBranch
343+
cs.Register("git .+ log", 1, "", func(args []string) {
344+
assert.Equal(t, "upstream/master...feature", args[len(args)-1])
345+
})
346+
347+
_, cleanupAsk := prompt.InitAskStubber()
348+
defer cleanupAsk()
349+
350+
output, err := runCommand(http, remotes, "feature", true, `-t title -b body`)
351+
require.NoError(t, err)
352+
353+
assert.Equal(t, "\nCreating pull request for monalisa:feature into master in OWNER/REPO\n\n", output.Stderr())
354+
assert.Equal(t, "https://github.com/OWNER/REPO/pull/12\n", output.String())
355+
}
356+
291357
func TestPRCreate_nonLegacyTemplate(t *testing.T) {
292358
http := initFakeHTTP()
293359
defer http.Verify(t)

0 commit comments

Comments
 (0)