Escape spaces in the query paths of git commit requests

This commit is contained in:
Haiyan Meng
2019-12-12 09:42:32 -08:00
parent 57059b74f5
commit d9239104aa
2 changed files with 25 additions and 1 deletions

View File

@@ -123,11 +123,15 @@ func (rc RequestConfig) ReposRequest(fullRepoName string) string {
return rc.makeRequest(uri, Query{}).URL() return rc.makeRequest(uri, Query{}).URL()
} }
func escapeSpace(s string) string {
return strings.Replace(s, " ", "%20", -1)
}
// CommitsRequest given the repo name, and a filepath returns a formatted query // CommitsRequest given the repo name, and a filepath returns a formatted query
// for the Github API to find the commits that affect this file. // for the Github API to find the commits that affect this file.
func (rc RequestConfig) CommitsRequest(fullRepoName, path string) string { func (rc RequestConfig) CommitsRequest(fullRepoName, path string) string {
uri := fmt.Sprintf("repos/%s/commits", fullRepoName) uri := fmt.Sprintf("repos/%s/commits", fullRepoName)
return rc.makeRequest(uri, Query{Path(path)}).URL() return rc.makeRequest(uri, Query{Path(escapeSpace(path))}).URL()
} }
func (rc RequestConfig) makeRequest(path string, query Query) request { func (rc RequestConfig) makeRequest(path string, query Query) request {

View File

@@ -100,6 +100,26 @@ func TestGithubSearchQuery(t *testing.T) {
expectedCommitsQuery: "https://api.github.com/repos/kubernetes-sigs/kustomize/commits?" + expectedCommitsQuery: "https://api.github.com/repos/kubernetes-sigs/kustomize/commits?" +
"q=path:examples/helloWorld/kustomization.yaml&per_page=100", "q=path:examples/helloWorld/kustomization.yaml&per_page=100",
}, },
{
rc: RequestConfig{
perPage: perPage,
},
codeQuery: Query{
Filename("kustomization.yaml"),
Filesize(RangeWithin{64, 128}),
},
fullRepoName: "kubernetes-sigs/kustomize",
path: "examples 1/helloWorld/kustomization.yaml",
expectedCodeQuery: "https://api.github.com/search/code?" +
"q=filename:kustomization.yaml+size:64..128&order=desc&per_page=100&sort=indexed",
expectedContentsQuery: "https://api.github.com/repos/kubernetes-sigs/kustomize/contents/" +
"examples%201/helloWorld/kustomization.yaml?per_page=100",
expectedCommitsQuery: "https://api.github.com/repos/kubernetes-sigs/kustomize/commits?" +
"q=path:examples%201/helloWorld/kustomization.yaml&per_page=100",
},
} }
for _, test := range testCases { for _, test := range testCases {