Skip to content

Commit d64a2a1

Browse files
committed
Add more query params
1 parent d84b722 commit d64a2a1

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

bitbucketapi/api.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,30 @@ func FetchToChannelJson(method string, url string, data any, c chan FetchToChanR
154154

155155
FetchToChannel(method, url, data, "application/json", c)
156156
}
157+
158+
type UrlQueryParam struct {
159+
CmdFlag string
160+
DefaultValue string
161+
ParamKey string
162+
}
163+
164+
type UrlQueryParams []UrlQueryParam
165+
166+
func FormatUrlQueryParam(cmd *cobra.Command, urlParams UrlQueryParams) (string, error) {
167+
queryParamString := ""
168+
for _, urlParam := range urlParams {
169+
if urlParam.CmdFlag == "" {
170+
queryParamString += fmt.Sprintf("%s=%s&", urlParam.ParamKey, urlParam.DefaultValue)
171+
}
172+
173+
paramValue, err := cmd.Flags().GetString(urlParam.CmdFlag)
174+
if err != nil {
175+
return queryParamString, err
176+
}
177+
if paramValue == "" {
178+
paramValue = urlParam.DefaultValue
179+
}
180+
queryParamString += fmt.Sprintf("%s=%s&", urlParam.ParamKey, paramValue)
181+
}
182+
return queryParamString, nil
183+
}

cmd/pipelines.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ import (
1818
"github.com/spf13/cobra"
1919
)
2020

21+
var listPipelinesQueryParams = bitbucketapi.UrlQueryParams{
22+
bitbucketapi.UrlQueryParam{CmdFlag: "page", DefaultValue: "1", ParamKey: "page"},
23+
bitbucketapi.UrlQueryParam{CmdFlag:"limit", DefaultValue: "20", ParamKey: "pagelen"},
24+
bitbucketapi.UrlQueryParam{CmdFlag:"order", DefaultValue: "-created_on", ParamKey: "sort"},
25+
}
26+
2127
// pipelinesCmd represents the pipelines command
2228
func listPipelines(cmd *cobra.Command) {
2329

@@ -26,11 +32,13 @@ func listPipelines(cmd *cobra.Command) {
2632
fmt.Println(cliformat.Error("No repo provided and current directory doesn't have a git remote repo"))
2733
return
2834
}
29-
page, err := cmd.Flags().GetString("page")
35+
urlQueryParam, err := bitbucketapi.FormatUrlQueryParam(cmd, listPipelinesQueryParams)
36+
3037
if err != nil {
31-
page = "1"
38+
fmt.Println(cliformat.Error(err.Error()))
39+
return
3240
}
33-
resp, err := bitbucketapi.HttpRequestWithBitbucketAuthJson("GET", repo+"/pipelines?pagelen=20&sort=-created_on&page="+page, map[string]string{})
41+
resp, err := bitbucketapi.HttpRequestWithBitbucketAuthJson("GET", repo+"/pipelines?"+urlQueryParam, map[string]string{})
3442
defer resp.Body.Close()
3543

3644
if err != nil {
@@ -343,7 +351,9 @@ func getStepStates(cmd *cobra.Command, args []string, toComplete string) ([]stri
343351
func init() {
344352
rootCmd.AddCommand(pipelinesCmd)
345353
pipelinesCmd.Flags().StringP("page", "p", "", "Page number for pipelines pagination")
354+
pipelinesCmd.Flags().StringP("limit", "l", "", "Page length (pagelen) param")
346355
pipelinesCmd.Flags().StringP("state", "s", "", "Stop by step with state {FAILED|IN_PROGRESS|SUCCESSFUL|COMPLETED}")
356+
pipelinesCmd.Flags().StringP("order", "o", "", "Order by")
347357
pipelinesCmd.Flags().StringP("format", "f", "", "Output template format")
348358
pipelinesCmd.Flags().BoolP("json", "j", false, "Output as json")
349359
pipelinesCmd.PersistentFlags().StringP("repo", "r", "", "Repo remote url")

cmd/pullrequests.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,24 +164,26 @@ func getPullRequestDetails(cmd *cobra.Command, args []string) {
164164

165165
}
166166

167+
var listPullRequestsQueryParams = bitbucketapi.UrlQueryParams{
168+
bitbucketapi.UrlQueryParam{CmdFlag: "page", DefaultValue: "1", ParamKey: "page"},
169+
bitbucketapi.UrlQueryParam{CmdFlag: "limit", DefaultValue: "20", ParamKey: "pagelen"},
170+
bitbucketapi.UrlQueryParam{CmdFlag: "order", DefaultValue: "-created_on", ParamKey: "sort"},
171+
bitbucketapi.UrlQueryParam{CmdFlag: "state", DefaultValue: "", ParamKey: "state"},
172+
}
173+
167174
func listPullRequests(cmd *cobra.Command) {
168175
repo, err := githelper.GetCurrentRepo(cmd)
169176
if err != nil {
170177
fmt.Println(cliformat.Error("No repo provided and current directory doesn't have a git remote repo"))
171178
return
172179
}
173-
url := repo + "/pullrequests?pagelen=20&sort=-created_on"
174-
175-
state, err := cmd.Flags().GetString("state")
176-
if err != nil || state != "" {
177-
url = url + "&state=" + state
178-
180+
urlQueryParams, err := bitbucketapi.FormatUrlQueryParam(cmd, listPullRequestsQueryParams)
181+
if err != nil {
182+
fmt.Println(cliformat.Error(err.Error()))
179183
}
180-
page, err := cmd.Flags().GetString("page")
181-
if err != nil || page != "" {
182-
url = url + "&page=" + page
183184

184-
}
185+
url := repo + "/pullrequests?" + urlQueryParams
186+
185187
resp, err := bitbucketapi.HttpRequestWithBitbucketAuthJson("GET", url, map[string]string{})
186188

187189
defer resp.Body.Close()
@@ -323,6 +325,8 @@ func init() {
323325
pullRequestsCmd.Flags().StringP("page", "p", "", "Page number for pullreuest pagination")
324326
pullRequestsCmd.Flags().StringP("format", "f", "", "Output template format")
325327
pullRequestsCmd.Flags().BoolP("json", "j", false, "Output as json")
328+
pullRequestsCmd.Flags().StringP("order", "o", "", "Order by")
329+
pullRequestsCmd.Flags().StringP("limit", "l", "", "Page length (pagelen) param")
326330
pullRequestsCmd.Flags().StringP("state", "s", "", "Pull request state")
327331
pullRequestCreateCmd.Flags().StringP("source", "s", "", "Pull request source branch")
328332
pullRequestCreateCmd.Flags().StringP("title", "t", "", "Pull request title")

0 commit comments

Comments
 (0)