Verify repo before viewing in the browser

This commit is contained in:
sibis 2020-03-10 22:33:47 +05:30
parent 115b4b5ee9
commit 0cef66dc67
2 changed files with 38 additions and 3 deletions

View file

@ -388,21 +388,46 @@ func repoView(cmd *cobra.Command, args []string) error {
ctx := contextForCommand(cmd)
var openURL string
var toView ghrepo.Interface
if len(args) == 0 {
baseRepo, err := determineBaseRepo(cmd, ctx)
if err != nil {
return err
}
openURL = fmt.Sprintf("https://github.com/%s", ghrepo.FullName(baseRepo))
toView = baseRepo
} else {
repoArg := args[0]
if isURL(repoArg) {
openURL = repoArg
parsedURL, err := url.Parse(repoArg)
if err != nil {
return fmt.Errorf("did not understand argument: %w", err)
}
toView, err = ghrepo.FromURL(parsedURL)
if err != nil {
return fmt.Errorf("did not understand argument: %w", err)
}
} else {
toView = ghrepo.FromFullName(repoArg)
openURL = fmt.Sprintf("https://github.com/%s", repoArg)
}
}
apiClient, err := apiClientForContext(ctx)
if err != nil {
return err
}
_, err_message := api.GitHubRepo(apiClient, toView)
if err_message != nil {
return err_message
}
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL))
return utils.OpenInBrowser(openURL)
}

View file

@ -579,10 +579,14 @@ func TestRepoCreate_orgWithTeam(t *testing.T) {
}
}
func TestRepoView(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
http.StubResponse(200, bytes.NewBufferString(`
{ }
`))
var seenCmd *exec.Cmd
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
@ -606,13 +610,17 @@ func TestRepoView(t *testing.T) {
eq(t, url, "https://github.com/OWNER/REPO")
}
func TestRepoView_ownerRepo(t *testing.T) {
ctx := context.NewBlank()
ctx.SetBranch("master")
initContext = func() context.Context {
return ctx
}
initFakeHTTP()
http := initFakeHTTP()
http.StubResponse(200, bytes.NewBufferString(`
{ }
`))
var seenCmd *exec.Cmd
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
@ -642,8 +650,10 @@ func TestRepoView_fullURL(t *testing.T) {
initContext = func() context.Context {
return ctx
}
initFakeHTTP()
http := initFakeHTTP()
http.StubResponse(200, bytes.NewBufferString(`
{ }
`))
var seenCmd *exec.Cmd
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
seenCmd = cmd