Skip to content

Commit 0c10f67

Browse files
committed
add branch placeholder for api calls
1 parent 291cded commit 0c10f67

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

pkg/cmd/api/api.go

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

1717
"github.com/MakeNowJust/heredoc"
18+
"github.com/cli/cli/git"
1819
"github.com/cli/cli/internal/ghrepo"
1920
"github.com/cli/cli/pkg/cmdutil"
2021
"github.com/cli/cli/pkg/iostreams"
@@ -36,15 +37,17 @@ type ApiOptions struct {
3637
Paginate bool
3738
Silent bool
3839

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

4345
func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command {
4446
opts := ApiOptions{
45-
IO: f.IOStreams,
46-
HttpClient: f.HttpClient,
47-
BaseRepo: f.BaseRepo,
47+
IO: f.IOStreams,
48+
HttpClient: f.HttpClient,
49+
BaseRepo: f.BaseRepo,
50+
CurrentBranch: git.CurrentBranch,
4851
}
4952

5053
cmd := &cobra.Command{
@@ -285,7 +288,7 @@ func processResponse(resp *http.Response, opts *ApiOptions, headersOutputStream
285288
return
286289
}
287290

288-
var placeholderRE = regexp.MustCompile(`\:(owner|repo)\b`)
291+
var placeholderRE = regexp.MustCompile(`\:(owner|repo|branch)\b`)
289292

290293
// fillPlaceholders populates `:owner` and `:repo` placeholders with values from the current repository
291294
func fillPlaceholders(value string, opts *ApiOptions) (string, error) {
@@ -304,6 +307,12 @@ func fillPlaceholders(value string, opts *ApiOptions) (string, error) {
304307
return baseRepo.RepoOwner()
305308
case ":repo":
306309
return baseRepo.RepoName()
310+
case ":branch":
311+
branch, err := opts.CurrentBranch()
312+
if err != nil {
313+
panic(err)
314+
}
315+
return branch
307316
default:
308317
panic(fmt.Sprintf("invalid placeholder: %q", m))
309318
}

pkg/cmd/api/api_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,22 @@ func Test_fillPlaceholders(t *testing.T) {
753753
want: "repos/hubot/robot-uprising/releases",
754754
wantErr: false,
755755
},
756+
{
757+
name: "has branch placeholder",
758+
args: args{
759+
value: "repos/:owner/:repo/branches/:branch",
760+
opts: &ApiOptions{
761+
BaseRepo: func() (ghrepo.Interface, error) {
762+
return ghrepo.New("hubot", "robot-uprising"), nil
763+
},
764+
CurrentBranch: func() (string, error) {
765+
return "feature", nil
766+
},
767+
},
768+
},
769+
want: "repos/hubot/robot-uprising/branches/feature",
770+
wantErr: false,
771+
},
756772
{
757773
name: "no greedy substitutes",
758774
args: args{

0 commit comments

Comments
 (0)