From 931fee566e2c0c186fd6259900a61f90d97deb18 Mon Sep 17 00:00:00 2001 From: boonhong Date: Sun, 23 Aug 2020 23:33:50 +0800 Subject: [PATCH] shorthand of view without username --- pkg/cmd/repo/view/view.go | 14 ++++++++--- pkg/cmd/repo/view/view_test.go | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/repo/view/view.go b/pkg/cmd/repo/view/view.go index a508317b0..adcf384a4 100644 --- a/pkg/cmd/repo/view/view.go +++ b/pkg/cmd/repo/view/view.go @@ -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 diff --git a/pkg/cmd/repo/view/view_test.go b/pkg/cmd/repo/view/view_test.go index 5a6d08702..7dba0f6df 100644 --- a/pkg/cmd/repo/view/view_test.go +++ b/pkg/cmd/repo/view/view_test.go @@ -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) +}