shorthand of view without username

This commit is contained in:
boonhong 2020-08-23 23:33:50 +08:00
parent b9292f582f
commit 931fee566e
2 changed files with 57 additions and 3 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghinstance"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
@ -63,6 +64,7 @@ func viewRun(opts *ViewOptions) error {
}
var toView ghrepo.Interface
apiClient := api.NewClientFromHTTP(httpClient)
if opts.RepoArg == "" {
var err error
toView, err = opts.BaseRepo()
@ -71,14 +73,20 @@ func viewRun(opts *ViewOptions) error {
}
} else {
var err error
toView, err = ghrepo.FromFullName(opts.RepoArg)
viewURL := opts.RepoArg
if !strings.Contains(viewURL, "/") {
currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default())
if err != nil {
return err
}
viewURL = currentUser + "/" + viewURL
}
toView, err = ghrepo.FromFullName(viewURL)
if err != nil {
return fmt.Errorf("argument error: %w", err)
}
}
apiClient := api.NewClientFromHTTP(httpClient)
repo, err := api.GitHubRepo(apiClient, toView)
if err != nil {
return err

View file

@ -469,3 +469,49 @@ func Test_ViewRun_NoDescription(t *testing.T) {
})
}
}
func Test_ViewRun_WithoutUsername(t *testing.T) {
reg := &httpmock.Registry{}
reg.Register(
httpmock.GraphQL(`query UserCurrent\b`),
httpmock.StringResponse(`
{ "data": { "viewer": {
"login": "OWNER"
}}}`))
reg.Register(
httpmock.GraphQL(`query RepositoryInfo\b`),
httpmock.StringResponse(`
{ "data": {
"repository": {
"description": "social distancing"
} } }`))
reg.Register(
httpmock.REST("GET", "repos/OWNER/REPO/readme"),
httpmock.StringResponse(`
{ "name": "readme.md",
"content": "IyB0cnVseSBjb29sIHJlYWRtZSBjaGVjayBpdCBvdXQ="}`))
io, _, stdout, stderr := iostreams.Test()
io.SetStdoutTTY(false)
opts := &ViewOptions{
RepoArg: "REPO",
HttpClient: func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
},
IO: io,
}
if err := viewRun(opts); err != nil {
t.Errorf("viewRun() error = %v", err)
}
assert.Equal(t, heredoc.Doc(`
name: OWNER/REPO
description: social distancing
--
# truly cool readme check it out
`), stdout.String())
assert.Equal(t, "", stderr.String())
reg.Verify(t)
}