X Tutup
Skip to content

Commit 931fee5

Browse files
committed
shorthand of view without username
1 parent b9292f5 commit 931fee5

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

pkg/cmd/repo/view/view.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/MakeNowJust/heredoc"
1010
"github.com/cli/cli/api"
11+
"github.com/cli/cli/internal/ghinstance"
1112
"github.com/cli/cli/internal/ghrepo"
1213
"github.com/cli/cli/pkg/cmdutil"
1314
"github.com/cli/cli/pkg/iostreams"
@@ -63,6 +64,7 @@ func viewRun(opts *ViewOptions) error {
6364
}
6465

6566
var toView ghrepo.Interface
67+
apiClient := api.NewClientFromHTTP(httpClient)
6668
if opts.RepoArg == "" {
6769
var err error
6870
toView, err = opts.BaseRepo()
@@ -71,14 +73,20 @@ func viewRun(opts *ViewOptions) error {
7173
}
7274
} else {
7375
var err error
74-
toView, err = ghrepo.FromFullName(opts.RepoArg)
76+
viewURL := opts.RepoArg
77+
if !strings.Contains(viewURL, "/") {
78+
currentUser, err := api.CurrentLoginName(apiClient, ghinstance.Default())
79+
if err != nil {
80+
return err
81+
}
82+
viewURL = currentUser + "/" + viewURL
83+
}
84+
toView, err = ghrepo.FromFullName(viewURL)
7585
if err != nil {
7686
return fmt.Errorf("argument error: %w", err)
7787
}
7888
}
7989

80-
apiClient := api.NewClientFromHTTP(httpClient)
81-
8290
repo, err := api.GitHubRepo(apiClient, toView)
8391
if err != nil {
8492
return err

pkg/cmd/repo/view/view_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,49 @@ func Test_ViewRun_NoDescription(t *testing.T) {
469469
})
470470
}
471471
}
472+
473+
func Test_ViewRun_WithoutUsername(t *testing.T) {
474+
reg := &httpmock.Registry{}
475+
reg.Register(
476+
httpmock.GraphQL(`query UserCurrent\b`),
477+
httpmock.StringResponse(`
478+
{ "data": { "viewer": {
479+
"login": "OWNER"
480+
}}}`))
481+
reg.Register(
482+
httpmock.GraphQL(`query RepositoryInfo\b`),
483+
httpmock.StringResponse(`
484+
{ "data": {
485+
"repository": {
486+
"description": "social distancing"
487+
} } }`))
488+
reg.Register(
489+
httpmock.REST("GET", "repos/OWNER/REPO/readme"),
490+
httpmock.StringResponse(`
491+
{ "name": "readme.md",
492+
"content": "IyB0cnVseSBjb29sIHJlYWRtZSBjaGVjayBpdCBvdXQ="}`))
493+
494+
io, _, stdout, stderr := iostreams.Test()
495+
io.SetStdoutTTY(false)
496+
497+
opts := &ViewOptions{
498+
RepoArg: "REPO",
499+
HttpClient: func() (*http.Client, error) {
500+
return &http.Client{Transport: reg}, nil
501+
},
502+
IO: io,
503+
}
504+
505+
if err := viewRun(opts); err != nil {
506+
t.Errorf("viewRun() error = %v", err)
507+
}
508+
509+
assert.Equal(t, heredoc.Doc(`
510+
name: OWNER/REPO
511+
description: social distancing
512+
--
513+
# truly cool readme check it out
514+
`), stdout.String())
515+
assert.Equal(t, "", stderr.String())
516+
reg.Verify(t)
517+
}

0 commit comments

Comments
 (0)
X Tutup