Skip to content

Commit b3bf8c2

Browse files
Rambatinoghostsquad
authored andcommitted
fix: empty SearchOptions causing malformed request
prevents empty SearchOptions from being interpolated into the JIRA REST request when calling Search on the Issue Service
1 parent e93c0e1 commit b3bf8c2

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

issue.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -905,8 +905,22 @@ func (s *IssueService) Search(jql string, options *SearchOptions) ([]Issue, *Res
905905
if options == nil {
906906
u = fmt.Sprintf("rest/api/2/search?jql=%s", url.QueryEscape(jql))
907907
} else {
908-
u = fmt.Sprintf("rest/api/2/search?jql=%s&startAt=%d&maxResults=%d&expand=%s&fields=%s&validateQuery=%s", url.QueryEscape(jql),
909-
options.StartAt, options.MaxResults, options.Expand, strings.Join(options.Fields, ","), options.ValidateQuery)
908+
u = "rest/api/2/search?jql=" + url.QueryEscape(jql)
909+
if options.StartAt != 0 {
910+
u += fmt.Sprintf("&startAt=%d", options.StartAt)
911+
}
912+
if options.MaxResults != 0 {
913+
u += fmt.Sprintf("&maxResults=%d", options.MaxResults)
914+
}
915+
if options.Expand != "" {
916+
u += fmt.Sprintf("&expand=%s", options.Expand)
917+
}
918+
if strings.Join(options.Fields, ",") != "" {
919+
u += fmt.Sprintf("&fields=%s", strings.Join(options.Fields, ","))
920+
}
921+
if options.ValidateQuery != "" {
922+
u += fmt.Sprintf("&validateQuery=%s", options.ValidateQuery)
923+
}
910924
}
911925

912926
req, err := s.client.NewRequest("GET", u, nil)

issue_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -635,15 +635,15 @@ func TestIssueService_SearchPages(t *testing.T) {
635635
defer teardown()
636636
testMux.HandleFunc("/rest/api/2/search", func(w http.ResponseWriter, r *http.Request) {
637637
testMethod(t, r, "GET")
638-
if r.URL.String() == "/rest/api/2/search?jql=something&startAt=1&maxResults=2&expand=foo&fields=&validateQuery=warn" {
638+
if r.URL.String() == "/rest/api/2/search?jql=something&startAt=1&maxResults=2&expand=foo&validateQuery=warn" {
639639
w.WriteHeader(http.StatusOK)
640640
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}}}]}`)
641641
return
642-
} else if r.URL.String() == "/rest/api/2/search?jql=something&startAt=3&maxResults=2&expand=foo&fields=&validateQuery=warn" {
642+
} else if r.URL.String() == "/rest/api/2/search?jql=something&startAt=3&maxResults=2&expand=foo&validateQuery=warn" {
643643
w.WriteHeader(http.StatusOK)
644644
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}}}]}`)
645645
return
646-
} else if r.URL.String() == "/rest/api/2/search?jql=something&startAt=5&maxResults=2&expand=foo&fields=&validateQuery=warn" {
646+
} else if r.URL.String() == "/rest/api/2/search?jql=something&startAt=5&maxResults=2&expand=foo&validateQuery=warn" {
647647
w.WriteHeader(http.StatusOK)
648648
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}}]}`)
649649
return

0 commit comments

Comments
 (0)