Merge pull request #49 from bchadwic/trunk

Fixed pr issues
This commit is contained in:
Benjamin Chadwick 2021-06-14 22:37:21 -07:00 committed by GitHub
commit 3d97aaf7f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 99 additions and 94 deletions

View file

@ -78,22 +78,13 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
opts.SelectorArg = args[0]
}
if err := cmdutil.MutuallyExclusive("cannot use --projects with --settings", opts.ProjectsFlag, opts.SettingsFlag); err != nil {
return err
}
if err := cmdutil.MutuallyExclusive("cannot use --projects with --wiki", opts.ProjectsFlag, opts.WikiFlag); err != nil {
return err
}
if err := cmdutil.MutuallyExclusive("cannot use --projects with --branch", opts.ProjectsFlag, opts.Branch != ""); err != nil {
return err
}
if err := cmdutil.MutuallyExclusive("cannot use --settings with --wiki", opts.SettingsFlag, opts.WikiFlag); err != nil {
return err
}
if err := cmdutil.MutuallyExclusive("cannot use --settings with --branch", opts.SettingsFlag, opts.Branch != ""); err != nil {
return err
}
if err := cmdutil.MutuallyExclusive("cannot use --wiki with --branch", opts.WikiFlag, opts.Branch != ""); err != nil {
if err := cmdutil.MutuallyExclusive(
"specify only one of `--branch`, `--projects`, `--wiki`, or `--settings`",
opts.Branch != "",
opts.WikiFlag,
opts.SettingsFlag,
opts.ProjectsFlag,
); err != nil {
return err
}
@ -114,7 +105,6 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
}
func runBrowse(opts *BrowseOptions) error {
baseRepo, err := opts.BaseRepo()
if err != nil {
return fmt.Errorf("unable to determine base repository: %w\nUse 'gh browse --help' for more information about browse\n", err)
@ -124,50 +114,46 @@ func runBrowse(opts *BrowseOptions) error {
if err != nil {
return fmt.Errorf("unable to create an http client: %w\nUse 'gh browse --help' for more information about browse\n", err)
}
url := ghrepo.GenerateRepoURL(baseRepo, "")
if opts.ProjectsFlag {
err := opts.Browser.Browse(url + "/projects")
return err
}
if opts.SettingsFlag {
err := opts.Browser.Browse(url + "/settings")
return err
}
if opts.WikiFlag {
err := opts.Browser.Browse(url + "/wiki")
return err
}
if isNumber(opts.SelectorArg) {
url += "/issues/" + opts.SelectorArg
err := opts.Browser.Browse(url)
return err
}
if opts.Branch != "" {
url += "/tree/" + opts.Branch + "/"
if opts.SelectorArg == "" {
if opts.ProjectsFlag {
url += "/projects"
}
if opts.SettingsFlag {
url += "/settings"
}
if opts.WikiFlag {
url += "/wiki"
}
if opts.Branch != "" {
url += "/tree/" + opts.Branch + "/"
}
} else {
apiClient := api.NewClientFromHTTP(httpClient)
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)
if err != nil {
return err
}
url += "/tree/" + branchName + "/"
}
if opts.SelectorArg != "" {
arr, err := parseFileArg(opts.SelectorArg)
if err != nil {
return err
}
if len(arr) > 1 {
url += arr[0] + "#L" + arr[1]
if isNumber(opts.SelectorArg) {
url += "/issues/" + opts.SelectorArg
} else {
url += arr[0]
arr, err := parseFileArg(opts.SelectorArg)
if err != nil {
return err
}
if opts.Branch != "" {
url += "/tree/" + opts.Branch + "/"
} else {
apiClient := api.NewClientFromHTTP(httpClient)
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)
if err != nil {
return err
}
url += "/tree/" + branchName + "/"
}
if opts.SelectorArg != "" {
if len(arr) > 1 {
url += arr[0] + "#L" + arr[1]
} else {
url += arr[0]
}
}
}
}
@ -175,8 +161,10 @@ func runBrowse(opts *BrowseOptions) error {
if opts.IO.IsStdoutTTY() && err == nil {
fmt.Fprintf(opts.IO.Out, "now opening %s in browser\n", url)
}
return err
}
func parseFileArg(fileArg string) ([]string, error) {
arr := strings.Split(fileArg, ":")
if len(arr) > 2 {
@ -185,7 +173,6 @@ func parseFileArg(fileArg string) ([]string, error) {
if len(arr) > 1 && !isNumber(arr[1]) {
return arr, fmt.Errorf("invalid line number after colon\nUse 'gh browse --help' for more information about browse\n")
}
return arr, nil
}

View file

@ -137,83 +137,101 @@ func Test_runBrowse(t *testing.T) {
opts: BrowseOptions{
SelectorArg: "",
},
baseRepo: ghrepo.New("jessica", "cli"),
defaultBranch: "trunk",
expectedURL: "https://github.com/jessica/cli/tree/trunk/",
},
{
name: "file argument",
opts: BrowseOptions{SelectorArg: "path/to/file.txt"},
baseRepo: ghrepo.New("ken", "cli"),
defaultBranch: "main",
expectedURL: "https://github.com/ken/cli/tree/main/path/to/file.txt",
},
{
name: "branch flag",
opts: BrowseOptions{
Branch: "trunk",
},
baseRepo: ghrepo.New("thanh", "cli"),
expectedURL: "https://github.com/thanh/cli/tree/trunk/",
baseRepo: ghrepo.New("jlsestak", "cli"),
expectedURL: "https://github.com/jlsestak/cli",
},
{
name: "settings flag",
opts: BrowseOptions{
SettingsFlag: true,
},
baseRepo: ghrepo.New("bchadwic", "cli"),
expectedURL: "https://github.com/bchadwic/cli/settings",
baseRepo: ghrepo.New("bchadwic", "ObscuredByClouds"),
expectedURL: "https://github.com/bchadwic/ObscuredByClouds/settings",
},
{
name: "projects flag",
opts: BrowseOptions{
ProjectsFlag: true,
},
baseRepo: ghrepo.New("bchadwic", "cli"),
expectedURL: "https://github.com/bchadwic/cli/projects",
baseRepo: ghrepo.New("ttran112", "7ate9"),
expectedURL: "https://github.com/ttran112/7ate9/projects",
},
{
name: "wiki flag",
opts: BrowseOptions{
WikiFlag: true,
},
baseRepo: ghrepo.New("bchadwic", "cli"),
expectedURL: "https://github.com/bchadwic/cli/wiki",
baseRepo: ghrepo.New("ravocean", "ThreatLevelMidnight"),
expectedURL: "https://github.com/ravocean/ThreatLevelMidnight/wiki",
},
{
name: "file argument",
opts: BrowseOptions{SelectorArg: "path/to/file.txt"},
baseRepo: ghrepo.New("ken", "mrprofessor"),
defaultBranch: "main",
expectedURL: "https://github.com/ken/mrprofessor/tree/main/path/to/file.txt",
},
{
name: "issue argument",
opts: BrowseOptions{
SelectorArg: "217",
},
baseRepo: ghrepo.New("bchadwic", "cli"),
expectedURL: "https://github.com/bchadwic/cli/issues/217",
baseRepo: ghrepo.New("kevin", "MinTy"),
expectedURL: "https://github.com/kevin/MinTy/issues/217",
},
{
name: "branch flag",
opts: BrowseOptions{
Branch: "trunk",
},
baseRepo: ghrepo.New("jlsestak", "CouldNotThinkOfARepoName"),
expectedURL: "https://github.com/jlsestak/CouldNotThinkOfARepoName/tree/trunk/",
},
{
name: "branch flag with file",
opts: BrowseOptions{
Branch: "trunk",
SelectorArg: "main.go",
},
baseRepo: ghrepo.New("bchadwic", "LedZeppelinIV"),
expectedURL: "https://github.com/bchadwic/LedZeppelinIV/tree/trunk/main.go",
},
{
name: "file with line number",
opts: BrowseOptions{
SelectorArg: "path/to/file.txt:32",
},
baseRepo: ghrepo.New("bchadwic", "cli"),
baseRepo: ghrepo.New("ravocean", "angur"),
defaultBranch: "trunk",
expectedURL: "https://github.com/bchadwic/cli/tree/trunk/path/to/file.txt#L32",
expectedURL: "https://github.com/ravocean/angur/tree/trunk/path/to/file.txt#L32",
},
{
name: "file with invalid line number",
opts: BrowseOptions{
SelectorArg: "path/to/file.txt:32:32",
},
baseRepo: ghrepo.New("bchadwic", "cli"),
defaultBranch: "trunk",
wantsErr: true,
baseRepo: ghrepo.New("ttran112", "ttrain211"),
wantsErr: true,
},
{
name: "file with line argument",
name: "branch with issue number",
opts: BrowseOptions{
SelectorArg: "path/to/file.txt:32",
SelectorArg: "217",
Branch: "trunk",
},
baseRepo: ghrepo.New("bchadwic", "cli"),
defaultBranch: "trunk",
expectedURL: "https://github.com/bchadwic/cli/tree/trunk/path/to/file.txt#L32",
baseRepo: ghrepo.New("ken", "grc"),
wantsErr: false,
expectedURL: "https://github.com/ken/grc/issues/217",
},
{
name: "opening branch file with line number",
opts: BrowseOptions{
Branch: "first-browse-pull",
SelectorArg: "browse.go:32",
},
baseRepo: ghrepo.New("github", "ThankYouGitHub"),
wantsErr: false,
expectedURL: "https://github.com/github/ThankYouGitHub/tree/first-browse-pull/browse.go#L32",
},
}