Skip to content

Commit 0cef66d

Browse files
committed
Verify repo before viewing in the browser
1 parent 115b4b5 commit 0cef66d

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

command/repo.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,21 +388,46 @@ func repoView(cmd *cobra.Command, args []string) error {
388388
ctx := contextForCommand(cmd)
389389

390390
var openURL string
391+
var toView ghrepo.Interface
391392
if len(args) == 0 {
392393
baseRepo, err := determineBaseRepo(cmd, ctx)
393394
if err != nil {
394395
return err
395396
}
396397
openURL = fmt.Sprintf("https://github.com/%s", ghrepo.FullName(baseRepo))
398+
toView = baseRepo
397399
} else {
398400
repoArg := args[0]
399401
if isURL(repoArg) {
400402
openURL = repoArg
403+
parsedURL, err := url.Parse(repoArg)
404+
if err != nil {
405+
return fmt.Errorf("did not understand argument: %w", err)
406+
}
407+
408+
toView, err = ghrepo.FromURL(parsedURL)
409+
if err != nil {
410+
return fmt.Errorf("did not understand argument: %w", err)
411+
}
401412
} else {
413+
toView = ghrepo.FromFullName(repoArg)
402414
openURL = fmt.Sprintf("https://github.com/%s", repoArg)
403415
}
404416
}
405417

418+
apiClient, err := apiClientForContext(ctx)
419+
420+
if err != nil {
421+
return err
422+
}
423+
424+
_, err_message := api.GitHubRepo(apiClient, toView)
425+
if err_message != nil {
426+
return err_message
427+
}
428+
406429
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL))
407430
return utils.OpenInBrowser(openURL)
431+
432+
408433
}

command/repo_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,10 +579,14 @@ func TestRepoCreate_orgWithTeam(t *testing.T) {
579579
}
580580
}
581581

582+
582583
func TestRepoView(t *testing.T) {
583584
initBlankContext("OWNER/REPO", "master")
584585
http := initFakeHTTP()
585586
http.StubRepoResponse("OWNER", "REPO")
587+
http.StubResponse(200, bytes.NewBufferString(`
588+
{ }
589+
`))
586590

587591
var seenCmd *exec.Cmd
588592
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
@@ -606,13 +610,17 @@ func TestRepoView(t *testing.T) {
606610
eq(t, url, "https://github.com/OWNER/REPO")
607611
}
608612

613+
609614
func TestRepoView_ownerRepo(t *testing.T) {
610615
ctx := context.NewBlank()
611616
ctx.SetBranch("master")
612617
initContext = func() context.Context {
613618
return ctx
614619
}
615-
initFakeHTTP()
620+
http := initFakeHTTP()
621+
http.StubResponse(200, bytes.NewBufferString(`
622+
{ }
623+
`))
616624

617625
var seenCmd *exec.Cmd
618626
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
@@ -642,8 +650,10 @@ func TestRepoView_fullURL(t *testing.T) {
642650
initContext = func() context.Context {
643651
return ctx
644652
}
645-
initFakeHTTP()
646-
653+
http := initFakeHTTP()
654+
http.StubResponse(200, bytes.NewBufferString(`
655+
{ }
656+
`))
647657
var seenCmd *exec.Cmd
648658
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
649659
seenCmd = cmd

0 commit comments

Comments
 (0)