From aa0f2de885b6902dbcedfc7f7c45c0f91a879099 Mon Sep 17 00:00:00 2001 From: Kousik Mitra Date: Fri, 14 Apr 2023 02:15:58 +0530 Subject: [PATCH] Return error on no-browser option if repo don't exists --- api/queries_repo.go | 20 ++++++++++ api/queries_repo_test.go | 70 +++++++++++++++++++++++++++++++++++ pkg/cmd/browse/browse.go | 14 ++++++- pkg/cmd/browse/browse_test.go | 11 ++++++ 4 files changed, 114 insertions(+), 1 deletion(-) diff --git a/api/queries_repo.go b/api/queries_repo.go index 06bea47bf..3b7e2e773 100644 --- a/api/queries_repo.go +++ b/api/queries_repo.go @@ -16,6 +16,7 @@ import ( "golang.org/x/sync/errgroup" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/go-gh/pkg/api" ghAPI "github.com/cli/go-gh/pkg/api" "github.com/shurcooL/githubv4" ) @@ -1370,3 +1371,22 @@ func GetRepoIDs(client *Client, host string, repositories []ghrepo.Interface) ([ } return result, nil } + +// RenameRepo renames the repository on GitHub and returns the renamed repository +func RepoExists(client *Client, repo ghrepo.Interface) (bool, error) { + path := fmt.Sprintf("%srepos/%s/%s", ghinstance.RESTPrefix(repo.RepoHost()), repo.RepoOwner(), repo.RepoName()) + + resp, err := client.HTTP().Head(path) + if err != nil { + return false, err + } + + switch resp.StatusCode { + case 200: + return true, nil + case 404: + return false, nil + default: + return false, api.HandleHTTPError(resp) + } +} diff --git a/api/queries_repo_test.go b/api/queries_repo_test.go index 4ea14d0f5..1c790e3d8 100644 --- a/api/queries_repo_test.go +++ b/api/queries_repo_test.go @@ -8,6 +8,7 @@ import ( "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/pkg/httpmock" + "github.com/stretchr/testify/assert" ) func TestGitHubRepo_notFound(t *testing.T) { @@ -467,3 +468,72 @@ func TestDisplayName(t *testing.T) { } } } + +func TestRepoExists(t *testing.T) { + tests := []struct { + name string + httpStub func(*httpmock.Registry) + repo ghrepo.Interface + existCheck bool + wantErrMsg string + }{ + { + name: "repo exists", + httpStub: func(r *httpmock.Registry) { + r.Register( + httpmock.REST("HEAD", "repos/OWNER/REPO"), + httpmock.StringResponse("{}"), + ) + }, + repo: ghrepo.New("OWNER", "REPO"), + existCheck: true, + wantErrMsg: "", + }, + { + name: "repo does not exists", + httpStub: func(r *httpmock.Registry) { + r.Register( + httpmock.REST("HEAD", "repos/OWNER/REPO"), + httpmock.StatusStringResponse(404, "Not Found"), + ) + }, + repo: ghrepo.New("OWNER", "REPO"), + existCheck: false, + wantErrMsg: "", + }, + { + name: "http error", + httpStub: func(r *httpmock.Registry) { + r.Register( + httpmock.REST("HEAD", "repos/OWNER/REPO"), + httpmock.StatusStringResponse(500, "Internal Server Error"), + ) + }, + repo: ghrepo.New("OWNER", "REPO"), + existCheck: false, + wantErrMsg: "HTTP 500 (https://api.github.com/repos/OWNER/REPO)", + }, + } + for _, tt := range tests { + reg := &httpmock.Registry{} + if tt.httpStub != nil { + tt.httpStub(reg) + } + + client := newTestClient(reg) + + t.Run(tt.name, func(t *testing.T) { + exist, err := RepoExists(client, ghrepo.New("OWNER", "REPO")) + if tt.wantErrMsg != "" { + assert.Equal(t, tt.wantErrMsg, err.Error()) + } else { + assert.NoError(t, err) + } + + if exist != tt.existCheck { + t.Errorf("RepoExists() returns %v, expected %v", exist, tt.existCheck) + return + } + }) + } +} diff --git a/pkg/cmd/browse/browse.go b/pkg/cmd/browse/browse.go index 42664d77c..ae78c5155 100644 --- a/pkg/cmd/browse/browse.go +++ b/pkg/cmd/browse/browse.go @@ -180,7 +180,19 @@ func runBrowse(opts *BrowseOptions) error { url := ghrepo.GenerateRepoURL(baseRepo, "%s", section) if opts.NoBrowserFlag { - _, err := fmt.Fprintln(opts.IO.Out, url) + client, err := opts.HttpClient() + if err != nil { + return err + } + + exist, err := api.RepoExists(api.NewClientFromHTTP(client), baseRepo) + if err != nil { + return err + } + if !exist { + return fmt.Errorf("%s doesn't exists", text.DisplayURL(url)) + } + _, err = fmt.Fprintln(opts.IO.Out, url) return err } diff --git a/pkg/cmd/browse/browse_test.go b/pkg/cmd/browse/browse_test.go index 4995e5cdf..9da60dcb1 100644 --- a/pkg/cmd/browse/browse_test.go +++ b/pkg/cmd/browse/browse_test.go @@ -232,6 +232,7 @@ func Test_runBrowse(t *testing.T) { tests := []struct { name string opts BrowseOptions + httpStub func(*httpmock.Registry) baseRepo ghrepo.Interface defaultBranch string expectedURL string @@ -432,6 +433,12 @@ func Test_runBrowse(t *testing.T) { SelectorArg: "init.rb:6", NoBrowserFlag: true, }, + httpStub: func(r *httpmock.Registry) { + r.Register( + httpmock.REST("HEAD", "repos/mislav/will_paginate"), + httpmock.StringResponse("{}"), + ) + }, baseRepo: ghrepo.New("mislav", "will_paginate"), wantsErr: false, expectedURL: "https://github.com/mislav/will_paginate/blob/3-0-stable/init.rb?plain=1#L6", @@ -556,6 +563,10 @@ func Test_runBrowse(t *testing.T) { reg.StubRepoInfoResponse(tt.baseRepo.RepoOwner(), tt.baseRepo.RepoName(), tt.defaultBranch) } + if tt.httpStub != nil { + tt.httpStub(®) + } + opts := tt.opts opts.IO = ios opts.BaseRepo = func() (ghrepo.Interface, error) {