@@ -11,7 +11,6 @@ import (
11
11
"net/http"
12
12
"net/url"
13
13
"reflect"
14
- "strconv"
15
14
"strings"
16
15
"time"
17
16
@@ -517,14 +516,23 @@ type CommentVisibility struct {
517
516
// Default Pagination options
518
517
type SearchOptions struct {
519
518
// StartAt: The starting index of the returned projects. Base index: 0.
520
- StartAt int `url:"startAt,omitempty"`
519
+ StartAt int `url:"startAt,omitempty" json:"startAt,omitempty" `
521
520
// MaxResults: The maximum number of projects to return per page. Default: 50.
522
- MaxResults int `url:"maxResults,omitempty"`
521
+ MaxResults int `url:"maxResults,omitempty" json:"maxResults,omitempty" `
523
522
// Expand: Expand specific sections in the returned issues
524
- Expand string `url:"expand,omitempty"`
525
- Fields []string
523
+ Expand string `url:"expand,omitempty" json:"- "`
524
+ Fields []string `url:"fields,comma,omitempty" json:"fields,omitempty"`
526
525
// ValidateQuery: The validateQuery param offers control over whether to validate and how strictly to treat the validation. Default: strict.
527
- ValidateQuery string `url:"validateQuery,omitempty"`
526
+ ValidateQuery string `url:"validateQuery,omitempty" json:"validateQuery,omitempty"`
527
+ }
528
+
529
+ // searchRequest is for the Search (with JQL) method to encode its inputs into a request whether it's POST or GET.
530
+ type searchRequest struct {
531
+ // JQL is the query that the user passed.
532
+ JQL string `url:"jql,omitempty" json:"jql,omitempty"`
533
+ // ExpandArray is the array form of SearchOptions.Expand used for the POST/JSON version of the search request.
534
+ ExpandArray []string `url:"-" json:"expand,omitempty"`
535
+ * SearchOptions `url:",omitempty" json:",omitempty"`
528
536
}
529
537
530
538
// searchResult is only a small wrapper around the Search (with JQL) method
@@ -1091,32 +1099,29 @@ func (s *IssueService) SearchWithContext(ctx context.Context, jql string, option
1091
1099
u := url.URL {
1092
1100
Path : "rest/api/2/search" ,
1093
1101
}
1094
- uv := url. Values {}
1095
- if jql != "" {
1096
- uv . Add ( "jql" , jql )
1102
+ r := searchRequest {
1103
+ JQL : jql ,
1104
+ SearchOptions : options ,
1097
1105
}
1098
-
1099
- if options != nil {
1100
- if options .StartAt != 0 {
1101
- uv .Add ("startAt" , strconv .Itoa (options .StartAt ))
1102
- }
1103
- if options .MaxResults != 0 {
1104
- uv .Add ("maxResults" , strconv .Itoa (options .MaxResults ))
1105
- }
1106
- if options .Expand != "" {
1107
- uv .Add ("expand" , options .Expand )
1108
- }
1109
- if strings .Join (options .Fields , "," ) != "" {
1110
- uv .Add ("fields" , strings .Join (options .Fields , "," ))
1111
- }
1112
- if options .ValidateQuery != "" {
1113
- uv .Add ("validateQuery" , options .ValidateQuery )
1114
- }
1106
+ if options != nil && options .Expand != "" {
1107
+ r .ExpandArray = strings .Split (options .Expand , "," )
1115
1108
}
1116
1109
1110
+ uv , err := query .Values (r )
1111
+ if err != nil {
1112
+ return []Issue {}, nil , err
1113
+ }
1117
1114
u .RawQuery = uv .Encode ()
1118
1115
1119
- req , err := s .client .NewRequestWithContext (ctx , "GET" , u .String (), nil )
1116
+ var req * http.Request
1117
+ if len (u .String ()) > 2000 {
1118
+ // If the JQL is too long, switch to the post method instead.
1119
+ u .RawQuery = ""
1120
+ req , err = s .client .NewRequestWithContext (ctx , "POST" , u .String (), r )
1121
+ } else {
1122
+ req , err = s .client .NewRequestWithContext (ctx , "GET" , u .String (), nil )
1123
+ }
1124
+
1120
1125
if err != nil {
1121
1126
return []Issue {}, nil , err
1122
1127
}
0 commit comments