Skip to content

Commit 2895252

Browse files
committed
respect -B when checking for existing pull requests
1 parent deb7ee6 commit 2895252

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

api/queries_pr.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
340340
return &resp.Repository.PullRequest, nil
341341
}
342342

343-
func PullRequestForBranch(client *Client, repo ghrepo.Interface, branch string) (*PullRequest, error) {
343+
func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, headBranch string) (*PullRequest, error) {
344344
type response struct {
345345
Repository struct {
346346
PullRequests struct {
@@ -376,9 +376,9 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, branch string)
376376
}
377377
}`
378378

379-
branchWithoutOwner := branch
380-
if idx := strings.Index(branch, ":"); idx >= 0 {
381-
branchWithoutOwner = branch[idx+1:]
379+
branchWithoutOwner := headBranch
380+
if idx := strings.Index(headBranch, ":"); idx >= 0 {
381+
branchWithoutOwner = headBranch[idx+1:]
382382
}
383383

384384
variables := map[string]interface{}{
@@ -394,12 +394,17 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, branch string)
394394
}
395395

396396
for _, pr := range resp.Repository.PullRequests.Nodes {
397-
if pr.HeadLabel() == branch {
397+
if pr.HeadLabel() == headBranch {
398+
if baseBranch != "" {
399+
if pr.BaseRefName != baseBranch {
400+
continue
401+
}
402+
}
398403
return &pr, nil
399404
}
400405
}
401406

402-
return nil, &NotFoundError{fmt.Errorf("no open pull requests found for branch %q", branch)}
407+
return nil, &NotFoundError{fmt.Errorf("no open pull requests found for branch %q", headBranch)}
403408
}
404409

405410
// CreatePullRequest creates a pull request in a GitHub repository

command/pr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func prView(cmd *cobra.Command, args []string) error {
288288
}
289289
}
290290
} else {
291-
pr, err = api.PullRequestForBranch(apiClient, baseRepo, branchWithOwner)
291+
pr, err = api.PullRequestForBranch(apiClient, baseRepo, "", branchWithOwner)
292292
if err != nil {
293293
return err
294294
}
@@ -341,7 +341,7 @@ func prFromArg(apiClient *api.Client, baseRepo ghrepo.Interface, arg string) (*a
341341
return api.PullRequestByNumber(apiClient, baseRepo, prNumber)
342342
}
343343

344-
return api.PullRequestForBranch(apiClient, baseRepo, arg)
344+
return api.PullRequestForBranch(apiClient, baseRepo, "", arg)
345345
}
346346

347347
func prSelectorForCurrentBranch(ctx context.Context, baseRepo ghrepo.Interface) (prNumber int, prHeadRef string, err error) {

command/pr_create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ func prCreate(cmd *cobra.Command, _ []string) error {
132132
if headRepo != nil && !ghrepo.IsSame(baseRepo, headRepo) {
133133
headBranchLabel = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch)
134134
}
135-
existingPR, err := api.PullRequestForBranch(client, baseRepo, headBranchLabel)
135+
existingPR, err := api.PullRequestForBranch(client, baseRepo, baseBranch, headBranchLabel)
136136
var notFound *api.NotFoundError
137137
if err != nil && !errors.As(err, &notFound) {
138138
return fmt.Errorf("error checking for existing pull request: %w", err)
139139
}
140140
if err == nil {
141-
return fmt.Errorf("a pull request for branch %q already exists:\n%s", headBranchLabel, existingPR.URL)
141+
return fmt.Errorf("a pull request for branch %q into branch %q already exists:\n%s", headBranchLabel, baseBranch, existingPR.URL)
142142
}
143143
}
144144

command/pr_create_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ func TestPRCreate_alreadyExists(t *testing.T) {
6464
http.StubResponse(200, bytes.NewBufferString(`
6565
{ "data": { "repository": { "pullRequests": { "nodes": [
6666
{ "url": "https://github.com/OWNER/REPO/pull/123",
67-
"headRefName": "feature" }
67+
"headRefName": "feature",
68+
"baseRefName": "master" }
6869
] } } } }
6970
`))
7071

@@ -78,11 +79,37 @@ func TestPRCreate_alreadyExists(t *testing.T) {
7879
if err == nil {
7980
t.Fatal("error expected, got nil")
8081
}
81-
if err.Error() != "a pull request for branch \"feature\" already exists:\nhttps://github.com/OWNER/REPO/pull/123" {
82+
if err.Error() != "a pull request for branch \"feature\" into branch \"master\" already exists:\nhttps://github.com/OWNER/REPO/pull/123" {
8283
t.Errorf("got error %q", err)
8384
}
8485
}
8586

87+
func TestPRCreate_alreadyExistsDifferentBase(t *testing.T) {
88+
initBlankContext("OWNER/REPO", "feature")
89+
http := initFakeHTTP()
90+
http.StubRepoResponse("OWNER", "REPO")
91+
http.StubResponse(200, bytes.NewBufferString(`
92+
{ "data": { "repository": { "pullRequests": { "nodes": [
93+
{ "url": "https://github.com/OWNER/REPO/pull/123",
94+
"headRefName": "feature",
95+
"baseRefName": "master" }
96+
] } } } }
97+
`))
98+
http.StubResponse(200, bytes.NewBufferString("{}"))
99+
100+
cs, cmdTeardown := initCmdStubber()
101+
defer cmdTeardown()
102+
103+
cs.Stub("") // git status
104+
cs.Stub("1234567890,commit 0\n2345678901,commit 1") // git log
105+
cs.Stub("") // git rev-parse
106+
107+
_, err := RunCommand(prCreateCmd, `pr create -BanotherBase -t"cool" -b"nah"`)
108+
if err != nil {
109+
t.Errorf("got unexpected error %q", err)
110+
}
111+
}
112+
86113
func TestPRCreate_web(t *testing.T) {
87114
initBlankContext("OWNER/REPO", "feature")
88115
http := initFakeHTTP()

0 commit comments

Comments
 (0)