Skip to content

Commit d5cb557

Browse files
authored
💅 simplify generateCompareURL and test
1 parent d0bee19 commit d5cb557

File tree

2 files changed

+65
-41
lines changed

2 files changed

+65
-41
lines changed

pkg/cmd/pr/create/create.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -554,11 +554,7 @@ func determineTrackingBranch(remotes context.Remotes, headBranch string) *git.Tr
554554
}
555555

556556
func generateCompareURL(r ghrepo.Interface, base, head, title, body string, assignees, labels, projects []string, milestones []string) (string, error) {
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)
557+
u := ghrepo.GenerateRepoURL(r, "compare/%s...%s?expand=1", url.QueryEscape(base), url.QueryEscape(head))
562558
url, err := shared.WithPrAndIssueQueryParams(u, title, body, assignees, labels, projects, milestones)
563559
if err != nil {
564560
return "", err

pkg/cmd/pr/create/create_test.go

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -636,42 +636,6 @@ 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-
675639
func Test_determineTrackingBranch_empty(t *testing.T) {
676640
cs, cmdTeardown := test.InitCmdStubber()
677641
defer cmdTeardown()
@@ -766,3 +730,67 @@ deadb00f refs/remotes/origin/feature`) // git show-ref --verify (ShowRefs)
766730

767731
eq(t, cs.Calls[1].Args, []string{"git", "show-ref", "--verify", "--", "HEAD", "refs/remotes/origin/great-feat", "refs/remotes/origin/feature"})
768732
}
733+
734+
func Test_generateCompareURL(t *testing.T) {
735+
type args struct {
736+
r ghrepo.Interface
737+
base string
738+
head string
739+
title string
740+
body string
741+
assignees []string
742+
labels []string
743+
projects []string
744+
milestones []string
745+
}
746+
tests := []struct {
747+
name string
748+
args args
749+
want string
750+
wantErr bool
751+
}{
752+
{
753+
name: "basic",
754+
args: args{
755+
r: ghrepo.New("OWNER", "REPO"),
756+
base: "main",
757+
head: "feature",
758+
},
759+
want: "https://github.com/OWNER/REPO/compare/main...feature?expand=1",
760+
wantErr: false,
761+
},
762+
{
763+
name: "with labels",
764+
args: args{
765+
r: ghrepo.New("OWNER", "REPO"),
766+
base: "a",
767+
head: "b",
768+
labels: []string{"one", "two three"},
769+
},
770+
want: "https://github.com/OWNER/REPO/compare/a...b?expand=1&labels=one%2Ctwo+three",
771+
wantErr: false,
772+
},
773+
{
774+
name: "complex branch names",
775+
args: args{
776+
r: ghrepo.New("OWNER", "REPO"),
777+
base: "main/trunk",
778+
head: "owner:feature",
779+
},
780+
want: "https://github.com/OWNER/REPO/compare/main%2Ftrunk...owner%3Afeature?expand=1",
781+
wantErr: false,
782+
},
783+
}
784+
for _, tt := range tests {
785+
t.Run(tt.name, func(t *testing.T) {
786+
got, err := generateCompareURL(tt.args.r, tt.args.base, tt.args.head, tt.args.title, tt.args.body, tt.args.assignees, tt.args.labels, tt.args.projects, tt.args.milestones)
787+
if (err != nil) != tt.wantErr {
788+
t.Errorf("generateCompareURL() error = %v, wantErr %v", err, tt.wantErr)
789+
return
790+
}
791+
if got != tt.want {
792+
t.Errorf("generateCompareURL() = %v, want %v", got, tt.want)
793+
}
794+
})
795+
}
796+
}

0 commit comments

Comments
 (0)