just swap BaseRepo implementation

This commit is contained in:
vilmibm 2020-07-23 11:58:22 -05:00
parent 086fb48d31
commit d92c80b560
4 changed files with 39 additions and 36 deletions

View file

@ -90,26 +90,6 @@ func init() {
}
return httpClient(token), nil
},
ResolvedBaseRepo: func(client *http.Client) (ghrepo.Interface, error) {
// TODO this may be abandoned when we stop trying to be magical about picking base repos for
// users. We can merge this with BaseRepo at that point.
apiClient := api.NewClientFromHTTP(client)
ctx := context.New()
remotes, err := ctx.Remotes()
if err != nil {
return nil, err
}
repoContext, err := context.ResolveRemotesToRepos(remotes, apiClient, "")
if err != nil {
return nil, err
}
baseRepo, err := repoContext.BaseRepo()
if err != nil {
return nil, err
}
return baseRepo, nil
},
BaseRepo: func() (ghrepo.Interface, error) {
// TODO: decouple from `context`
ctx := context.New()

View file

@ -9,6 +9,7 @@ import (
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/api"
"github.com/cli/cli/context"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
@ -17,9 +18,9 @@ import (
)
type ViewOptions struct {
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
ResolvedBaseRepo func(*http.Client) (ghrepo.Interface, error)
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
BaseRepo func() (ghrepo.Interface, error)
RepoArg string
Web bool
@ -27,9 +28,32 @@ type ViewOptions struct {
func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command {
opts := ViewOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
ResolvedBaseRepo: f.ResolvedBaseRepo,
IO: f.IOStreams,
HttpClient: f.HttpClient,
BaseRepo: func() (ghrepo.Interface, error) {
httpClient, err := f.HttpClient()
if err != nil {
return nil, err
}
apiClient := api.NewClientFromHTTP(httpClient)
ctx := context.New()
remotes, err := ctx.Remotes()
if err != nil {
return nil, err
}
repoContext, err := context.ResolveRemotesToRepos(remotes, apiClient, "")
if err != nil {
return nil, err
}
baseRepo, err := repoContext.BaseRepo()
if err != nil {
return nil, err
}
return baseRepo, nil
},
}
cmd := &cobra.Command{
@ -66,7 +90,7 @@ func viewRun(opts *ViewOptions) error {
var toView ghrepo.Interface
if opts.RepoArg == "" {
var err error
toView, err = opts.ResolvedBaseRepo(httpClient)
toView, err = opts.BaseRepo()
if err != nil {
return err
}

View file

@ -113,7 +113,7 @@ func Test_RepoView_Web(t *testing.T) {
HttpClient: func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
},
ResolvedBaseRepo: func(_ *http.Client) (ghrepo.Interface, error) {
BaseRepo: func() (ghrepo.Interface, error) {
return ghrepo.New("OWNER", "REPO"), nil
},
}
@ -225,7 +225,7 @@ func Test_ViewRun(t *testing.T) {
tt.repoName = "OWNER/REPO"
}
tt.opts.ResolvedBaseRepo = func(_ *http.Client) (ghrepo.Interface, error) {
tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
repo, _ := ghrepo.FromFullName(tt.repoName)
return repo, nil
}
@ -312,7 +312,7 @@ func Test_ViewRun_NonMarkdownReadme(t *testing.T) {
HttpClient: func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
},
ResolvedBaseRepo: func(_ *http.Client) (ghrepo.Interface, error) {
BaseRepo: func() (ghrepo.Interface, error) {
return ghrepo.New("OWNER", "REPO"), nil
},
}
@ -378,7 +378,7 @@ func Test_ViewRun_NoReadme(t *testing.T) {
HttpClient: func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
},
ResolvedBaseRepo: func(_ *http.Client) (ghrepo.Interface, error) {
BaseRepo: func() (ghrepo.Interface, error) {
return ghrepo.New("OWNER", "REPO"), nil
},
}
@ -448,7 +448,7 @@ func Test_ViewRun_NoDescription(t *testing.T) {
HttpClient: func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
},
ResolvedBaseRepo: func(_ *http.Client) (ghrepo.Interface, error) {
BaseRepo: func() (ghrepo.Interface, error) {
return ghrepo.New("OWNER", "REPO"), nil
},
}

View file

@ -8,8 +8,7 @@ import (
)
type Factory struct {
IOStreams *iostreams.IOStreams
HttpClient func() (*http.Client, error)
ResolvedBaseRepo func(*http.Client) (ghrepo.Interface, error)
BaseRepo func() (ghrepo.Interface, error)
IOStreams *iostreams.IOStreams
HttpClient func() (*http.Client, error)
BaseRepo func() (ghrepo.Interface, error)
}