Skip to content

Commit

Permalink
Merge pull request #88 from guywithnose/pagination
Browse files Browse the repository at this point in the history
Add SearchPages
  • Loading branch information
andygrunwald authored Nov 1, 2017
2 parents f238f40 + 6bfb81b commit 767d4ea
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
40 changes: 40 additions & 0 deletions issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,46 @@ func (s *IssueService) Search(jql string, options *SearchOptions) ([]Issue, *Res
return v.Issues, resp, err
}

// SearchPages will get issues from all pages in a search
//
// JIRA API docs: https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-query-issues
func (s *IssueService) SearchPages(jql string, options *SearchOptions, f func(Issue) error) error {
if options == nil {
options = &SearchOptions{
StartAt: 0,
MaxResults: 50,
}
}

if options.MaxResults == 0 {
options.MaxResults = 50
}

issues, resp, err := s.Search(jql, options)
if err != nil {
return err
}

for {
for _, issue := range issues {
err = f(issue)
if err != nil {
return err
}
}

if resp.StartAt+resp.MaxResults >= resp.Total {
return nil
}

options.StartAt += resp.MaxResults
issues, resp, err = s.Search(jql, options)
if err != nil {
return err
}
}
}

// GetCustomFields returns a map of customfield_* keys with string values
func (s *IssueService) GetCustomFields(issueID string) (CustomFields, *Response, error) {
apiEndpoint := fmt.Sprintf("rest/api/2/issue/%s", issueID)
Expand Down
38 changes: 38 additions & 0 deletions issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,44 @@ func TestIssueService_Search_WithoutPaging(t *testing.T) {
}
}

func TestIssueService_SearchPages(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/rest/api/2/search", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
if r.URL.String() == "/rest/api/2/search?jql=something&startAt=1&maxResults=2&expand=foo&fields=" {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"expand": "schema,names","startAt": 1,"maxResults": 2,"total": 6,"issues": [{"expand": "html","id": "10230","self": "http://kelpie9:8081/rest/api/2/issue/BULK-62","key": "BULK-62","fields": {"summary": "testing","timetracking": null,"issuetype": {"self": "http://kelpie9:8081/rest/api/2/issuetype/5","id": "5","description": "The sub-task of the issue","iconUrl": "http://kelpie9:8081/images/icons/issue_subtask.gif","name": "Sub-task","subtask": true},"customfield_10071": null}},{"expand": "html","id": "10004","self": "http://kelpie9:8081/rest/api/2/issue/BULK-47","key": "BULK-47","fields": {"summary": "Cheese v1 2.0 issue","timetracking": null,"issuetype": {"self": "http://kelpie9:8081/rest/api/2/issuetype/3","id": "3","description": "A task that needs to be done.","iconUrl": "http://kelpie9:8081/images/icons/task.gif","name": "Task","subtask": false}}}]}`)
return
} else if r.URL.String() == "/rest/api/2/search?jql=something&startAt=3&maxResults=2&expand=foo&fields=" {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"expand": "schema,names","startAt": 3,"maxResults": 2,"total": 6,"issues": [{"expand": "html","id": "10230","self": "http://kelpie9:8081/rest/api/2/issue/BULK-62","key": "BULK-62","fields": {"summary": "testing","timetracking": null,"issuetype": {"self": "http://kelpie9:8081/rest/api/2/issuetype/5","id": "5","description": "The sub-task of the issue","iconUrl": "http://kelpie9:8081/images/icons/issue_subtask.gif","name": "Sub-task","subtask": true},"customfield_10071": null}},{"expand": "html","id": "10004","self": "http://kelpie9:8081/rest/api/2/issue/BULK-47","key": "BULK-47","fields": {"summary": "Cheese v1 2.0 issue","timetracking": null,"issuetype": {"self": "http://kelpie9:8081/rest/api/2/issuetype/3","id": "3","description": "A task that needs to be done.","iconUrl": "http://kelpie9:8081/images/icons/task.gif","name": "Task","subtask": false}}}]}`)
return
} else if r.URL.String() == "/rest/api/2/search?jql=something&startAt=5&maxResults=2&expand=foo&fields=" {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"expand": "schema,names","startAt": 5,"maxResults": 2,"total": 6,"issues": [{"expand": "html","id": "10230","self": "http://kelpie9:8081/rest/api/2/issue/BULK-62","key": "BULK-62","fields": {"summary": "testing","timetracking": null,"issuetype": {"self": "http://kelpie9:8081/rest/api/2/issuetype/5","id": "5","description": "The sub-task of the issue","iconUrl": "http://kelpie9:8081/images/icons/issue_subtask.gif","name": "Sub-task","subtask": true},"customfield_10071": null}}]}`)
return
}

t.Errorf("Unexpected URL: %v", r.URL)
})

opt := &SearchOptions{StartAt: 1, MaxResults: 2, Expand: "foo"}
issues := make([]Issue, 0)
err := testClient.Issue.SearchPages("something", opt, func(issue Issue) error {
issues = append(issues, issue)
return nil
})

if err != nil {
t.Errorf("Error given: %s", err)
}

if len(issues) != 5 {
t.Errorf("Expected 5 issues, %v given", len(issues))
}
}

func TestIssueService_GetCustomFields(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit 767d4ea

Please sign in to comment.