Remove shadowed errors from prSelectorForCurrentBranch and cover with tests

This commit is contained in:
Tyler McGoffin 2025-01-08 15:20:17 -08:00
parent 94b2d4ec3b
commit 15ac566222
2 changed files with 45 additions and 6 deletions

View file

@ -197,18 +197,21 @@ func prSelectorForCurrentBranch(branchConfig git.BranchConfig, baseRepo ghrepo.I
return prNumber, prHeadRef, nil
}
var err error
var branchOwner string
if branchConfig.RemoteURL != nil {
// the branch merges from a remote specified by URL
if r, err := ghrepo.FromURL(branchConfig.RemoteURL); err == nil {
branchOwner = r.RepoOwner()
r, err := ghrepo.FromURL(branchConfig.RemoteURL)
if err != nil {
return 0, prHeadRef, err
}
branchOwner = r.RepoOwner()
} else if branchConfig.RemoteName != "" {
// the branch merges from a remote specified by name
if r, err := rem.FindByName(branchConfig.RemoteName); err == nil {
branchOwner = r.RepoOwner()
r, err := rem.FindByName(branchConfig.RemoteName)
if err != nil {
return 0, prHeadRef, err
}
branchOwner = r.RepoOwner()
}
selector := prHeadRef
@ -222,7 +225,7 @@ func prSelectorForCurrentBranch(branchConfig git.BranchConfig, baseRepo ghrepo.I
}
}
return 0, selector, err
return 0, selector, nil
}
func totalApprovals(pr *api.PullRequest) int {

View file

@ -2,6 +2,7 @@ package status
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
@ -527,6 +528,41 @@ func Test_prSelectorForCurrentBranch(t *testing.T) {
wantSelector: "forkName:main",
wantError: nil,
},
{
name: "Remote URL error",
branchConfig: git.BranchConfig{
RemoteURL: &url.URL{
Scheme: "ssh",
User: url.User("git"),
Host: "github.com",
Path: "/\\invalid?Path/",
},
},
prHeadRef: "Frederick888/main",
wantPrNumber: 0,
wantSelector: "Frederick888/main",
wantError: fmt.Errorf("invalid path: /\\invalid?Path/"),
},
{
name: "Remote Name error",
branchConfig: git.BranchConfig{
RemoteName: "nonexistentRemote",
},
prHeadRef: "Frederick888/main",
remotes: context.Remotes{
&context.Remote{
Remote: &git.Remote{Name: "origin"},
Repo: ghrepo.NewWithHost("forkName", "playground", "github.com"),
},
&context.Remote{
Remote: &git.Remote{Name: "upstream"},
Repo: ghrepo.NewWithHost("Frederick888", "playground", "github.com"),
},
},
wantPrNumber: 0,
wantSelector: "Frederick888/main",
wantError: fmt.Errorf("no matching remote found"),
},
}
for _, tt := range tests {