From 3c19c364bde3120b8da88a2e36b9ccdc3641eb4e Mon Sep 17 00:00:00 2001 From: Akshay Pratinav Date: Tue, 12 Mar 2019 21:24:47 -0700 Subject: [PATCH 1/3] add pagination support for /user/teams --- providers/github.go | 72 +++++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/providers/github.go b/providers/github.go index d39ee2b6..fc3740c4 100644 --- a/providers/github.go +++ b/providers/github.go @@ -137,36 +137,56 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) { } `json:"organization"` } - params := url.Values{ - "limit": {"200"}, + type teamsPage []struct { + Name string `json:"name"` + Slug string `json:"slug"` + Org struct { + Login string `json:"login"` + } `json:"organization"` } - endpoint := &url.URL{ - Scheme: p.ValidateURL.Scheme, - Host: p.ValidateURL.Host, - Path: path.Join(p.ValidateURL.Path, "/user/teams"), - RawQuery: params.Encode(), - } - req, _ := http.NewRequest("GET", endpoint.String(), nil) - req.Header.Set("Accept", "application/vnd.github.v3+json") - req.Header.Set("Authorization", fmt.Sprintf("token %s", accessToken)) - resp, err := http.DefaultClient.Do(req) - if err != nil { - return false, err - } + pn := 1 + for { + params := url.Values{ + "limit": {"200"}, + "page": {strconv.Itoa(pn)}, + } - body, err := ioutil.ReadAll(resp.Body) - resp.Body.Close() - if err != nil { - return false, err - } - if resp.StatusCode != 200 { - return false, fmt.Errorf( - "got %d from %q %s", resp.StatusCode, endpoint.String(), body) - } + endpoint := &url.URL{ + Scheme: p.ValidateURL.Scheme, + Host: p.ValidateURL.Host, + Path: path.Join(p.ValidateURL.Path, "/user/teams"), + RawQuery: params.Encode(), + } - if err := json.Unmarshal(body, &teams); err != nil { - return false, fmt.Errorf("%s unmarshaling %s", err, body) + req, _ := http.NewRequest("GET", endpoint.String(), nil) + req.Header.Set("Accept", "application/vnd.github.v3+json") + req.Header.Set("Authorization", fmt.Sprintf("token %s", accessToken)) + resp, err := http.DefaultClient.Do(req) + if err != nil { + return false, err + } + + body, err := ioutil.ReadAll(resp.Body) + resp.Body.Close() + if err != nil { + return false, err + } + if resp.StatusCode != 200 { + return false, fmt.Errorf( + "got %d from %q %s", resp.StatusCode, endpoint.String(), body) + } + + var tp teamsPage + if err := json.Unmarshal(body, &tp); err != nil { + return false, fmt.Errorf("%s unmarshaling %s", err, body) + } + if len(tp) == 0 { + break + } + + teams = append(teams, tp...) + pn++ } var hasOrg bool From e73f6501f0171e63f804c45469e58ac219a83b03 Mon Sep 17 00:00:00 2001 From: Akshay Pratinav Date: Thu, 14 Mar 2019 20:04:45 -0700 Subject: [PATCH 2/3] limit => per_page --- providers/github.go | 8 ++++---- providers/github_test.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/providers/github.go b/providers/github.go index fc3740c4..eecf5dfc 100644 --- a/providers/github.go +++ b/providers/github.go @@ -73,8 +73,8 @@ func (p *GitHubProvider) hasOrg(accessToken string) (bool, error) { pn := 1 for { params := url.Values{ - "limit": {"200"}, - "page": {strconv.Itoa(pn)}, + "per_page": {"200"}, + "page": {strconv.Itoa(pn)}, } endpoint := &url.URL{ @@ -148,8 +148,8 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) { pn := 1 for { params := url.Values{ - "limit": {"200"}, - "page": {strconv.Itoa(pn)}, + "per_page": {"200"}, + "page": {strconv.Itoa(pn)}, } endpoint := &url.URL{ diff --git a/providers/github_test.go b/providers/github_test.go index c96877cb..1a66a59c 100644 --- a/providers/github_test.go +++ b/providers/github_test.go @@ -31,7 +31,7 @@ func testGitHubBackend(payload []string) *httptest.Server { pathToQueryMap := map[string][]string{ "/user": {""}, "/user/emails": {""}, - "/user/orgs": {"limit=200&page=1", "limit=200&page=2", "limit=200&page=3"}, + "/user/orgs": {"page=1&per_page=200", "page=2&per_page=200", "page=3&per_page=200"}, } return httptest.NewServer(http.HandlerFunc( From 6d15fe004eabcad382737976d3b686db7132becf Mon Sep 17 00:00:00 2001 From: Akshay Pratinav Date: Fri, 15 Mar 2019 08:00:20 -0700 Subject: [PATCH 3/3] change per_page value from 200 to 100 --- providers/github.go | 4 ++-- providers/github_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/providers/github.go b/providers/github.go index eecf5dfc..7bbc8b95 100644 --- a/providers/github.go +++ b/providers/github.go @@ -73,7 +73,7 @@ func (p *GitHubProvider) hasOrg(accessToken string) (bool, error) { pn := 1 for { params := url.Values{ - "per_page": {"200"}, + "per_page": {"100"}, "page": {strconv.Itoa(pn)}, } @@ -148,7 +148,7 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) { pn := 1 for { params := url.Values{ - "per_page": {"200"}, + "per_page": {"100"}, "page": {strconv.Itoa(pn)}, } diff --git a/providers/github_test.go b/providers/github_test.go index 1a66a59c..3d63551e 100644 --- a/providers/github_test.go +++ b/providers/github_test.go @@ -31,7 +31,7 @@ func testGitHubBackend(payload []string) *httptest.Server { pathToQueryMap := map[string][]string{ "/user": {""}, "/user/emails": {""}, - "/user/orgs": {"page=1&per_page=200", "page=2&per_page=200", "page=3&per_page=200"}, + "/user/orgs": {"page=1&per_page=100", "page=2&per_page=100", "page=3&per_page=100"}, } return httptest.NewServer(http.HandlerFunc(