OAuth: Support pagination for GitHub orgs (#58648)

* Support pagination for github orgs

* fix unused variable

* Increase initial page to 100 per the way teams work
This commit is contained in:
Sean Dague 2023-01-11 03:52:17 -05:00 committed by GitHub
parent fa9f1c3a52
commit 4de0149bd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -147,27 +147,33 @@ func (s *SocialGithub) HasMoreRecords(headers http.Header) (string, bool) {
}
func (s *SocialGithub) FetchOrganizations(client *http.Client, organizationsUrl string) ([]string, error) {
url := organizationsUrl
hasMore := true
logins := make([]string, 0)
type Record struct {
Login string `json:"login"`
}
response, err := s.httpGet(client, organizationsUrl)
if err != nil {
return nil, fmt.Errorf("error getting organizations: %s", err)
for hasMore {
response, err := s.httpGet(client, url)
if err != nil {
return nil, fmt.Errorf("error getting organizations: %s", err)
}
var records []Record
err = json.Unmarshal(response.Body, &records)
if err != nil {
return nil, fmt.Errorf("error getting organizations: %s", err)
}
for _, record := range records {
logins = append(logins, record.Login)
}
url, hasMore = s.HasMoreRecords(response.Headers)
}
var records []Record
err = json.Unmarshal(response.Body, &records)
if err != nil {
return nil, fmt.Errorf("error getting organizations: %s", err)
}
var logins = make([]string, len(records))
for i, record := range records {
logins[i] = record.Login
}
return logins, nil
}
@ -218,7 +224,7 @@ func (s *SocialGithub) UserInfo(client *http.Client, token *oauth2.Token) (*Basi
userInfo.Name = data.Name
}
organizationsUrl := fmt.Sprintf(s.apiUrl + "/orgs")
organizationsUrl := fmt.Sprintf(s.apiUrl + "/orgs?per_page=100")
if !s.IsTeamMember(client) {
return nil, ErrMissingTeamMembership