git.iko.soy/typeable/oauth2-proxy
on master help sign in
add pagination support for /user/teams
commit3c19c364bde3120b8da88a2e36b9ccdc3641eb4e authorAkshay Pratinav <apratina@adobe.com> dateTue 12 Mar 2019, 21:24 (8 years ago) parent056089b
1 file changed+46−26
providers/github.go+46−26
providers/github.go+46−26
@@ -137,36 +137,56 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) {
137137 } `json:"organization"`
138138 }
139139
140− params := url.Values{
141− "limit": {"200"},
140+ type teamsPage []struct {
141+ Name string `json:"name"`
142+ Slug string `json:"slug"`
143+ Org struct {
144+ Login string `json:"login"`
145+ } `json:"organization"`
142146 }
143147
144− endpoint := &url.URL{
145− Scheme: p.ValidateURL.Scheme,
146− Host: p.ValidateURL.Host,
147− Path: path.Join(p.ValidateURL.Path, "/user/teams"),
148− RawQuery: params.Encode(),
149− }
150− req, _ := http.NewRequest("GET", endpoint.String(), nil)
151− req.Header.Set("Accept", "application/vnd.github.v3+json")
152− req.Header.Set("Authorization", fmt.Sprintf("token %s", accessToken))
153− resp, err := http.DefaultClient.Do(req)
154− if err != nil {
155− return false, err
156− }
148+ pn := 1
149+ for {
150+ params := url.Values{
151+ "limit": {"200"},
152+ "page": {strconv.Itoa(pn)},
153+ }
157154
158− body, err := ioutil.ReadAll(resp.Body)
159− resp.Body.Close()
160− if err != nil {
161− return false, err
162− }
163− if resp.StatusCode != 200 {
164− return false, fmt.Errorf(
165− "got %d from %q %s", resp.StatusCode, endpoint.String(), body)
166− }
155+ endpoint := &url.URL{
156+ Scheme: p.ValidateURL.Scheme,
157+ Host: p.ValidateURL.Host,
158+ Path: path.Join(p.ValidateURL.Path, "/user/teams"),
159+ RawQuery: params.Encode(),
160+ }
167161
168− if err := json.Unmarshal(body, &teams); err != nil {
169− return false, fmt.Errorf("%s unmarshaling %s", err, body)
162+ req, _ := http.NewRequest("GET", endpoint.String(), nil)
163+ req.Header.Set("Accept", "application/vnd.github.v3+json")
164+ req.Header.Set("Authorization", fmt.Sprintf("token %s", accessToken))
165+ resp, err := http.DefaultClient.Do(req)
166+ if err != nil {
167+ return false, err
168+ }
169+
170+ body, err := ioutil.ReadAll(resp.Body)
171+ resp.Body.Close()
172+ if err != nil {
173+ return false, err
174+ }
175+ if resp.StatusCode != 200 {
176+ return false, fmt.Errorf(
177+ "got %d from %q %s", resp.StatusCode, endpoint.String(), body)
178+ }
179+
180+ var tp teamsPage
181+ if err := json.Unmarshal(body, &tp); err != nil {
182+ return false, fmt.Errorf("%s unmarshaling %s", err, body)
183+ }
184+ if len(tp) == 0 {
185+ break
186+ }
187+
188+ teams = append(teams, tp...)
189+ pn++
170190 }
171191
172192 var hasOrg bool