From ee659a70e409f51ec2d26a7a3589e95b65f07823 Mon Sep 17 00:00:00 2001 From: Haiyan Meng Date: Thu, 19 Dec 2019 12:30:02 -0800 Subject: [PATCH] Fix how to construct URLs for finding all the commits related to a github file The existing logic sets the creation time of a github file to the time when the github repository was created. The fix sets the creation time of a github file to the time when the file was created. --- api/internal/crawl/crawler/github/queries.go | 24 +++++++++---------- .../crawl/crawler/github/queries_test.go | 4 ++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/api/internal/crawl/crawler/github/queries.go b/api/internal/crawl/crawler/github/queries.go index 98eb1b9f3..557e0f371 100644 --- a/api/internal/crawl/crawler/github/queries.go +++ b/api/internal/crawl/crawler/github/queries.go @@ -116,9 +116,11 @@ type RequestConfig struct { // the URL method to get the string value of the URL. See request.CopyWith, to // understand why the request object is useful. func (rc RequestConfig) CodeSearchRequestWith(query Query) request { - req := rc.makeRequest("search/code", query) - req.vals.Set("sort", "indexed") - req.vals.Set("order", "desc") + vals := url.Values{ + "sort": []string{"indexed"}, + "order": []string{"desc"}, + } + req := rc.makeRequest("search/code", query, vals) return req } @@ -126,27 +128,25 @@ func (rc RequestConfig) CodeSearchRequestWith(query Query) request { // query for the Github API to find the dowload information of this filepath. func (rc RequestConfig) ContentsRequest(fullRepoName, path string) string { uri := fmt.Sprintf("repos/%s/contents/%s", fullRepoName, path) - return rc.makeRequest(uri, Query{}).URL() + return rc.makeRequest(uri, Query{}, url.Values{}).URL() } func (rc RequestConfig) ReposRequest(fullRepoName string) string { uri := fmt.Sprintf("repos/%s", fullRepoName) - return rc.makeRequest(uri, Query{}).URL() -} - -func escapeSpace(s string) string { - return strings.Replace(s, " ", "%20", -1) + return rc.makeRequest(uri, Query{}, url.Values{}).URL() } // CommitsRequest given the repo name, and a filepath returns a formatted query // for the Github API to find the commits that affect this file. func (rc RequestConfig) CommitsRequest(fullRepoName, path string) string { uri := fmt.Sprintf("repos/%s/commits", fullRepoName) - return rc.makeRequest(uri, Query{Path(escapeSpace(path))}).URL() + vals := url.Values{ + "path": []string{path}, + } + return rc.makeRequest(uri, Query{}, vals).URL() } -func (rc RequestConfig) makeRequest(path string, query Query) request { - vals := url.Values{} +func (rc RequestConfig) makeRequest(path string, query Query, vals url.Values) request { vals.Set(perPageArg, fmt.Sprint(rc.perPage)) return request{ diff --git a/api/internal/crawl/crawler/github/queries_test.go b/api/internal/crawl/crawler/github/queries_test.go index 07c9036b6..fcab464f0 100644 --- a/api/internal/crawl/crawler/github/queries_test.go +++ b/api/internal/crawl/crawler/github/queries_test.go @@ -101,7 +101,7 @@ func TestGithubSearchQuery(t *testing.T) { "examples/helloWorld/kustomization.yaml?per_page=100", expectedCommitsQuery: "https://api.github.com/repos/kubernetes-sigs/kustomize/commits?" + - "q=path:examples/helloWorld/kustomization.yaml&per_page=100", + "path=examples%2FhelloWorld%2Fkustomization.yaml&per_page=100", }, { rc: RequestConfig{ @@ -121,7 +121,7 @@ func TestGithubSearchQuery(t *testing.T) { "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", + "path=examples+1%2FhelloWorld%2Fkustomization.yaml&per_page=100", }, }