Skip to content

Commit 7462feb

Browse files
Yosuke Akatsukagmlewis
authored andcommitted
Use url.QueryEscape instead of url.PathEscape to escape query string (#1125)
Fixes #1122.
1 parent d173fe3 commit 7462feb

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

github/search.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ package github
88
import (
99
"context"
1010
"fmt"
11-
"net/url"
1211
"strconv"
13-
"strings"
1412

1513
qs "github.com/google/go-querystring/query"
1614
)
@@ -218,20 +216,19 @@ func (s *SearchService) Labels(ctx context.Context, repoID int64, query string,
218216

219217
// Helper function that executes search queries against different
220218
// GitHub search types (repositories, commits, code, issues, users, labels)
219+
//
220+
// If searchParameters.Query includes multiple condition, it MUST NOT include "+" as condition separator.
221+
// For example, querying with "language:c++" and "leveldb", then searchParameters.Query should be "language:c++ leveldb" but not "language:c+++leveldb".
221222
func (s *SearchService) search(ctx context.Context, searchType string, parameters *searchParameters, opt *SearchOptions, result interface{}) (*Response, error) {
222223
params, err := qs.Values(opt)
223224
if err != nil {
224225
return nil, err
225226
}
226-
q := strings.Replace(parameters.Query, " ", "+", -1)
227227
if parameters.RepositoryID != nil {
228228
params.Set("repository_id", strconv.FormatInt(*parameters.RepositoryID, 10))
229229
}
230-
query := "q=" + url.PathEscape(q)
231-
if v := params.Encode(); v != "" {
232-
query = query + "&" + v
233-
}
234-
u := fmt.Sprintf("search/%s?%s", searchType, query)
230+
params.Set("q", parameters.Query)
231+
u := fmt.Sprintf("search/%s?%s", searchType, params.Encode())
235232

236233
req, err := s.client.NewRequest("GET", u, nil)
237234
if err != nil {

github/search_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func TestSearchService_Issues_withQualifiersNoOpts(t *testing.T) {
115115
client, mux, _, teardown := setup()
116116
defer teardown()
117117

118-
const q = "gopher is:issue label:bug language:go pushed:>=2018-01-01 stars:>=200"
118+
const q = "gopher is:issue label:bug language:c++ pushed:>=2018-01-01 stars:>=200"
119119

120120
var requestURI string
121121
mux.HandleFunc("/search/issues", func(w http.ResponseWriter, r *http.Request) {
@@ -134,7 +134,7 @@ func TestSearchService_Issues_withQualifiersNoOpts(t *testing.T) {
134134
t.Errorf("Search.Issues returned error: %v", err)
135135
}
136136

137-
if want := "/api-v3/search/issues?q=gopher+is:issue+label:bug+language:go+pushed:%3E=2018-01-01+stars:%3E=200"; requestURI != want {
137+
if want := "/api-v3/search/issues?q=gopher+is%3Aissue+label%3Abug+language%3Ac%2B%2B+pushed%3A%3E%3D2018-01-01+stars%3A%3E%3D200"; requestURI != want {
138138
t.Fatalf("URI encoding failed: got %v, want %v", requestURI, want)
139139
}
140140

@@ -152,7 +152,7 @@ func TestSearchService_Issues_withQualifiersAndOpts(t *testing.T) {
152152
client, mux, _, teardown := setup()
153153
defer teardown()
154154

155-
const q = "gopher is:issue label:bug language:go pushed:>=2018-01-01 stars:>=200"
155+
const q = "gopher is:issue label:bug language:c++ pushed:>=2018-01-01 stars:>=200"
156156

157157
var requestURI string
158158
mux.HandleFunc("/search/issues", func(w http.ResponseWriter, r *http.Request) {
@@ -172,7 +172,7 @@ func TestSearchService_Issues_withQualifiersAndOpts(t *testing.T) {
172172
t.Errorf("Search.Issues returned error: %v", err)
173173
}
174174

175-
if want := "/api-v3/search/issues?q=gopher+is:issue+label:bug+language:go+pushed:%3E=2018-01-01+stars:%3E=200&sort=forks"; requestURI != want {
175+
if want := "/api-v3/search/issues?q=gopher+is%3Aissue+label%3Abug+language%3Ac%2B%2B+pushed%3A%3E%3D2018-01-01+stars%3A%3E%3D200&sort=forks"; requestURI != want {
176176
t.Fatalf("URI encoding failed: got %v, want %v", requestURI, want)
177177
}
178178

0 commit comments

Comments
 (0)