From 0cef66dc674bd9324e51176b197dc36d9603380d Mon Sep 17 00:00:00 2001 From: sibis Date: Tue, 10 Mar 2020 22:33:47 +0530 Subject: [PATCH] Verify repo before viewing in the browser --- command/repo.go | 25 +++++++++++++++++++++++++ command/repo_test.go | 16 +++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/command/repo.go b/command/repo.go index 53b2ca8f8..8a4d1cc0e 100644 --- a/command/repo.go +++ b/command/repo.go @@ -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) + + } diff --git a/command/repo_test.go b/command/repo_test.go index da64712b7..3fd3357da 100644 --- a/command/repo_test.go +++ b/command/repo_test.go @@ -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