Skip to content

Commit 19d3fc0

Browse files
evilnerdghostsquad
authored andcommitted
fix: paging with load balancer going to endless loop
When used with a load balanced Jira, the SearchPages method would end up in an endless loop. This was caused by a bug where Jira would not handle the MaxResults=50 that is sent by defaul properly, thus retur- ning no issues. The SearchPages method didn't check for empty results and ended up in an endless loop. Fixed this by 1. Pre-escaping '&maxResults' to '&MaxResults'. 2. Adding a check in SearchPages to see if the issues array is empty before going into the endless 'for'. Also fixed the appropriate tests. Fixes issue #260.
1 parent 436469b commit 19d3fc0

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

issue.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ func (s *IssueService) Search(jql string, options *SearchOptions) ([]Issue, *Res
965965
u += fmt.Sprintf("&startAt=%d", options.StartAt)
966966
}
967967
if options.MaxResults != 0 {
968-
u += fmt.Sprintf("&maxResults=%d", options.MaxResults)
968+
u += fmt.Sprintf("&maxResults=%d", options.MaxResults)
969969
}
970970
if options.Expand != "" {
971971
u += fmt.Sprintf("&expand=%s", options.Expand)
@@ -1011,6 +1011,10 @@ func (s *IssueService) SearchPages(jql string, options *SearchOptions, f func(Is
10111011
return err
10121012
}
10131013

1014+
if len(issues) == 0 {
1015+
return nil
1016+
}
1017+
10141018
for {
10151019
for _, issue := range issues {
10161020
err = f(issue)

issue_test.go

+34-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package jira
33
import (
44
"encoding/json"
55
"fmt"
6-
"github.com/google/go-cmp/cmp"
76
"io"
87
"io/ioutil"
98
"net/http"
109
"reflect"
1110
"strings"
1211
"testing"
1312

13+
"github.com/google/go-cmp/cmp"
14+
1415
"time"
1516

1617
"github.com/trivago/tgo/tcontainer"
@@ -596,7 +597,7 @@ func TestIssueService_Search(t *testing.T) {
596597
defer teardown()
597598
testMux.HandleFunc("/rest/api/2/search", func(w http.ResponseWriter, r *http.Request) {
598599
testMethod(t, r, "GET")
599-
testRequestURL(t, r, "/rest/api/2/search?jql=something&startAt=1&maxResults=40&expand=foo")
600+
testRequestURL(t, r, "/rest/api/2/search?jql=something&startAt=1&maxResults=40&expand=foo")
600601
w.WriteHeader(http.StatusOK)
601602
fmt.Fprint(w, `{"expand": "schema,names","startAt": 1,"maxResults": 40,"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}}}]}`)
602603
})
@@ -657,15 +658,15 @@ func TestIssueService_SearchPages(t *testing.T) {
657658
defer teardown()
658659
testMux.HandleFunc("/rest/api/2/search", func(w http.ResponseWriter, r *http.Request) {
659660
testMethod(t, r, "GET")
660-
if r.URL.String() == "/rest/api/2/search?jql=something&startAt=1&maxResults=2&expand=foo&validateQuery=warn" {
661+
if r.URL.String() == "/rest/api/2/search?jql=something&startAt=1&maxResults=2&expand=foo&validateQuery=warn" {
661662
w.WriteHeader(http.StatusOK)
662663
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}}}]}`)
663664
return
664-
} else if r.URL.String() == "/rest/api/2/search?jql=something&startAt=3&maxResults=2&expand=foo&validateQuery=warn" {
665+
} else if r.URL.String() == "/rest/api/2/search?jql=something&startAt=3&maxResults=2&expand=foo&validateQuery=warn" {
665666
w.WriteHeader(http.StatusOK)
666667
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}}}]}`)
667668
return
668-
} else if r.URL.String() == "/rest/api/2/search?jql=something&startAt=5&maxResults=2&expand=foo&validateQuery=warn" {
669+
} else if r.URL.String() == "/rest/api/2/search?jql=something&startAt=5&maxResults=2&expand=foo&validateQuery=warn" {
669670
w.WriteHeader(http.StatusOK)
670671
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}}]}`)
671672
return
@@ -690,6 +691,34 @@ func TestIssueService_SearchPages(t *testing.T) {
690691
}
691692
}
692693

694+
func TestIssueService_SearchPages_EmptyResult(t *testing.T) {
695+
setup()
696+
defer teardown()
697+
testMux.HandleFunc("/rest/api/2/search", func(w http.ResponseWriter, r *http.Request) {
698+
testMethod(t, r, "GET")
699+
if r.URL.String() == "/rest/api/2/search?jql=something&startAt=1&maxResults=50&expand=foo&validateQuery=warn" {
700+
w.WriteHeader(http.StatusOK)
701+
// This is what Jira outputs when the &maxResult= issue occurs. It used to cause SearchPages to go into an endless loop.
702+
fmt.Fprint(w, `{"expand": "schema,names","startAt": 0,"maxResults": 0,"total": 6,"issues": []}`)
703+
return
704+
}
705+
706+
t.Errorf("Unexpected URL: %v", r.URL)
707+
})
708+
709+
opt := &SearchOptions{StartAt: 1, MaxResults: 50, Expand: "foo", ValidateQuery: "warn"}
710+
issues := make([]Issue, 0)
711+
err := testClient.Issue.SearchPages("something", opt, func(issue Issue) error {
712+
issues = append(issues, issue)
713+
return nil
714+
})
715+
716+
if err != nil {
717+
t.Errorf("Error given: %s", err)
718+
}
719+
720+
}
721+
693722
func TestIssueService_GetCustomFields(t *testing.T) {
694723
setup()
695724
defer teardown()

0 commit comments

Comments
 (0)