Skip to content

Commit b7521e1

Browse files
committed
fixup! Only find PRs using branch.<name>.merge if push.default = upstream
1 parent 3693b51 commit b7521e1

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

pkg/cmd/pr/shared/finder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (f *finder) parseCurrentBranch() (string, int, error) {
266266
if branchOwner != "" {
267267
if branchConfig.Push != "" {
268268
prHeadRef = strings.TrimPrefix(branchConfig.Push, branchConfig.PushRemoteName+"/")
269-
} else if pushDefault, _ := f.pushDefault(); pushDefault == "upstream" &&
269+
} else if pushDefault, _ := f.pushDefault(); (pushDefault == "upstream" || pushDefault == "tracking") &&
270270
strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") {
271271
prHeadRef = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/")
272272
}

pkg/cmd/pr/shared/finder_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,51 @@ func TestFind(t *testing.T) {
399399
wantPR: 13,
400400
wantRepo: "https://github.com/OWNER/REPO",
401401
},
402+
{
403+
name: "current branch with tracking (deprecated synonym of upstream) configuration",
404+
args: args{
405+
selector: "",
406+
fields: []string{"id", "number"},
407+
baseRepoFn: func() (ghrepo.Interface, error) {
408+
return ghrepo.FromFullName("OWNER/REPO")
409+
},
410+
branchFn: func() (string, error) {
411+
return "blueberries", nil
412+
},
413+
branchConfig: func(branch string) (c git.BranchConfig) {
414+
c.MergeRef = "refs/heads/blue-upstream-berries"
415+
c.RemoteName = "origin"
416+
c.PushRemoteName = "origin"
417+
c.Push = "origin/blue-upstream-berries"
418+
return
419+
},
420+
pushDefault: func() (string, error) { return "tracking", nil },
421+
remotesFn: func() (context.Remotes, error) {
422+
return context.Remotes{{
423+
Remote: &git.Remote{Name: "origin"},
424+
Repo: ghrepo.New("UPSTREAMOWNER", "REPO"),
425+
}}, nil
426+
},
427+
},
428+
httpStub: func(r *httpmock.Registry) {
429+
r.Register(
430+
httpmock.GraphQL(`query PullRequestForBranch\b`),
431+
httpmock.StringResponse(`{"data":{"repository":{
432+
"pullRequests":{"nodes":[
433+
{
434+
"number": 13,
435+
"state": "OPEN",
436+
"baseRefName": "main",
437+
"headRefName": "blue-upstream-berries",
438+
"isCrossRepository": true,
439+
"headRepositoryOwner": {"login":"UPSTREAMOWNER"}
440+
}
441+
]}
442+
}}}`))
443+
},
444+
wantPR: 13,
445+
wantRepo: "https://github.com/OWNER/REPO",
446+
},
402447
{
403448
name: "current branch made by pr checkout",
404449
args: args{

pkg/cmd/pr/status/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func prSelectorForCurrentBranch(gitClient *git.Client, baseRepo ghrepo.Interface
211211
if branchOwner != "" {
212212
if branchConfig.Push != "" {
213213
selector = strings.TrimPrefix(branchConfig.Push, branchConfig.PushRemoteName+"/")
214-
} else if pushDefault, _ := gitClient.Config(context.Background(), "push.default"); pushDefault == "upstream" &&
214+
} else if pushDefault, _ := gitClient.Config(context.Background(), "push.default"); (pushDefault == "upstream" || pushDefault == "tracking") &&
215215
strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") {
216216
selector = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/")
217217
}

pkg/cmd/pr/status/status_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ Requesting a code review from you
375375
}
376376
}
377377

378-
func Test_prSelectorForCurrentBranch(t *testing.T) {
378+
func Test_prSelectorForCurrentBranchPushDefaultUpstream(t *testing.T) {
379379
rs, cleanup := run.Stub()
380380
defer cleanup(t)
381381

@@ -406,3 +406,35 @@ func Test_prSelectorForCurrentBranch(t *testing.T) {
406406
t.Errorf("expected headRef to be \"Frederick888:main\", got %q", headRef)
407407
}
408408
}
409+
410+
func Test_prSelectorForCurrentBranchPushDefaultTracking(t *testing.T) {
411+
rs, cleanup := run.Stub()
412+
defer cleanup(t)
413+
414+
rs.Register(`git config --get-regexp \^branch\\.`, 0, heredoc.Doc(`
415+
branch.Frederick888/main.remote [email protected]:Frederick888/playground.git
416+
branch.Frederick888/main.merge refs/heads/main
417+
`))
418+
rs.Register(`git config remote.pushDefault`, 1, "")
419+
rs.Register(`git rev-parse --verify --quiet --abbrev-ref Frederick888/main@\{push\}`, 1, "")
420+
rs.Register(`git config push\.default`, 0, "tracking")
421+
422+
repo := ghrepo.NewWithHost("octocat", "playground", "github.com")
423+
rem := context.Remotes{
424+
&context.Remote{
425+
Remote: &git.Remote{Name: "origin"},
426+
Repo: repo,
427+
},
428+
}
429+
gitClient := &git.Client{GitPath: "some/path/git"}
430+
prNum, headRef, err := prSelectorForCurrentBranch(gitClient, repo, "Frederick888/main", rem)
431+
if err != nil {
432+
t.Fatalf("prSelectorForCurrentBranch error: %v", err)
433+
}
434+
if prNum != 0 {
435+
t.Errorf("expected prNum to be 0, got %q", prNum)
436+
}
437+
if headRef != "Frederick888:main" {
438+
t.Errorf("expected headRef to be \"Frederick888:main\", got %q", headRef)
439+
}
440+
}

0 commit comments

Comments
 (0)