Skip to content

Commit b833428

Browse files
committed
use factory method for branch
Also, improve the handling for the branch placeholder
1 parent 9ebcec9 commit b833428

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

pkg/cmd/api/api.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"strings"
1616

1717
"github.com/MakeNowJust/heredoc"
18-
"github.com/cli/cli/git"
1918
"github.com/cli/cli/internal/ghrepo"
2019
"github.com/cli/cli/pkg/cmdutil"
2120
"github.com/cli/cli/pkg/iostreams"
@@ -37,17 +36,17 @@ type ApiOptions struct {
3736
Paginate bool
3837
Silent bool
3938

40-
HttpClient func() (*http.Client, error)
41-
BaseRepo func() (ghrepo.Interface, error)
42-
CurrentBranch func() (string, error)
39+
HttpClient func() (*http.Client, error)
40+
BaseRepo func() (ghrepo.Interface, error)
41+
Branch func() (string, error)
4342
}
4443

4544
func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command {
4645
opts := ApiOptions{
47-
IO: f.IOStreams,
48-
HttpClient: f.HttpClient,
49-
BaseRepo: f.BaseRepo,
50-
CurrentBranch: git.CurrentBranch,
46+
IO: f.IOStreams,
47+
HttpClient: f.HttpClient,
48+
BaseRepo: f.BaseRepo,
49+
Branch: f.Branch,
5150
}
5251

5352
cmd := &cobra.Command{
@@ -302,27 +301,26 @@ func fillPlaceholders(value string, opts *ApiOptions) (string, error) {
302301
}
303302

304303
var branch string
305-
if strings.Contains(value, ":branch") {
306-
branch, err = opts.CurrentBranch()
307-
if err != nil {
308-
return value, err
309-
}
310-
}
311304

312-
value = placeholderRE.ReplaceAllStringFunc(value, func(m string) string {
305+
filled := placeholderRE.ReplaceAllStringFunc(value, func(m string) string {
313306
switch m {
314307
case ":owner":
315308
return baseRepo.RepoOwner()
316309
case ":repo":
317310
return baseRepo.RepoName()
318311
case ":branch":
312+
branch, err = opts.Branch()
319313
return branch
320314
default:
321315
panic(fmt.Sprintf("invalid placeholder: %q", m))
322316
}
323317
})
324318

325-
return value, nil
319+
if err != nil {
320+
return value, err
321+
}
322+
323+
return filled, nil
326324
}
327325

328326
func printHeaders(w io.Writer, headers http.Header, colorize bool) {

pkg/cmd/api/api_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,9 +760,9 @@ func Test_fillPlaceholders(t *testing.T) {
760760
value: "repos/cli/cli/branches/:branch",
761761
opts: &ApiOptions{
762762
BaseRepo: func() (ghrepo.Interface, error) {
763-
return ghrepo.New("hubot", "robot-uprising"), nil
763+
return ghrepo.New("cli", "cli"), nil
764764
},
765-
CurrentBranch: func() (string, error) {
765+
Branch: func() (string, error) {
766766
return "trunk", nil
767767
},
768768
},
@@ -778,7 +778,7 @@ func Test_fillPlaceholders(t *testing.T) {
778778
BaseRepo: func() (ghrepo.Interface, error) {
779779
return ghrepo.New("cli", "cli"), nil
780780
},
781-
CurrentBranch: func() (string, error) {
781+
Branch: func() (string, error) {
782782
return "", git.ErrNotOnAnyBranch
783783
},
784784
},

0 commit comments

Comments
 (0)