diff --git a/pkg/cmd/pr/create/create.go b/pkg/cmd/pr/create/create.go index 09b2ba1f7..8ad123226 100644 --- a/pkg/cmd/pr/create/create.go +++ b/pkg/cmd/pr/create/create.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net/http" + "net/url" "strings" "time" @@ -553,7 +554,7 @@ func determineTrackingBranch(remotes context.Remotes, headBranch string) *git.Tr } func generateCompareURL(r ghrepo.Interface, base, head, title, body string, assignees, labels, projects []string, milestones []string) (string, error) { - u := ghrepo.GenerateRepoURL(r, "compare/%s...%s?expand=1", base, head) + u := ghrepo.GenerateRepoURL(r, "compare/%s...%s?expand=1", url.QueryEscape(base), url.QueryEscape(head)) url, err := shared.WithPrAndIssueQueryParams(u, title, body, assignees, labels, projects, milestones) if err != nil { return "", err diff --git a/pkg/cmd/pr/create/create_test.go b/pkg/cmd/pr/create/create_test.go index ebedd9f4e..456da84a9 100644 --- a/pkg/cmd/pr/create/create_test.go +++ b/pkg/cmd/pr/create/create_test.go @@ -730,3 +730,67 @@ deadb00f refs/remotes/origin/feature`) // git show-ref --verify (ShowRefs) eq(t, cs.Calls[1].Args, []string{"git", "show-ref", "--verify", "--", "HEAD", "refs/remotes/origin/great-feat", "refs/remotes/origin/feature"}) } + +func Test_generateCompareURL(t *testing.T) { + type args struct { + r ghrepo.Interface + base string + head string + title string + body string + assignees []string + labels []string + projects []string + milestones []string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + name: "basic", + args: args{ + r: ghrepo.New("OWNER", "REPO"), + base: "main", + head: "feature", + }, + want: "https://github.com/OWNER/REPO/compare/main...feature?expand=1", + wantErr: false, + }, + { + name: "with labels", + args: args{ + r: ghrepo.New("OWNER", "REPO"), + base: "a", + head: "b", + labels: []string{"one", "two three"}, + }, + want: "https://github.com/OWNER/REPO/compare/a...b?expand=1&labels=one%2Ctwo+three", + wantErr: false, + }, + { + name: "complex branch names", + args: args{ + r: ghrepo.New("OWNER", "REPO"), + base: "main/trunk", + head: "owner:feature", + }, + want: "https://github.com/OWNER/REPO/compare/main%2Ftrunk...owner%3Afeature?expand=1", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + 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) + if (err != nil) != tt.wantErr { + t.Errorf("generateCompareURL() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("generateCompareURL() = %v, want %v", got, tt.want) + } + }) + } +}