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.
This commit is contained in:
Haiyan Meng
2019-12-19 12:30:02 -08:00
parent 837df94d67
commit ee659a70e4
2 changed files with 14 additions and 14 deletions

View File

@@ -116,9 +116,11 @@ type RequestConfig struct {
// the URL method to get the string value of the URL. See request.CopyWith, to // the URL method to get the string value of the URL. See request.CopyWith, to
// understand why the request object is useful. // understand why the request object is useful.
func (rc RequestConfig) CodeSearchRequestWith(query Query) request { func (rc RequestConfig) CodeSearchRequestWith(query Query) request {
req := rc.makeRequest("search/code", query) vals := url.Values{
req.vals.Set("sort", "indexed") "sort": []string{"indexed"},
req.vals.Set("order", "desc") "order": []string{"desc"},
}
req := rc.makeRequest("search/code", query, vals)
return req 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. // query for the Github API to find the dowload information of this filepath.
func (rc RequestConfig) ContentsRequest(fullRepoName, path string) string { func (rc RequestConfig) ContentsRequest(fullRepoName, path string) string {
uri := fmt.Sprintf("repos/%s/contents/%s", fullRepoName, path) 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 { func (rc RequestConfig) ReposRequest(fullRepoName string) string {
uri := fmt.Sprintf("repos/%s", fullRepoName) uri := fmt.Sprintf("repos/%s", fullRepoName)
return rc.makeRequest(uri, Query{}).URL() return rc.makeRequest(uri, Query{}, url.Values{}).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(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 { func (rc RequestConfig) makeRequest(path string, query Query, vals url.Values) request {
vals := url.Values{}
vals.Set(perPageArg, fmt.Sprint(rc.perPage)) vals.Set(perPageArg, fmt.Sprint(rc.perPage))
return request{ return request{

View File

@@ -101,7 +101,7 @@ func TestGithubSearchQuery(t *testing.T) {
"examples/helloWorld/kustomization.yaml?per_page=100", "examples/helloWorld/kustomization.yaml?per_page=100",
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", "path=examples%2FhelloWorld%2Fkustomization.yaml&per_page=100",
}, },
{ {
rc: RequestConfig{ rc: RequestConfig{
@@ -121,7 +121,7 @@ func TestGithubSearchQuery(t *testing.T) {
"examples%201/helloWorld/kustomization.yaml?per_page=100", "examples%201/helloWorld/kustomization.yaml?per_page=100",
expectedCommitsQuery: "https://api.github.com/repos/kubernetes-sigs/kustomize/commits?" + 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",
}, },
} }