Add repo view command

```
$ gh repo
Work with GitHub repositories.

A repository can be supplied as an argument in any of the following formats:
- by owner/repo, e.g. "cli/cli"
- by URL, e.g. "https://github.com/cli/cli"

Usage:
  gh repo [command]

Available Commands:
  view        View a repository in the browser
```
This commit is contained in:
Scott Penrose 2020-02-09 00:00:05 -05:00
parent f6856d9783
commit 7c76fb645a
No known key found for this signature in database
GPG key ID: F4666FC471E2FE70
2 changed files with 146 additions and 0 deletions

57
command/repo.go Normal file
View file

@ -0,0 +1,57 @@
package command
import (
"fmt"
"strings"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/utils"
"github.com/spf13/cobra"
)
func init() {
RootCmd.AddCommand(repoCmd)
repoCmd.AddCommand(repoViewCmd)
}
var repoCmd = &cobra.Command{
Use: "repo",
Short: "View repositories",
Long: `Work with GitHub repositories.
A repository can be supplied as an argument in any of the following formats:
- by owner/repo, e.g. "cli/cli"
- by URL, e.g. "https://github.com/cli/cli"`,
}
var repoViewCmd = &cobra.Command{
Use: "view [{<owner/repo> | <url>}]",
Short: "View a repository in the browser",
Long: `View a repository specified by the argument in the browser.
Without an argument, the repository that belongs to the current
branch is opened.`,
RunE: repoView,
}
func repoView(cmd *cobra.Command, args []string) error {
ctx := contextForCommand(cmd)
baseRepo, err := determineBaseRepo(cmd, ctx)
if err != nil {
return err
}
var openURL string
if len(args) == 0 {
openURL = fmt.Sprintf("https://github.com/%s", ghrepo.FullName(*baseRepo))
} else {
if strings.HasPrefix(args[0], "http") {
openURL = args[0]
} else {
openURL = fmt.Sprintf("https://github.com/%s", args[0])
}
}
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", openURL)
return utils.OpenInBrowser(openURL)
}

89
command/repo_test.go Normal file
View file

@ -0,0 +1,89 @@
package command
import (
"os/exec"
"testing"
"github.com/cli/cli/utils"
)
func TestRepoView(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
var seenCmd *exec.Cmd
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
seenCmd = cmd
return &outputStub{}
})
defer restoreCmd()
output, err := RunCommand(repoViewCmd, "repo view")
if err != nil {
t.Errorf("error running command `repo view`: %v", err)
}
eq(t, output.String(), "")
eq(t, output.Stderr(), "Opening https://github.com/OWNER/REPO in your browser.\n")
if seenCmd == nil {
t.Fatal("expected a command to run")
}
url := seenCmd.Args[len(seenCmd.Args)-1]
eq(t, url, "https://github.com/OWNER/REPO")
}
func TestRepoView_ownerRepo(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
var seenCmd *exec.Cmd
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
seenCmd = cmd
return &outputStub{}
})
defer restoreCmd()
output, err := RunCommand(repoViewCmd, "repo view cli/cli")
if err != nil {
t.Errorf("error running command `repo view`: %v", err)
}
eq(t, output.String(), "")
eq(t, output.Stderr(), "Opening https://github.com/cli/cli in your browser.\n")
if seenCmd == nil {
t.Fatal("expected a command to run")
}
url := seenCmd.Args[len(seenCmd.Args)-1]
eq(t, url, "https://github.com/cli/cli")
}
func TestRepoView_fullURL(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
var seenCmd *exec.Cmd
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
seenCmd = cmd
return &outputStub{}
})
defer restoreCmd()
output, err := RunCommand(repoViewCmd, "repo view https://github.com/cli/cli")
if err != nil {
t.Errorf("error running command `repo view`: %v", err)
}
eq(t, output.String(), "")
eq(t, output.Stderr(), "Opening https://github.com/cli/cli in your browser.\n")
if seenCmd == nil {
t.Fatal("expected a command to run")
}
url := seenCmd.Args[len(seenCmd.Args)-1]
eq(t, url, "https://github.com/cli/cli")
}