Skip to content

Commit

Permalink
fix: use pagination to fetch all team members (#1092)
Browse files Browse the repository at this point in the history
* fix: use pagination to fetch all team members

* chore: typo

Co-authored-by: Pascal Hofmann <[email protected]>

Co-authored-by: Camilo Roca <[email protected]>
Co-authored-by: Pascal Hofmann <[email protected]>
Co-authored-by: Keegan Campbell <[email protected]>
  • Loading branch information
4 people authored Mar 25, 2022
1 parent 0f94cc2 commit fa02655
Showing 1 changed file with 43 additions and 8 deletions.
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

0 comments on commit fa02655

Please sign in to comment.