Skip to content

Commit d0bee19

Browse files
author
Leonardo
authored
Query escape branch names to allow branch/cli#123 names
1 parent becb316 commit d0bee19

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

pkg/cmd/pr/create/create.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"net/http"
7+
"net/url"
78
"strings"
89
"time"
910

@@ -553,7 +554,11 @@ func determineTrackingBranch(remotes context.Remotes, headBranch string) *git.Tr
553554
}
554555

555556
func generateCompareURL(r ghrepo.Interface, base, head, title, body string, assignees, labels, projects []string, milestones []string) (string, error) {
556-
u := ghrepo.GenerateRepoURL(r, "compare/%s...%s?expand=1", base, head)
557+
// This is to fix issues with names like "branch/#123"
558+
b := url.QueryEscape(base)
559+
h := url.QueryEscape(head)
560+
561+
u := ghrepo.GenerateRepoURL(r, "compare/%s...%s?expand=1", b, h)
557562
url, err := shared.WithPrAndIssueQueryParams(u, title, body, assignees, labels, projects, milestones)
558563
if err != nil {
559564
return "", err

pkg/cmd/pr/create/create_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,42 @@ func TestPRCreate_web(t *testing.T) {
636636
eq(t, browserCall[len(browserCall)-1], "https://github.com/OWNER/REPO/compare/master...feature?expand=1")
637637
}
638638

639+
func TestPRCreate_web_uncommon_branch_names(t *testing.T) {
640+
http := initFakeHTTP()
641+
defer http.Verify(t)
642+
643+
http.StubRepoInfoResponse("OWNER", "REPO", "master")
644+
http.StubRepoResponse("OWNER", "REPO")
645+
http.Register(
646+
httpmock.GraphQL(`query UserCurrent\b`),
647+
httpmock.StringResponse(`{"data": {"viewer": {"login": "OWNER"} } }`))
648+
649+
cs, cmdTeardown := test.InitCmdStubber()
650+
defer cmdTeardown()
651+
652+
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
653+
cs.Stub("") // git show-ref --verify (determineTrackingBranch)
654+
cs.Stub("") // git status
655+
cs.Stub("1234567890,commit 0\n2345678901,commit 1") // git log
656+
cs.Stub("") // git push
657+
cs.Stub("") // browser
658+
659+
ask, cleanupAsk := prompt.InitAskStubber()
660+
defer cleanupAsk()
661+
ask.StubOne(0)
662+
663+
output, err := runCommand(http, nil, "feature/#123", true, `--web`)
664+
require.NoError(t, err)
665+
666+
eq(t, output.String(), "")
667+
eq(t, output.Stderr(), "Opening github.com/OWNER/REPO/compare/master...feature/#123 in your browser.\n")
668+
669+
eq(t, len(cs.Calls), 6)
670+
eq(t, strings.Join(cs.Calls[4].Args, " "), "git push --set-upstream origin HEAD:feature/#123")
671+
browserCall := cs.Calls[5].Args
672+
eq(t, browserCall[len(browserCall)-1], "https://github.com/OWNER/REPO/compare/master...feature%2F%23123?expand=1")
673+
}
674+
639675
func Test_determineTrackingBranch_empty(t *testing.T) {
640676
cs, cmdTeardown := test.InitCmdStubber()
641677
defer cmdTeardown()

0 commit comments

Comments
 (0)