mirror of
https://github.com/Threnklyn/jira.git
synced 2026-05-18 20:23:28 +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)
|
||||
},
|
||||
func(o *oreo.Client, globals *jiracli.GlobalOptions) error {
|
||||
if opts.MaxResults == 0 {
|
||||
opts.MaxResults = 500
|
||||
}
|
||||
if opts.QueryFields == "" {
|
||||
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
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -777,6 +777,5 @@ type SearchResults struct {
|
||||
Schema JSONTypeMap `json:"schema,omitempty" yaml:"schema,omitempty"`
|
||||
StartAt int `json:"startAt,omitempty" yaml:"startAt,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"`
|
||||
}
|
||||
|
||||
@@ -73,13 +73,35 @@ func (o *SearchOptions) ProvideSearchRequest() *jiradata.SearchRequest {
|
||||
}
|
||||
|
||||
// https://docs.atlassian.com/jira/REST/cloud/#api/2/search-searchUsingSearchRequest
|
||||
func (j *Jira) Search(sp SearchProvider) (*jiradata.SearchResults, error) {
|
||||
return Search(j.UA, j.Endpoint, sp)
|
||||
func (j *Jira) Search(sp SearchProvider, opts ...SearchOpt) (*jiradata.SearchResults, error) {
|
||||
return Search(j.UA, j.Endpoint, sp, opts...)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func Search(ua HttpClient, endpoint string, sp SearchProvider) (*jiradata.SearchResults, error) {
|
||||
req := sp.ProvideSearchRequest()
|
||||
results := &jiradata.SearchResults{}
|
||||
limit := req.MaxResults
|
||||
if limit == 0 {
|
||||
// max page size is 100
|
||||
req.MaxResults = 100
|
||||
}
|
||||
|
||||
issues := jiradata.Issues{}
|
||||
for {
|
||||
encoded, err := json.Marshal(req)
|
||||
@@ -97,18 +119,25 @@ func Search(ua HttpClient, endpoint string, sp SearchProvider) (*jiradata.Search
|
||||
return nil, responseError(resp)
|
||||
}
|
||||
|
||||
err = readJSON(resp.Body, results)
|
||||
page := &jiradata.SearchResults{}
|
||||
err = readJSON(resp.Body, page)
|
||||
if err != nil {
|
||||
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)
|
||||
|
||||
if len(issues) == results.Total || results.Total == 0 {
|
||||
break
|
||||
if len(issues)+req.MaxResults > limit {
|
||||
req.MaxResults = limit - len(issues)
|
||||
}
|
||||
}
|
||||
results.Issues = issues
|
||||
return results, nil
|
||||
}
|
||||
|
||||
+9
-5
@@ -4,7 +4,7 @@ cd $(dirname $0)
|
||||
jira="../jira"
|
||||
. env.sh
|
||||
|
||||
PLAN 94
|
||||
PLAN 98
|
||||
|
||||
# reset login
|
||||
RUNS $jira logout
|
||||
@@ -568,10 +568,14 @@ description: |
|
||||
blocks
|
||||
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 --project BASIC --status Closed --limit 150
|
||||
IS $(printf $0 | wc -l) -eq 150
|
||||
RUNS $jira ls -q "project = 'BASIC' AND status = 'Done'" --limit 1
|
||||
IS $(wc -l <$OSHT_STDOUT) -eq 1
|
||||
|
||||
Reference in New Issue
Block a user