Return error on no-browser option if repo don't exists

This commit is contained in:
Kousik Mitra 2023-04-14 02:15:58 +05:30
parent c0855a3e61
commit aa0f2de885
4 changed files with 114 additions and 1 deletions

View file

@ -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)
}
}

View file

@ -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
}
})
}
}

View file

@ -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
}

View file

@ -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(&reg)
}
opts := tt.opts
opts.IO = ios
opts.BaseRepo = func() (ghrepo.Interface, error) {