Skip to content

Commit

Permalink
add pagination to search
Browse files Browse the repository at this point in the history
  • Loading branch information
Miles Maddox committed Apr 21, 2019
1 parent 514c149 commit 76dd1d8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
1 change: 1 addition & 0 deletions jiradata/SearchResults.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,5 +777,6 @@ 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"`
}
44 changes: 30 additions & 14 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,36 @@ func (j *Jira) Search(sp SearchProvider) (*jiradata.SearchResults, error) {

func Search(ua HttpClient, endpoint string, sp SearchProvider) (*jiradata.SearchResults, error) {
req := sp.ProvideSearchRequest()
encoded, err := json.Marshal(req)
if err != nil {
return nil, err
}
uri := URLJoin(endpoint, "rest/api/2/search")
resp, err := ua.Post(uri, "application/json", bytes.NewBuffer(encoded))
if err != nil {
return nil, err
}
defer resp.Body.Close()
results := &jiradata.SearchResults{}
issues := jiradata.Issues{}
for {
encoded, err := json.Marshal(req)
if err != nil {
return nil, err
}
uri := URLJoin(endpoint, "rest/api/2/search")
resp, err := ua.Post(uri, "application/json", bytes.NewBuffer(encoded))
if err != nil {
return nil, err
}

if resp.StatusCode != 200 {
return nil, responseError(resp)
}

err = readJSON(resp.Body, results)
if err != nil {
return nil, err
}

if resp.StatusCode == 200 {
results := &jiradata.SearchResults{}
return results, readJSON(resp.Body, results)
issues = append(issues, results.Issues...)
req.StartAt = len(issues)

if len(issues) == results.Total || results.Total == 0 {
break
}
resp.Body.Close()
}
return nil, responseError(resp)
results.Issues = issues
return results, nil
}

0 comments on commit 76dd1d8

Please sign in to comment.