From 5fc722fbb802064f4bf4be98abaac630fcb681d4 Mon Sep 17 00:00:00 2001 From: AliabbasMerchant Date: Sat, 23 May 2020 14:40:49 +0530 Subject: [PATCH 1/4] Query Params in Web mode of Issue/PR creation --- command/issue.go | 37 ++++++++++++---------- command/issue_test.go | 2 +- command/pr_create.go | 72 ++++++++++++++++++++++++++++++------------- 3 files changed, 71 insertions(+), 40 deletions(-) diff --git a/command/issue.go b/command/issue.go index 3d3f01708..62ccae125 100644 --- a/command/issue.go +++ b/command/issue.go @@ -29,14 +29,14 @@ func init() { issueCreateCmd.Flags().StringP("body", "b", "", "Supply a body. Will prompt for one otherwise.") issueCreateCmd.Flags().BoolP("web", "w", false, "Open the browser to create an issue") - issueCreateCmd.Flags().StringSliceP("assignee", "a", nil, "Assign a person by their `login`") - issueCreateCmd.Flags().StringSliceP("label", "l", nil, "Add a label by `name`") - issueCreateCmd.Flags().StringSliceP("project", "p", nil, "Add the issue to a project by `name`") + issueCreateCmd.Flags().StringSliceP("assignees", "a", nil, "Assign people by their `login`") + issueCreateCmd.Flags().StringSliceP("labels", "l", nil, "Add labels by `name`") + issueCreateCmd.Flags().StringSliceP("projects", "p", nil, "Add the issue to projects by `name`") issueCreateCmd.Flags().StringP("milestone", "m", "", "Add the issue to a milestone by `name`") issueCmd.AddCommand(issueListCmd) issueListCmd.Flags().StringP("assignee", "a", "", "Filter by assignee") - issueListCmd.Flags().StringSliceP("label", "l", nil, "Filter by label") + issueListCmd.Flags().StringSliceP("labels", "l", nil, "Filter by labels") issueListCmd.Flags().StringP("state", "s", "open", "Filter by state: {open|closed|all}") issueListCmd.Flags().IntP("limit", "L", 30, "Maximum number of issues to fetch") issueListCmd.Flags().StringP("author", "A", "", "Filter by author") @@ -116,7 +116,7 @@ func issueList(cmd *cobra.Command, args []string) error { return err } - labels, err := cmd.Flags().GetStringSlice("label") + labels, err := cmd.Flags().GetStringSlice("labels") if err != nil { return err } @@ -373,15 +373,15 @@ func issueCreate(cmd *cobra.Command, args []string) error { return fmt.Errorf("could not parse body: %w", err) } - assignees, err := cmd.Flags().GetStringSlice("assignee") + assignees, err := cmd.Flags().GetStringSlice("assignees") if err != nil { return fmt.Errorf("could not parse assignees: %w", err) } - labelNames, err := cmd.Flags().GetStringSlice("label") + labelNames, err := cmd.Flags().GetStringSlice("labels") if err != nil { return fmt.Errorf("could not parse labels: %w", err) } - projectNames, err := cmd.Flags().GetStringSlice("project") + projectNames, err := cmd.Flags().GetStringSlice("projects") if err != nil { return fmt.Errorf("could not parse projects: %w", err) } @@ -394,13 +394,13 @@ func issueCreate(cmd *cobra.Command, args []string) error { if isWeb, err := cmd.Flags().GetBool("web"); err == nil && isWeb { // TODO: move URL generation into GitHubRepository - openURL := fmt.Sprintf("https://github.com/%s/issues/new", ghrepo.FullName(baseRepo)) + openURL := fmt.Sprintf("https://github.com/%s/issues/new?", ghrepo.FullName(baseRepo)) if title != "" || body != "" { - openURL += fmt.Sprintf( - "?title=%s&body=%s", - url.QueryEscape(title), - url.QueryEscape(body), - ) + milestone := "" + if len(milestoneTitles) > 0 { + milestone = milestoneTitles[0] + } + openURL += getPrAndIssueQueryParams(title, body, assignees, labelNames, projectNames, milestone) } else if len(templateFiles) > 1 { openURL += "/choose" } @@ -456,11 +456,14 @@ func issueCreate(cmd *cobra.Command, args []string) error { if action == PreviewAction { openURL := fmt.Sprintf( - "https://github.com/%s/issues/new/?title=%s&body=%s", + "https://github.com/%s/issues/new/?", ghrepo.FullName(baseRepo), - url.QueryEscape(title), - url.QueryEscape(body), ) + milestone := "" + if len(milestoneTitles) > 0 { + milestone = milestoneTitles[0] + } + openURL += getPrAndIssueQueryParams(title, body, assignees, labelNames, projectNames, milestone) // TODO could exceed max url length for explorer fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL)) return utils.OpenInBrowser(openURL) diff --git a/command/issue_test.go b/command/issue_test.go index 3df6910f5..416a338c2 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -611,7 +611,7 @@ func TestIssueCreate_web(t *testing.T) { t.Fatal("expected a command to run") } url := seenCmd.Args[len(seenCmd.Args)-1] - eq(t, url, "https://github.com/OWNER/REPO/issues/new") + eq(t, url, "https://github.com/OWNER/REPO/issues/new?") eq(t, output.String(), "Opening github.com/OWNER/REPO/issues/new in your browser.\n") eq(t, output.Stderr(), "") } diff --git a/command/pr_create.go b/command/pr_create.go index 96dc08791..85135324d 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -124,19 +124,19 @@ func prCreate(cmd *cobra.Command, _ []string) error { return fmt.Errorf("could not parse body: %w", err) } - reviewers, err := cmd.Flags().GetStringSlice("reviewer") + reviewers, err := cmd.Flags().GetStringSlice("reviewers") if err != nil { return fmt.Errorf("could not parse reviewers: %w", err) } - assignees, err := cmd.Flags().GetStringSlice("assignee") + assignees, err := cmd.Flags().GetStringSlice("assignees") if err != nil { return fmt.Errorf("could not parse assignees: %w", err) } - labelNames, err := cmd.Flags().GetStringSlice("label") + labelNames, err := cmd.Flags().GetStringSlice("labels") if err != nil { return fmt.Errorf("could not parse labels: %w", err) } - projectNames, err := cmd.Flags().GetStringSlice("project") + projectNames, err := cmd.Flags().GetStringSlice("projects") if err != nil { return fmt.Errorf("could not parse projects: %w", err) } @@ -178,17 +178,17 @@ func prCreate(cmd *cobra.Command, _ []string) error { } if !isWeb { - headBranchLabel := headBranch + headBranchRef := headBranch if headRepo != nil && !ghrepo.IsSame(baseRepo, headRepo) { - headBranchLabel = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch) + headBranchRef = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch) } - existingPR, err := api.PullRequestForBranch(client, baseRepo, baseBranch, headBranchLabel) + existingPR, err := api.PullRequestForBranch(client, baseRepo, baseBranch, headBranchRef) var notFound *api.NotFoundError if err != nil && !errors.As(err, ¬Found) { return fmt.Errorf("error checking for existing pull request: %w", err) } if err == nil { - return fmt.Errorf("a pull request for branch %q into branch %q already exists:\n%s", headBranchLabel, baseBranch, existingPR.URL) + return fmt.Errorf("a pull request for branch %q into branch %q already exists:\n%s", headBranchRef, baseBranch, existingPR.URL) } } @@ -250,6 +250,9 @@ func prCreate(cmd *cobra.Command, _ []string) error { if isDraft && isWeb { return errors.New("the --draft flag is not supported with --web") } + if len(reviewers) > 0 && isWeb { + return errors.New("the --reviewers flag is not supported with --web") + } didForkRepo := false // if a head repository could not be determined so far, automatically create @@ -265,9 +268,9 @@ func prCreate(cmd *cobra.Command, _ []string) error { didForkRepo = true } - headBranchLabel := headBranch + headBranchRef := headBranch if !ghrepo.IsSame(baseRepo, headRepo) { - headBranchLabel = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch) + headBranchRef = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch) } if headRemote == nil { @@ -322,7 +325,7 @@ func prCreate(cmd *cobra.Command, _ []string) error { "body": body, "draft": isDraft, "baseRefName": baseBranch, - "headRefName": headBranchLabel, + "headRefName": headBranchRef, } err = addMetadataToIssueParams(client, baseRepo, params, &tb) @@ -337,7 +340,11 @@ func prCreate(cmd *cobra.Command, _ []string) error { fmt.Fprintln(cmd.OutOrStdout(), pr.URL) } else if action == PreviewAction { - openURL := generateCompareURL(baseRepo, baseBranch, headBranchLabel, title, body) + milestone := "" + if len(milestoneTitles) > 0 { + milestone = milestoneTitles[0] + } + openURL := generateCompareURL(baseRepo, baseBranch, headBranchRef, title, body, assignees, labelNames, projectNames, milestone) // TODO could exceed max url length for explorer fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL)) return utils.OpenInBrowser(openURL) @@ -389,18 +396,39 @@ func determineTrackingBranch(remotes context.Remotes, headBranch string) *git.Tr return nil } -func generateCompareURL(r ghrepo.Interface, base, head, title, body string) string { +func getPrAndIssueQueryParams(title, body string, assignees, labels, projects []string, milestone string) string { + queryParams := "" + if title != "" { + queryParams += "&title=" + url.QueryEscape(title) + } + if body != "" { + queryParams += "&body=" + url.QueryEscape(body) + } + if len(assignees) > 0 { + queryParams += "&assignees=" + url.QueryEscape(strings.Join(assignees, ",")) + } + if len(labels) > 0 { + queryParams += "&labels=" + url.QueryEscape(strings.Join(labels, ",")) + } + if len(projects) > 0 { + queryParams += "&projects=" + url.QueryEscape(strings.Join(projects, ",")) + } + if milestone != "" { + queryParams += "&milestone=" + url.QueryEscape(milestone) + } + return strings.TrimLeft(queryParams, "&") +} + +func generateCompareURL(r ghrepo.Interface, base, head, title, body string, assignees, labels, projects []string, milestone string) string { u := fmt.Sprintf( "https://github.com/%s/compare/%s...%s?expand=1", ghrepo.FullName(r), base, head, ) - if title != "" { - u += "&title=" + url.QueryEscape(title) - } - if body != "" { - u += "&body=" + url.QueryEscape(body) + queryParams := getPrAndIssueQueryParams(title, body, assignees, labels, projects, milestone) + if len(queryParams) > 0 { + u += "&" + getPrAndIssueQueryParams(title, body, assignees, labels, projects, milestone) } return u } @@ -423,9 +451,9 @@ func init() { prCreateCmd.Flags().BoolP("web", "w", false, "Open the web browser to create a pull request") prCreateCmd.Flags().BoolP("fill", "f", false, "Do not prompt for title/body and just use commit info") - prCreateCmd.Flags().StringSliceP("reviewer", "r", nil, "Request a review from someone by their `login`") - prCreateCmd.Flags().StringSliceP("assignee", "a", nil, "Assign a person by their `login`") - prCreateCmd.Flags().StringSliceP("label", "l", nil, "Add a label by `name`") - prCreateCmd.Flags().StringSliceP("project", "p", nil, "Add the pull request to a project by `name`") + prCreateCmd.Flags().StringSliceP("reviewers", "r", nil, "Request reviews from people by their `login`") + prCreateCmd.Flags().StringSliceP("assignees", "a", nil, "Assign people by their `login`") + prCreateCmd.Flags().StringSliceP("labels", "l", nil, "Add labels by `name`") + prCreateCmd.Flags().StringSliceP("projects", "p", nil, "Add the pull request to projects by `name`") prCreateCmd.Flags().StringP("milestone", "m", "", "Add the pull request to a milestone by `name`") } From 42edd42da1259e85931b6a498deaf6e4792276cb Mon Sep 17 00:00:00 2001 From: AliabbasMerchant Date: Tue, 26 May 2020 07:10:28 +0530 Subject: [PATCH 2/4] Undo unnecessary renames --- command/issue.go | 16 ++++++++-------- command/pr_create.go | 32 ++++++++++++++++---------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/command/issue.go b/command/issue.go index 62ccae125..695168906 100644 --- a/command/issue.go +++ b/command/issue.go @@ -29,14 +29,14 @@ func init() { issueCreateCmd.Flags().StringP("body", "b", "", "Supply a body. Will prompt for one otherwise.") issueCreateCmd.Flags().BoolP("web", "w", false, "Open the browser to create an issue") - issueCreateCmd.Flags().StringSliceP("assignees", "a", nil, "Assign people by their `login`") - issueCreateCmd.Flags().StringSliceP("labels", "l", nil, "Add labels by `name`") - issueCreateCmd.Flags().StringSliceP("projects", "p", nil, "Add the issue to projects by `name`") + issueCreateCmd.Flags().StringSliceP("assignee", "a", nil, "Assign a person by their `login`") + issueCreateCmd.Flags().StringSliceP("label", "l", nil, "Add a label by `name`") + issueCreateCmd.Flags().StringSliceP("project", "p", nil, "Add the issue to a project by `name`") issueCreateCmd.Flags().StringP("milestone", "m", "", "Add the issue to a milestone by `name`") issueCmd.AddCommand(issueListCmd) issueListCmd.Flags().StringP("assignee", "a", "", "Filter by assignee") - issueListCmd.Flags().StringSliceP("labels", "l", nil, "Filter by labels") + issueListCmd.Flags().StringSliceP("label", "l", nil, "Filter by label") issueListCmd.Flags().StringP("state", "s", "open", "Filter by state: {open|closed|all}") issueListCmd.Flags().IntP("limit", "L", 30, "Maximum number of issues to fetch") issueListCmd.Flags().StringP("author", "A", "", "Filter by author") @@ -116,7 +116,7 @@ func issueList(cmd *cobra.Command, args []string) error { return err } - labels, err := cmd.Flags().GetStringSlice("labels") + labels, err := cmd.Flags().GetStringSlice("label") if err != nil { return err } @@ -373,15 +373,15 @@ func issueCreate(cmd *cobra.Command, args []string) error { return fmt.Errorf("could not parse body: %w", err) } - assignees, err := cmd.Flags().GetStringSlice("assignees") + assignees, err := cmd.Flags().GetStringSlice("assignee") if err != nil { return fmt.Errorf("could not parse assignees: %w", err) } - labelNames, err := cmd.Flags().GetStringSlice("labels") + labelNames, err := cmd.Flags().GetStringSlice("label") if err != nil { return fmt.Errorf("could not parse labels: %w", err) } - projectNames, err := cmd.Flags().GetStringSlice("projects") + projectNames, err := cmd.Flags().GetStringSlice("project") if err != nil { return fmt.Errorf("could not parse projects: %w", err) } diff --git a/command/pr_create.go b/command/pr_create.go index 85135324d..2669a082c 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -124,19 +124,19 @@ func prCreate(cmd *cobra.Command, _ []string) error { return fmt.Errorf("could not parse body: %w", err) } - reviewers, err := cmd.Flags().GetStringSlice("reviewers") + reviewers, err := cmd.Flags().GetStringSlice("reviewer") if err != nil { return fmt.Errorf("could not parse reviewers: %w", err) } - assignees, err := cmd.Flags().GetStringSlice("assignees") + assignees, err := cmd.Flags().GetStringSlice("assignee") if err != nil { return fmt.Errorf("could not parse assignees: %w", err) } - labelNames, err := cmd.Flags().GetStringSlice("labels") + labelNames, err := cmd.Flags().GetStringSlice("label") if err != nil { return fmt.Errorf("could not parse labels: %w", err) } - projectNames, err := cmd.Flags().GetStringSlice("projects") + projectNames, err := cmd.Flags().GetStringSlice("project") if err != nil { return fmt.Errorf("could not parse projects: %w", err) } @@ -178,17 +178,17 @@ func prCreate(cmd *cobra.Command, _ []string) error { } if !isWeb { - headBranchRef := headBranch + headBranchLabel := headBranch if headRepo != nil && !ghrepo.IsSame(baseRepo, headRepo) { - headBranchRef = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch) + headBranchLabel = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch) } - existingPR, err := api.PullRequestForBranch(client, baseRepo, baseBranch, headBranchRef) + existingPR, err := api.PullRequestForBranch(client, baseRepo, baseBranch, headBranchLabel) var notFound *api.NotFoundError if err != nil && !errors.As(err, ¬Found) { return fmt.Errorf("error checking for existing pull request: %w", err) } if err == nil { - return fmt.Errorf("a pull request for branch %q into branch %q already exists:\n%s", headBranchRef, baseBranch, existingPR.URL) + return fmt.Errorf("a pull request for branch %q into branch %q already exists:\n%s", headBranchLabel, baseBranch, existingPR.URL) } } @@ -268,9 +268,9 @@ func prCreate(cmd *cobra.Command, _ []string) error { didForkRepo = true } - headBranchRef := headBranch + headBranchLabel := headBranch if !ghrepo.IsSame(baseRepo, headRepo) { - headBranchRef = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch) + headBranchLabel = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch) } if headRemote == nil { @@ -325,7 +325,7 @@ func prCreate(cmd *cobra.Command, _ []string) error { "body": body, "draft": isDraft, "baseRefName": baseBranch, - "headRefName": headBranchRef, + "headRefName": headBranchLabel, } err = addMetadataToIssueParams(client, baseRepo, params, &tb) @@ -344,7 +344,7 @@ func prCreate(cmd *cobra.Command, _ []string) error { if len(milestoneTitles) > 0 { milestone = milestoneTitles[0] } - openURL := generateCompareURL(baseRepo, baseBranch, headBranchRef, title, body, assignees, labelNames, projectNames, milestone) + openURL := generateCompareURL(baseRepo, baseBranch, headBranchLabel, title, body, assignees, labelNames, projectNames, milestone) // TODO could exceed max url length for explorer fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL)) return utils.OpenInBrowser(openURL) @@ -451,9 +451,9 @@ func init() { prCreateCmd.Flags().BoolP("web", "w", false, "Open the web browser to create a pull request") prCreateCmd.Flags().BoolP("fill", "f", false, "Do not prompt for title/body and just use commit info") - prCreateCmd.Flags().StringSliceP("reviewers", "r", nil, "Request reviews from people by their `login`") - prCreateCmd.Flags().StringSliceP("assignees", "a", nil, "Assign people by their `login`") - prCreateCmd.Flags().StringSliceP("labels", "l", nil, "Add labels by `name`") - prCreateCmd.Flags().StringSliceP("projects", "p", nil, "Add the pull request to projects by `name`") + prCreateCmd.Flags().StringSliceP("reviewer", "r", nil, "Request a review from someone by their `login`") + prCreateCmd.Flags().StringSliceP("assignee", "a", nil, "Assign a person by their `login`") + prCreateCmd.Flags().StringSliceP("label", "l", nil, "Add a label by `name`") + prCreateCmd.Flags().StringSliceP("project", "p", nil, "Add the pull request to a project by `name`") prCreateCmd.Flags().StringP("milestone", "m", "", "Add the pull request to a milestone by `name`") } From f95b8916f33d8c1ba860ad33be9acd0adbc5af6a Mon Sep 17 00:00:00 2001 From: AliabbasMerchant Date: Tue, 26 May 2020 07:45:14 +0530 Subject: [PATCH 3/4] Use url package for setting query params --- command/issue.go | 17 ++++++++++------- command/issue_test.go | 4 ++-- command/pr_create.go | 41 +++++++++++++++++++++++++---------------- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/command/issue.go b/command/issue.go index 695168906..372bb227e 100644 --- a/command/issue.go +++ b/command/issue.go @@ -394,13 +394,16 @@ func issueCreate(cmd *cobra.Command, args []string) error { if isWeb, err := cmd.Flags().GetBool("web"); err == nil && isWeb { // TODO: move URL generation into GitHubRepository - openURL := fmt.Sprintf("https://github.com/%s/issues/new?", ghrepo.FullName(baseRepo)) + openURL := fmt.Sprintf("https://github.com/%s/issues/new", ghrepo.FullName(baseRepo)) if title != "" || body != "" { milestone := "" if len(milestoneTitles) > 0 { milestone = milestoneTitles[0] } - openURL += getPrAndIssueQueryParams(title, body, assignees, labelNames, projectNames, milestone) + openURL, err = withPrAndIssueQueryParams(openURL, title, body, assignees, labelNames, projectNames, milestone) + if err != nil { + return err + } } else if len(templateFiles) > 1 { openURL += "/choose" } @@ -455,15 +458,15 @@ func issueCreate(cmd *cobra.Command, args []string) error { } if action == PreviewAction { - openURL := fmt.Sprintf( - "https://github.com/%s/issues/new/?", - ghrepo.FullName(baseRepo), - ) + openURL := fmt.Sprintf("https://github.com/%s/issues/new/", ghrepo.FullName(baseRepo)) milestone := "" if len(milestoneTitles) > 0 { milestone = milestoneTitles[0] } - openURL += getPrAndIssueQueryParams(title, body, assignees, labelNames, projectNames, milestone) + openURL, err = withPrAndIssueQueryParams(openURL, title, body, assignees, labelNames, projectNames, milestone) + if err != nil { + return err + } // TODO could exceed max url length for explorer fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL)) return utils.OpenInBrowser(openURL) diff --git a/command/issue_test.go b/command/issue_test.go index 416a338c2..45fdd63ce 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -611,7 +611,7 @@ func TestIssueCreate_web(t *testing.T) { t.Fatal("expected a command to run") } url := seenCmd.Args[len(seenCmd.Args)-1] - eq(t, url, "https://github.com/OWNER/REPO/issues/new?") + eq(t, url, "https://github.com/OWNER/REPO/issues/new") eq(t, output.String(), "Opening github.com/OWNER/REPO/issues/new in your browser.\n") eq(t, output.Stderr(), "") } @@ -637,7 +637,7 @@ func TestIssueCreate_webTitleBody(t *testing.T) { t.Fatal("expected a command to run") } url := strings.ReplaceAll(seenCmd.Args[len(seenCmd.Args)-1], "^", "") - eq(t, url, "https://github.com/OWNER/REPO/issues/new?title=mytitle&body=mybody") + eq(t, url, "https://github.com/OWNER/REPO/issues/new?body=mybody&title=mytitle") eq(t, output.String(), "Opening github.com/OWNER/REPO/issues/new in your browser.\n") } diff --git a/command/pr_create.go b/command/pr_create.go index 2669a082c..931187859 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -3,6 +3,7 @@ package command import ( "errors" "fmt" + "log" "net/url" "strings" "time" @@ -251,7 +252,7 @@ func prCreate(cmd *cobra.Command, _ []string) error { return errors.New("the --draft flag is not supported with --web") } if len(reviewers) > 0 && isWeb { - return errors.New("the --reviewers flag is not supported with --web") + return errors.New("the --reviewer flag is not supported with --web") } didForkRepo := false @@ -344,7 +345,10 @@ func prCreate(cmd *cobra.Command, _ []string) error { if len(milestoneTitles) > 0 { milestone = milestoneTitles[0] } - openURL := generateCompareURL(baseRepo, baseBranch, headBranchLabel, title, body, assignees, labelNames, projectNames, milestone) + openURL, err := generateCompareURL(baseRepo, baseBranch, headBranchLabel, title, body, assignees, labelNames, projectNames, milestone) + if err != nil { + return err + } // TODO could exceed max url length for explorer fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL)) return utils.OpenInBrowser(openURL) @@ -396,41 +400,46 @@ func determineTrackingBranch(remotes context.Remotes, headBranch string) *git.Tr return nil } -func getPrAndIssueQueryParams(title, body string, assignees, labels, projects []string, milestone string) string { - queryParams := "" +func withPrAndIssueQueryParams(baseURL, title, body string, assignees, labels, projects []string, milestone string) (string, error) { + u, err := url.Parse(baseURL) + if err != nil { + log.Fatal(err) + } + q := u.Query() if title != "" { - queryParams += "&title=" + url.QueryEscape(title) + q.Set("title", title) } if body != "" { - queryParams += "&body=" + url.QueryEscape(body) + q.Set("body", body) } if len(assignees) > 0 { - queryParams += "&assignees=" + url.QueryEscape(strings.Join(assignees, ",")) + q.Set("assignees", strings.Join(assignees, ",")) } if len(labels) > 0 { - queryParams += "&labels=" + url.QueryEscape(strings.Join(labels, ",")) + q.Set("labels", strings.Join(labels, ",")) } if len(projects) > 0 { - queryParams += "&projects=" + url.QueryEscape(strings.Join(projects, ",")) + q.Set("projects", strings.Join(projects, ",")) } if milestone != "" { - queryParams += "&milestone=" + url.QueryEscape(milestone) + q.Set("milestone", milestone) } - return strings.TrimLeft(queryParams, "&") + u.RawQuery = q.Encode() + return u.String(), nil } -func generateCompareURL(r ghrepo.Interface, base, head, title, body string, assignees, labels, projects []string, milestone string) string { +func generateCompareURL(r ghrepo.Interface, base, head, title, body string, assignees, labels, projects []string, milestone string) (string, error) { u := fmt.Sprintf( "https://github.com/%s/compare/%s...%s?expand=1", ghrepo.FullName(r), base, head, ) - queryParams := getPrAndIssueQueryParams(title, body, assignees, labels, projects, milestone) - if len(queryParams) > 0 { - u += "&" + getPrAndIssueQueryParams(title, body, assignees, labels, projects, milestone) + url, err := withPrAndIssueQueryParams(u, title, body, assignees, labels, projects, milestone) + if err != nil { + return "", err } - return u + return url, nil } var prCreateCmd = &cobra.Command{ From e910c039f0b619729d77c57ceb5b64da85d0ec3a Mon Sep 17 00:00:00 2001 From: AliabbasMerchant Date: Tue, 26 May 2020 07:50:59 +0530 Subject: [PATCH 4/4] Return error when it occurs --- command/pr_create.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/command/pr_create.go b/command/pr_create.go index 931187859..45ec801d4 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -3,7 +3,6 @@ package command import ( "errors" "fmt" - "log" "net/url" "strings" "time" @@ -403,7 +402,7 @@ func determineTrackingBranch(remotes context.Remotes, headBranch string) *git.Tr func withPrAndIssueQueryParams(baseURL, title, body string, assignees, labels, projects []string, milestone string) (string, error) { u, err := url.Parse(baseURL) if err != nil { - log.Fatal(err) + return "", nil } q := u.Query() if title != "" {