From fa02655ee720156aadbd4bb38564f9fbfd269026 Mon Sep 17 00:00:00 2001 From: Camilo Roca Date: Fri, 25 Mar 2022 22:13:49 +0100 Subject: [PATCH] fix: use pagination to fetch all team members (#1092) * fix: use pagination to fetch all team members * chore: typo Co-authored-by: Pascal Hofmann Co-authored-by: Camilo Roca Co-authored-by: Pascal Hofmann Co-authored-by: Keegan Campbell --- github/resource_github_team_members.go | 51 ++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/github/resource_github_team_members.go b/github/resource_github_team_members.go index 7726afcac0..241a458857 100644 --- a/github/resource_github_team_members.go +++ b/github/resource_github_team_members.go @@ -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)) @@ -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 }