Skip to content

Commit 3d97aaf

Browse files
authored
Merge pull request cli#49 from bchadwic/trunk
Fixed pr issues
2 parents 707897f + 679d396 commit 3d97aaf

File tree

2 files changed

+98
-93
lines changed

2 files changed

+98
-93
lines changed

pkg/cmd/browse/browse.go

Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,13 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
7878
opts.SelectorArg = args[0]
7979
}
8080

81-
if err := cmdutil.MutuallyExclusive("cannot use --projects with --settings", opts.ProjectsFlag, opts.SettingsFlag); err != nil {
82-
return err
83-
}
84-
if err := cmdutil.MutuallyExclusive("cannot use --projects with --wiki", opts.ProjectsFlag, opts.WikiFlag); err != nil {
85-
return err
86-
}
87-
if err := cmdutil.MutuallyExclusive("cannot use --projects with --branch", opts.ProjectsFlag, opts.Branch != ""); err != nil {
88-
return err
89-
}
90-
if err := cmdutil.MutuallyExclusive("cannot use --settings with --wiki", opts.SettingsFlag, opts.WikiFlag); err != nil {
91-
return err
92-
}
93-
if err := cmdutil.MutuallyExclusive("cannot use --settings with --branch", opts.SettingsFlag, opts.Branch != ""); err != nil {
94-
return err
95-
}
96-
if err := cmdutil.MutuallyExclusive("cannot use --wiki with --branch", opts.WikiFlag, opts.Branch != ""); err != nil {
81+
if err := cmdutil.MutuallyExclusive(
82+
"specify only one of `--branch`, `--projects`, `--wiki`, or `--settings`",
83+
opts.Branch != "",
84+
opts.WikiFlag,
85+
opts.SettingsFlag,
86+
opts.ProjectsFlag,
87+
); err != nil {
9788
return err
9889
}
9990

@@ -114,7 +105,6 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
114105
}
115106

116107
func runBrowse(opts *BrowseOptions) error {
117-
118108
baseRepo, err := opts.BaseRepo()
119109
if err != nil {
120110
return fmt.Errorf("unable to determine base repository: %w\nUse 'gh browse --help' for more information about browse\n", err)
@@ -124,59 +114,57 @@ func runBrowse(opts *BrowseOptions) error {
124114
if err != nil {
125115
return fmt.Errorf("unable to create an http client: %w\nUse 'gh browse --help' for more information about browse\n", err)
126116
}
127-
128117
url := ghrepo.GenerateRepoURL(baseRepo, "")
129118

130-
if opts.ProjectsFlag {
131-
err := opts.Browser.Browse(url + "/projects")
132-
return err
133-
}
134-
135-
if opts.SettingsFlag {
136-
err := opts.Browser.Browse(url + "/settings")
137-
return err
138-
}
139-
140-
if opts.WikiFlag {
141-
err := opts.Browser.Browse(url + "/wiki")
142-
return err
143-
}
144-
145-
if isNumber(opts.SelectorArg) {
146-
url += "/issues/" + opts.SelectorArg
147-
err := opts.Browser.Browse(url)
148-
return err
149-
}
150-
151-
if opts.Branch != "" {
152-
url += "/tree/" + opts.Branch + "/"
153-
} else {
154-
apiClient := api.NewClientFromHTTP(httpClient)
155-
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)
156-
if err != nil {
157-
return err
119+
if opts.SelectorArg == "" {
120+
if opts.ProjectsFlag {
121+
url += "/projects"
158122
}
159-
url += "/tree/" + branchName + "/"
160-
}
161-
162-
if opts.SelectorArg != "" {
163-
arr, err := parseFileArg(opts.SelectorArg)
164-
if err != nil {
165-
return err
123+
if opts.SettingsFlag {
124+
url += "/settings"
125+
}
126+
if opts.WikiFlag {
127+
url += "/wiki"
128+
}
129+
if opts.Branch != "" {
130+
url += "/tree/" + opts.Branch + "/"
166131
}
167-
if len(arr) > 1 {
168-
url += arr[0] + "#L" + arr[1]
132+
} else {
133+
if isNumber(opts.SelectorArg) {
134+
url += "/issues/" + opts.SelectorArg
169135
} else {
170-
url += arr[0]
136+
arr, err := parseFileArg(opts.SelectorArg)
137+
if err != nil {
138+
return err
139+
}
140+
if opts.Branch != "" {
141+
url += "/tree/" + opts.Branch + "/"
142+
} else {
143+
apiClient := api.NewClientFromHTTP(httpClient)
144+
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)
145+
if err != nil {
146+
return err
147+
}
148+
url += "/tree/" + branchName + "/"
149+
}
150+
if opts.SelectorArg != "" {
151+
if len(arr) > 1 {
152+
url += arr[0] + "#L" + arr[1]
153+
} else {
154+
url += arr[0]
155+
}
156+
}
171157
}
172158
}
173159

174160
err = opts.Browser.Browse(url)
175161
if opts.IO.IsStdoutTTY() && err == nil {
176162
fmt.Fprintf(opts.IO.Out, "now opening %s in browser\n", url)
177163
}
164+
178165
return err
179166
}
167+
180168
func parseFileArg(fileArg string) ([]string, error) {
181169
arr := strings.Split(fileArg, ":")
182170
if len(arr) > 2 {
@@ -185,7 +173,6 @@ func parseFileArg(fileArg string) ([]string, error) {
185173
if len(arr) > 1 && !isNumber(arr[1]) {
186174
return arr, fmt.Errorf("invalid line number after colon\nUse 'gh browse --help' for more information about browse\n")
187175
}
188-
189176
return arr, nil
190177
}
191178

pkg/cmd/browse/browse_test.go

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -137,83 +137,101 @@ func Test_runBrowse(t *testing.T) {
137137
opts: BrowseOptions{
138138
SelectorArg: "",
139139
},
140-
baseRepo: ghrepo.New("jessica", "cli"),
141-
defaultBranch: "trunk",
142-
expectedURL: "https://github.com/jessica/cli/tree/trunk/",
143-
},
144-
{
145-
name: "file argument",
146-
opts: BrowseOptions{SelectorArg: "path/to/file.txt"},
147-
baseRepo: ghrepo.New("ken", "cli"),
148-
defaultBranch: "main",
149-
expectedURL: "https://github.com/ken/cli/tree/main/path/to/file.txt",
150-
},
151-
{
152-
name: "branch flag",
153-
opts: BrowseOptions{
154-
Branch: "trunk",
155-
},
156-
baseRepo: ghrepo.New("thanh", "cli"),
157-
expectedURL: "https://github.com/thanh/cli/tree/trunk/",
140+
baseRepo: ghrepo.New("jlsestak", "cli"),
141+
expectedURL: "https://github.com/jlsestak/cli",
158142
},
159143
{
160144
name: "settings flag",
161145
opts: BrowseOptions{
162146
SettingsFlag: true,
163147
},
164-
baseRepo: ghrepo.New("bchadwic", "cli"),
165-
expectedURL: "https://github.com/bchadwic/cli/settings",
148+
baseRepo: ghrepo.New("bchadwic", "ObscuredByClouds"),
149+
expectedURL: "https://github.com/bchadwic/ObscuredByClouds/settings",
166150
},
167151
{
168152
name: "projects flag",
169153
opts: BrowseOptions{
170154
ProjectsFlag: true,
171155
},
172-
baseRepo: ghrepo.New("bchadwic", "cli"),
173-
expectedURL: "https://github.com/bchadwic/cli/projects",
156+
baseRepo: ghrepo.New("ttran112", "7ate9"),
157+
expectedURL: "https://github.com/ttran112/7ate9/projects",
174158
},
175159
{
176160
name: "wiki flag",
177161
opts: BrowseOptions{
178162
WikiFlag: true,
179163
},
180-
baseRepo: ghrepo.New("bchadwic", "cli"),
181-
expectedURL: "https://github.com/bchadwic/cli/wiki",
164+
baseRepo: ghrepo.New("ravocean", "ThreatLevelMidnight"),
165+
expectedURL: "https://github.com/ravocean/ThreatLevelMidnight/wiki",
166+
},
167+
{
168+
name: "file argument",
169+
opts: BrowseOptions{SelectorArg: "path/to/file.txt"},
170+
baseRepo: ghrepo.New("ken", "mrprofessor"),
171+
defaultBranch: "main",
172+
expectedURL: "https://github.com/ken/mrprofessor/tree/main/path/to/file.txt",
182173
},
183174
{
184175
name: "issue argument",
185176
opts: BrowseOptions{
186177
SelectorArg: "217",
187178
},
188-
baseRepo: ghrepo.New("bchadwic", "cli"),
189-
expectedURL: "https://github.com/bchadwic/cli/issues/217",
179+
baseRepo: ghrepo.New("kevin", "MinTy"),
180+
expectedURL: "https://github.com/kevin/MinTy/issues/217",
181+
},
182+
{
183+
name: "branch flag",
184+
opts: BrowseOptions{
185+
Branch: "trunk",
186+
},
187+
baseRepo: ghrepo.New("jlsestak", "CouldNotThinkOfARepoName"),
188+
expectedURL: "https://github.com/jlsestak/CouldNotThinkOfARepoName/tree/trunk/",
189+
},
190+
{
191+
name: "branch flag with file",
192+
opts: BrowseOptions{
193+
Branch: "trunk",
194+
SelectorArg: "main.go",
195+
},
196+
baseRepo: ghrepo.New("bchadwic", "LedZeppelinIV"),
197+
expectedURL: "https://github.com/bchadwic/LedZeppelinIV/tree/trunk/main.go",
190198
},
191199
{
192200
name: "file with line number",
193201
opts: BrowseOptions{
194202
SelectorArg: "path/to/file.txt:32",
195203
},
196-
baseRepo: ghrepo.New("bchadwic", "cli"),
204+
baseRepo: ghrepo.New("ravocean", "angur"),
197205
defaultBranch: "trunk",
198-
expectedURL: "https://github.com/bchadwic/cli/tree/trunk/path/to/file.txt#L32",
206+
expectedURL: "https://github.com/ravocean/angur/tree/trunk/path/to/file.txt#L32",
199207
},
200208
{
201209
name: "file with invalid line number",
202210
opts: BrowseOptions{
203211
SelectorArg: "path/to/file.txt:32:32",
204212
},
205-
baseRepo: ghrepo.New("bchadwic", "cli"),
206-
defaultBranch: "trunk",
207-
wantsErr: true,
213+
baseRepo: ghrepo.New("ttran112", "ttrain211"),
214+
wantsErr: true,
208215
},
209216
{
210-
name: "file with line argument",
217+
name: "branch with issue number",
211218
opts: BrowseOptions{
212-
SelectorArg: "path/to/file.txt:32",
219+
SelectorArg: "217",
220+
Branch: "trunk",
213221
},
214-
baseRepo: ghrepo.New("bchadwic", "cli"),
215-
defaultBranch: "trunk",
216-
expectedURL: "https://github.com/bchadwic/cli/tree/trunk/path/to/file.txt#L32",
222+
baseRepo: ghrepo.New("ken", "grc"),
223+
wantsErr: false,
224+
expectedURL: "https://github.com/ken/grc/issues/217",
225+
},
226+
{
227+
name: "opening branch file with line number",
228+
opts: BrowseOptions{
229+
Branch: "first-browse-pull",
230+
SelectorArg: "browse.go:32",
231+
},
232+
baseRepo: ghrepo.New("github", "ThankYouGitHub"),
233+
wantsErr: false,
234+
expectedURL: "https://github.com/github/ThankYouGitHub/tree/first-browse-pull/browse.go#L32",
217235
},
218236
}
219237

0 commit comments

Comments
 (0)