Skip to content

Commit

Permalink
fixup! Only find PRs using branch.<name>.merge if push.default = upst…
Browse files Browse the repository at this point in the history
…ream
  • Loading branch information
Frederick888 committed Jul 25, 2024
1 parent 3693b51 commit b7521e1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/pr/shared/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (f *finder) parseCurrentBranch() (string, int, error) {
if branchOwner != "" {
if branchConfig.Push != "" {
prHeadRef = strings.TrimPrefix(branchConfig.Push, branchConfig.PushRemoteName+"/")
} else if pushDefault, _ := f.pushDefault(); pushDefault == "upstream" &&
} else if pushDefault, _ := f.pushDefault(); (pushDefault == "upstream" || pushDefault == "tracking") &&
strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") {
prHeadRef = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/")
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/cmd/pr/shared/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,51 @@ func TestFind(t *testing.T) {
wantPR: 13,
wantRepo: "https://github.com/OWNER/REPO",
},
{
name: "current branch with tracking (deprecated synonym of upstream) configuration",
args: args{
selector: "",
fields: []string{"id", "number"},
baseRepoFn: func() (ghrepo.Interface, error) {
return ghrepo.FromFullName("OWNER/REPO")
},
branchFn: func() (string, error) {
return "blueberries", nil
},
branchConfig: func(branch string) (c git.BranchConfig) {
c.MergeRef = "refs/heads/blue-upstream-berries"
c.RemoteName = "origin"
c.PushRemoteName = "origin"
c.Push = "origin/blue-upstream-berries"
return
},
pushDefault: func() (string, error) { return "tracking", nil },
remotesFn: func() (context.Remotes, error) {
return context.Remotes{{
Remote: &git.Remote{Name: "origin"},
Repo: ghrepo.New("UPSTREAMOWNER", "REPO"),
}}, nil
},
},
httpStub: func(r *httpmock.Registry) {
r.Register(
httpmock.GraphQL(`query PullRequestForBranch\b`),
httpmock.StringResponse(`{"data":{"repository":{
"pullRequests":{"nodes":[
{
"number": 13,
"state": "OPEN",
"baseRefName": "main",
"headRefName": "blue-upstream-berries",
"isCrossRepository": true,
"headRepositoryOwner": {"login":"UPSTREAMOWNER"}
}
]}
}}}`))
},
wantPR: 13,
wantRepo: "https://github.com/OWNER/REPO",
},
{
name: "current branch made by pr checkout",
args: args{
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/pr/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func prSelectorForCurrentBranch(gitClient *git.Client, baseRepo ghrepo.Interface
if branchOwner != "" {
if branchConfig.Push != "" {
selector = strings.TrimPrefix(branchConfig.Push, branchConfig.PushRemoteName+"/")
} else if pushDefault, _ := gitClient.Config(context.Background(), "push.default"); pushDefault == "upstream" &&
} else if pushDefault, _ := gitClient.Config(context.Background(), "push.default"); (pushDefault == "upstream" || pushDefault == "tracking") &&
strings.HasPrefix(branchConfig.MergeRef, "refs/heads/") {
selector = strings.TrimPrefix(branchConfig.MergeRef, "refs/heads/")
}
Expand Down
34 changes: 33 additions & 1 deletion pkg/cmd/pr/status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ Requesting a code review from you
}
}

func Test_prSelectorForCurrentBranch(t *testing.T) {
func Test_prSelectorForCurrentBranchPushDefaultUpstream(t *testing.T) {
rs, cleanup := run.Stub()
defer cleanup(t)

Expand Down Expand Up @@ -406,3 +406,35 @@ func Test_prSelectorForCurrentBranch(t *testing.T) {
t.Errorf("expected headRef to be \"Frederick888:main\", got %q", headRef)
}
}

func Test_prSelectorForCurrentBranchPushDefaultTracking(t *testing.T) {
rs, cleanup := run.Stub()
defer cleanup(t)

rs.Register(`git config --get-regexp \^branch\\.`, 0, heredoc.Doc(`
branch.Frederick888/main.remote [email protected]:Frederick888/playground.git
branch.Frederick888/main.merge refs/heads/main
`))
rs.Register(`git config remote.pushDefault`, 1, "")
rs.Register(`git rev-parse --verify --quiet --abbrev-ref Frederick888/main@\{push\}`, 1, "")
rs.Register(`git config push\.default`, 0, "tracking")

repo := ghrepo.NewWithHost("octocat", "playground", "github.com")
rem := context.Remotes{
&context.Remote{
Remote: &git.Remote{Name: "origin"},
Repo: repo,
},
}
gitClient := &git.Client{GitPath: "some/path/git"}
prNum, headRef, err := prSelectorForCurrentBranch(gitClient, repo, "Frederick888/main", rem)
if err != nil {
t.Fatalf("prSelectorForCurrentBranch error: %v", err)
}
if prNum != 0 {
t.Errorf("expected prNum to be 0, got %q", prNum)
}
if headRef != "Frederick888:main" {
t.Errorf("expected headRef to be \"Frederick888:main\", got %q", headRef)
}
}

0 comments on commit b7521e1

Please sign in to comment.