From 4bb9ffcfbe385125d9df1f224e575883b78e3932 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven!=20Ragnar=C3=B6k?= Date: Fri, 18 Jun 2021 14:15:57 -0700 Subject: [PATCH] Add members to organization data source. (#811) Lifting heavily from how repositories are fetched for the organization data source I added an attribute containing all members of the organization. --- github/data_source_github_organization.go | 32 +++++++++++++++++++ .../data_source_github_organization_test.go | 1 + 2 files changed, 33 insertions(+) diff --git a/github/data_source_github_organization.go b/github/data_source_github_organization.go index 83ee431a22..ed76cf3dcb 100644 --- a/github/data_source_github_organization.go +++ b/github/data_source_github_organization.go @@ -39,6 +39,13 @@ func dataSourceGithubOrganization() *schema.Resource { Type: schema.TypeString, }, }, + "members": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, }, } } @@ -81,12 +88,37 @@ func dataSourceGithubOrganizationRead(d *schema.ResourceData, meta interface{}) repoList = append(repoList, allRepos[index].GetFullName()) } + membershipOpts := &github.ListMembersOptions{ + ListOptions: github.ListOptions{PerPage: 10, Page: 1}, + } + + var memberList []string + var allMembers []*github.User + + for { + members, resp, err := client.Organizations.ListMembers(ctx, name, membershipOpts) + if err != nil { + return err + } + allMembers = append(allMembers, members...) + + membershipOpts.Page = resp.NextPage + + if resp.NextPage == 0 { + break + } + } + for index := range allMembers { + memberList = append(memberList, *allMembers[index].Login) + } + d.SetId(strconv.FormatInt(organization.GetID(), 10)) d.Set("login", organization.GetLogin()) d.Set("name", organization.GetName()) d.Set("description", organization.GetDescription()) d.Set("plan", plan.Name) d.Set("repositories", repoList) + d.Set("members", memberList) return nil } diff --git a/github/data_source_github_organization_test.go b/github/data_source_github_organization_test.go index c8e5a6efa3..a803a460f6 100644 --- a/github/data_source_github_organization_test.go +++ b/github/data_source_github_organization_test.go @@ -23,6 +23,7 @@ func TestAccGithubOrganizationDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_organization.test", "description"), resource.TestCheckResourceAttrSet("data.github_organization.test", "plan"), resource.TestCheckResourceAttrSet("data.github_organization.test", "repositories.#"), + resource.TestCheckResourceAttrSet("data.github_organization.test", "members.#"), ) testCase := func(t *testing.T, mode string) {