add pagination support for /user/teams

This commit is contained in:
Akshay Pratinav 2019-03-12 21:24:47 -07:00
parent 056089bbcc
commit 3c19c364bd

View File

@ -137,36 +137,56 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) {
} `json:"organization"` } `json:"organization"`
} }
params := url.Values{ type teamsPage []struct {
"limit": {"200"}, Name string `json:"name"`
Slug string `json:"slug"`
Org struct {
Login string `json:"login"`
} `json:"organization"`
} }
endpoint := &url.URL{ pn := 1
Scheme: p.ValidateURL.Scheme, for {
Host: p.ValidateURL.Host, params := url.Values{
Path: path.Join(p.ValidateURL.Path, "/user/teams"), "limit": {"200"},
RawQuery: params.Encode(), "page": {strconv.Itoa(pn)},
} }
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) endpoint := &url.URL{
resp.Body.Close() Scheme: p.ValidateURL.Scheme,
if err != nil { Host: p.ValidateURL.Host,
return false, err Path: path.Join(p.ValidateURL.Path, "/user/teams"),
} RawQuery: params.Encode(),
if resp.StatusCode != 200 { }
return false, fmt.Errorf(
"got %d from %q %s", resp.StatusCode, endpoint.String(), body)
}
if err := json.Unmarshal(body, &teams); err != nil { req, _ := http.NewRequest("GET", endpoint.String(), nil)
return false, fmt.Errorf("%s unmarshaling %s", err, body) 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 var hasOrg bool