Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use pagination to fetch all team members #1092

Merged
merged 3 commits into from
Mar 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions github/resource_github_team_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,52 @@ func resourceGithubTeamMembersRead(d *schema.ResourceData, meta interface{}) err
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
}

etags := make([]string, 0)
// List members & maintainers as list 'all' drops role information
log.Printf("[DEBUG] Reading team members: %s", teamIdString)
members, resp1, err := client.Teams.ListTeamMembersByID(ctx, orgId, teamId, &github.TeamListTeamMembersOptions{Role: "member"})
if err != nil {
return err
memberOptions := github.TeamListTeamMembersOptions{
ListOptions: github.ListOptions{
PerPage: maxPerPage,
},
Role: "member",
}

var members []*github.User
for {
member, resp, err := client.Teams.ListTeamMembersByID(ctx, orgId, teamId, &memberOptions)
if err != nil {
return err
}

etags = append(etags, resp.Header.Get("ETag"))
members = append(members, member...)
if resp.NextPage == 0 {
break
}
memberOptions.Page = resp.NextPage
}

log.Printf("[DEBUG] Reading team maintainers: %s", teamIdString)
maintainers, resp2, err := client.Teams.ListTeamMembersByID(ctx, orgId, teamId, &github.TeamListTeamMembersOptions{Role: "maintainer"})
if err != nil {
return err
maintainerOptions := github.TeamListTeamMembersOptions{
ListOptions: github.ListOptions{
PerPage: maxPerPage,
},
Role: "maintainer",
}
var maintainers []*github.User
for {
maintaner, resp, err := client.Teams.ListTeamMembersByID(ctx, orgId, teamId, &maintainerOptions)
if err != nil {
return err
}

etags = append(etags, resp.Header.Get("ETag"))
maintainers = append(maintainers, maintaner...)

if resp.NextPage == 0 {
break
}
maintainerOptions.Page = resp.NextPage
}

teamMembersAndMaintainers := make([]interface{}, len(members)+len(maintainers))
Expand All @@ -238,8 +273,8 @@ func resourceGithubTeamMembersRead(d *schema.ResourceData, meta interface{}) err
return err
}

// Combine etag of both requests
d.Set("etag", buildTwoPartID(resp1.Header.Get("ETag"), resp2.Header.Get("ETag")))
// Combine etag of all requests
d.Set("etag", buildChecksumID(etags))

return nil
}
Expand Down