From 2895252e3bdc3f39cbc447c85829eb28fce3e70f Mon Sep 17 00:00:00 2001 From: vilmibm Date: Mon, 23 Mar 2020 14:26:31 -0500 Subject: [PATCH] respect -B when checking for existing pull requests --- api/queries_pr.go | 17 +++++++++++------ command/pr.go | 4 ++-- command/pr_create.go | 4 ++-- command/pr_create_test.go | 31 +++++++++++++++++++++++++++++-- 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/api/queries_pr.go b/api/queries_pr.go index be86dc679..b2e5f2f77 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -340,7 +340,7 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu return &resp.Repository.PullRequest, nil } -func PullRequestForBranch(client *Client, repo ghrepo.Interface, branch string) (*PullRequest, error) { +func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, headBranch string) (*PullRequest, error) { type response struct { Repository struct { PullRequests struct { @@ -376,9 +376,9 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, branch string) } }` - branchWithoutOwner := branch - if idx := strings.Index(branch, ":"); idx >= 0 { - branchWithoutOwner = branch[idx+1:] + branchWithoutOwner := headBranch + if idx := strings.Index(headBranch, ":"); idx >= 0 { + branchWithoutOwner = headBranch[idx+1:] } variables := map[string]interface{}{ @@ -394,12 +394,17 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, branch string) } for _, pr := range resp.Repository.PullRequests.Nodes { - if pr.HeadLabel() == branch { + if pr.HeadLabel() == headBranch { + if baseBranch != "" { + if pr.BaseRefName != baseBranch { + continue + } + } return &pr, nil } } - return nil, &NotFoundError{fmt.Errorf("no open pull requests found for branch %q", branch)} + return nil, &NotFoundError{fmt.Errorf("no open pull requests found for branch %q", headBranch)} } // CreatePullRequest creates a pull request in a GitHub repository diff --git a/command/pr.go b/command/pr.go index bd7743f46..55e00b0c5 100644 --- a/command/pr.go +++ b/command/pr.go @@ -288,7 +288,7 @@ func prView(cmd *cobra.Command, args []string) error { } } } else { - pr, err = api.PullRequestForBranch(apiClient, baseRepo, branchWithOwner) + pr, err = api.PullRequestForBranch(apiClient, baseRepo, "", branchWithOwner) if err != nil { return err } @@ -341,7 +341,7 @@ func prFromArg(apiClient *api.Client, baseRepo ghrepo.Interface, arg string) (*a return api.PullRequestByNumber(apiClient, baseRepo, prNumber) } - return api.PullRequestForBranch(apiClient, baseRepo, arg) + return api.PullRequestForBranch(apiClient, baseRepo, "", arg) } func prSelectorForCurrentBranch(ctx context.Context, baseRepo ghrepo.Interface) (prNumber int, prHeadRef string, err error) { diff --git a/command/pr_create.go b/command/pr_create.go index ca7800a19..2be922987 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -132,13 +132,13 @@ func prCreate(cmd *cobra.Command, _ []string) error { if headRepo != nil && !ghrepo.IsSame(baseRepo, headRepo) { headBranchLabel = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch) } - existingPR, err := api.PullRequestForBranch(client, baseRepo, headBranchLabel) + 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 already exists:\n%s", headBranchLabel, existingPR.URL) + return fmt.Errorf("a pull request for branch %q into branch %q already exists:\n%s", headBranchLabel, baseBranch, existingPR.URL) } } diff --git a/command/pr_create_test.go b/command/pr_create_test.go index 688dfd9c8..874b02a86 100644 --- a/command/pr_create_test.go +++ b/command/pr_create_test.go @@ -64,7 +64,8 @@ func TestPRCreate_alreadyExists(t *testing.T) { http.StubResponse(200, bytes.NewBufferString(` { "data": { "repository": { "pullRequests": { "nodes": [ { "url": "https://github.com/OWNER/REPO/pull/123", - "headRefName": "feature" } + "headRefName": "feature", + "baseRefName": "master" } ] } } } } `)) @@ -78,11 +79,37 @@ func TestPRCreate_alreadyExists(t *testing.T) { if err == nil { t.Fatal("error expected, got nil") } - if err.Error() != "a pull request for branch \"feature\" already exists:\nhttps://github.com/OWNER/REPO/pull/123" { + if err.Error() != "a pull request for branch \"feature\" into branch \"master\" already exists:\nhttps://github.com/OWNER/REPO/pull/123" { t.Errorf("got error %q", err) } } +func TestPRCreate_alreadyExistsDifferentBase(t *testing.T) { + initBlankContext("OWNER/REPO", "feature") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + http.StubResponse(200, bytes.NewBufferString(` + { "data": { "repository": { "pullRequests": { "nodes": [ + { "url": "https://github.com/OWNER/REPO/pull/123", + "headRefName": "feature", + "baseRefName": "master" } + ] } } } } + `)) + http.StubResponse(200, bytes.NewBufferString("{}")) + + cs, cmdTeardown := initCmdStubber() + defer cmdTeardown() + + cs.Stub("") // git status + cs.Stub("1234567890,commit 0\n2345678901,commit 1") // git log + cs.Stub("") // git rev-parse + + _, err := RunCommand(prCreateCmd, `pr create -BanotherBase -t"cool" -b"nah"`) + if err != nil { + t.Errorf("got unexpected error %q", err) + } +} + func TestPRCreate_web(t *testing.T) { initBlankContext("OWNER/REPO", "feature") http := initFakeHTTP()