mirror of
https://github.com/Threnklyn/jira.git
synced 2026-06-07 21:43:32 +02:00
make automatic pagination on search optional, fix tests
This commit is contained in:
+1
-4
@@ -30,9 +30,6 @@ func CmdListRegistry() *jiracli.CommandRegistryEntry {
|
|||||||
return CmdListUsage(cmd, &opts, fig)
|
return CmdListUsage(cmd, &opts, fig)
|
||||||
},
|
},
|
||||||
func(o *oreo.Client, globals *jiracli.GlobalOptions) error {
|
func(o *oreo.Client, globals *jiracli.GlobalOptions) error {
|
||||||
if opts.MaxResults == 0 {
|
|
||||||
opts.MaxResults = 500
|
|
||||||
}
|
|
||||||
if opts.QueryFields == "" {
|
if opts.QueryFields == "" {
|
||||||
opts.QueryFields = "assignee,created,priority,reporter,status,summary,updated,issuetype"
|
opts.QueryFields = "assignee,created,priority,reporter,status,summary,updated,issuetype"
|
||||||
}
|
}
|
||||||
@@ -72,7 +69,7 @@ func CmdListUsage(cmd *kingpin.CmdClause, opts *ListOptions, fig *figtree.FigTre
|
|||||||
|
|
||||||
// List will query jira and send data to "list" template
|
// List will query jira and send data to "list" template
|
||||||
func CmdList(o *oreo.Client, globals *jiracli.GlobalOptions, opts *ListOptions) error {
|
func CmdList(o *oreo.Client, globals *jiracli.GlobalOptions, opts *ListOptions) error {
|
||||||
data, err := jira.Search(o, globals.Endpoint.Value, opts)
|
data, err := jira.Search(o, globals.Endpoint.Value, opts, jira.WithAutoPagination())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -777,6 +777,5 @@ type SearchResults struct {
|
|||||||
Schema JSONTypeMap `json:"schema,omitempty" yaml:"schema,omitempty"`
|
Schema JSONTypeMap `json:"schema,omitempty" yaml:"schema,omitempty"`
|
||||||
StartAt int `json:"startAt,omitempty" yaml:"startAt,omitempty"`
|
StartAt int `json:"startAt,omitempty" yaml:"startAt,omitempty"`
|
||||||
Total int `json:"total,omitempty" yaml:"total,omitempty"`
|
Total int `json:"total,omitempty" yaml:"total,omitempty"`
|
||||||
IsLast bool `json:"isLast,omitempty" yaml:"isLast,omitempty"`
|
|
||||||
WarningMessages WarningMessages `json:"warningMessages,omitempty" yaml:"warningMessages,omitempty"`
|
WarningMessages WarningMessages `json:"warningMessages,omitempty" yaml:"warningMessages,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,13 +73,35 @@ func (o *SearchOptions) ProvideSearchRequest() *jiradata.SearchRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// https://docs.atlassian.com/jira/REST/cloud/#api/2/search-searchUsingSearchRequest
|
// https://docs.atlassian.com/jira/REST/cloud/#api/2/search-searchUsingSearchRequest
|
||||||
func (j *Jira) Search(sp SearchProvider) (*jiradata.SearchResults, error) {
|
func (j *Jira) Search(sp SearchProvider, opts ...SearchOpt) (*jiradata.SearchResults, error) {
|
||||||
return Search(j.UA, j.Endpoint, sp)
|
return Search(j.UA, j.Endpoint, sp, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Search(ua HttpClient, endpoint string, sp SearchProvider) (*jiradata.SearchResults, error) {
|
type searchConfig struct {
|
||||||
|
autoPaginate bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type SearchOpt func(*searchConfig)
|
||||||
|
|
||||||
|
func WithAutoPagination() SearchOpt {
|
||||||
|
return func(c *searchConfig) {
|
||||||
|
c.autoPaginate = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Search(ua HttpClient, endpoint string, sp SearchProvider, opts ...SearchOpt) (*jiradata.SearchResults, error) {
|
||||||
|
c := &searchConfig{}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(c)
|
||||||
|
}
|
||||||
|
|
||||||
req := sp.ProvideSearchRequest()
|
req := sp.ProvideSearchRequest()
|
||||||
results := &jiradata.SearchResults{}
|
limit := req.MaxResults
|
||||||
|
if limit == 0 {
|
||||||
|
// max page size is 100
|
||||||
|
req.MaxResults = 100
|
||||||
|
}
|
||||||
|
|
||||||
issues := jiradata.Issues{}
|
issues := jiradata.Issues{}
|
||||||
for {
|
for {
|
||||||
encoded, err := json.Marshal(req)
|
encoded, err := json.Marshal(req)
|
||||||
@@ -97,18 +119,25 @@ func Search(ua HttpClient, endpoint string, sp SearchProvider) (*jiradata.Search
|
|||||||
return nil, responseError(resp)
|
return nil, responseError(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = readJSON(resp.Body, results)
|
page := &jiradata.SearchResults{}
|
||||||
|
err = readJSON(resp.Body, page)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if !c.autoPaginate {
|
||||||
|
return page, nil
|
||||||
|
}
|
||||||
|
|
||||||
issues = append(issues, results.Issues...)
|
issues = append(issues, page.Issues...)
|
||||||
|
// if we are done paginating just force all issues onto current
|
||||||
|
// response and return
|
||||||
|
if (limit > 0 && len(issues) >= limit) || len(issues) >= page.Total {
|
||||||
|
page.Issues = issues
|
||||||
|
return page, nil
|
||||||
|
}
|
||||||
req.StartAt = len(issues)
|
req.StartAt = len(issues)
|
||||||
|
if len(issues)+req.MaxResults > limit {
|
||||||
if len(issues) == results.Total || results.Total == 0 {
|
req.MaxResults = limit - len(issues)
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
results.Issues = issues
|
|
||||||
return results, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-5
@@ -4,7 +4,7 @@ cd $(dirname $0)
|
|||||||
jira="../jira"
|
jira="../jira"
|
||||||
. env.sh
|
. env.sh
|
||||||
|
|
||||||
PLAN 94
|
PLAN 98
|
||||||
|
|
||||||
# reset login
|
# reset login
|
||||||
RUNS $jira logout
|
RUNS $jira logout
|
||||||
@@ -568,10 +568,14 @@ description: |
|
|||||||
blocks
|
blocks
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
## List 102 closed issues, should be more than 100 (max page size), verify pagination
|
||||||
|
###############################################################################
|
||||||
|
RUNS $jira ls -q "project = 'BASIC' AND status = 'Done'" --limit 102
|
||||||
|
IS $(wc -l <$OSHT_STDOUT) -eq 102
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
## List 150 closed issues, should be more than 100
|
## List 1 issue, verify we dont get full page
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
RUNS $jira ls -q "project = 'BASIC' AND status = 'Done'" --limit 1
|
||||||
RUNS $jira ls --project BASIC --status Closed --limit 150
|
IS $(wc -l <$OSHT_STDOUT) -eq 1
|
||||||
IS $(printf $0 | wc -l) -eq 150
|
|
||||||
|
|||||||
Reference in New Issue
Block a user