From 0c10f67cd5c766ffa63cf61c9fffe56f21c2f992 Mon Sep 17 00:00:00 2001 From: Francisco Miamoto Date: Tue, 11 Aug 2020 19:13:10 -0300 Subject: [PATCH] add branch placeholder for api calls --- pkg/cmd/api/api.go | 21 +++++++++++++++------ pkg/cmd/api/api_test.go | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/api/api.go b/pkg/cmd/api/api.go index 27810187b..529cfa0c2 100644 --- a/pkg/cmd/api/api.go +++ b/pkg/cmd/api/api.go @@ -15,6 +15,7 @@ import ( "strings" "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/git" "github.com/cli/cli/internal/ghrepo" "github.com/cli/cli/pkg/cmdutil" "github.com/cli/cli/pkg/iostreams" @@ -36,15 +37,17 @@ type ApiOptions struct { Paginate bool Silent bool - HttpClient func() (*http.Client, error) - BaseRepo func() (ghrepo.Interface, error) + HttpClient func() (*http.Client, error) + BaseRepo func() (ghrepo.Interface, error) + CurrentBranch func() (string, error) } func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command { opts := ApiOptions{ - IO: f.IOStreams, - HttpClient: f.HttpClient, - BaseRepo: f.BaseRepo, + IO: f.IOStreams, + HttpClient: f.HttpClient, + BaseRepo: f.BaseRepo, + CurrentBranch: git.CurrentBranch, } cmd := &cobra.Command{ @@ -285,7 +288,7 @@ func processResponse(resp *http.Response, opts *ApiOptions, headersOutputStream return } -var placeholderRE = regexp.MustCompile(`\:(owner|repo)\b`) +var placeholderRE = regexp.MustCompile(`\:(owner|repo|branch)\b`) // fillPlaceholders populates `:owner` and `:repo` placeholders with values from the current repository func fillPlaceholders(value string, opts *ApiOptions) (string, error) { @@ -304,6 +307,12 @@ func fillPlaceholders(value string, opts *ApiOptions) (string, error) { return baseRepo.RepoOwner() case ":repo": return baseRepo.RepoName() + case ":branch": + branch, err := opts.CurrentBranch() + if err != nil { + panic(err) + } + return branch default: panic(fmt.Sprintf("invalid placeholder: %q", m)) } diff --git a/pkg/cmd/api/api_test.go b/pkg/cmd/api/api_test.go index e5aa9f31d..9ffef0f4b 100644 --- a/pkg/cmd/api/api_test.go +++ b/pkg/cmd/api/api_test.go @@ -753,6 +753,22 @@ func Test_fillPlaceholders(t *testing.T) { want: "repos/hubot/robot-uprising/releases", wantErr: false, }, + { + name: "has branch placeholder", + args: args{ + value: "repos/:owner/:repo/branches/:branch", + opts: &ApiOptions{ + BaseRepo: func() (ghrepo.Interface, error) { + return ghrepo.New("hubot", "robot-uprising"), nil + }, + CurrentBranch: func() (string, error) { + return "feature", nil + }, + }, + }, + want: "repos/hubot/robot-uprising/branches/feature", + wantErr: false, + }, { name: "no greedy substitutes", args: args{