Skip to content

Commit

Permalink
Add members to organization data source. (integrations#811)
Browse files Browse the repository at this point in the history
Lifting heavily from how repositories are fetched for the organization
data source I added an attribute containing all members of the
organization.
  • Loading branch information
nuclearsandwich authored Jun 18, 2021
1 parent 851be51 commit 4bb9ffc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions github/data_source_github_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func dataSourceGithubOrganization() *schema.Resource {
Type: schema.TypeString,
},
},
"members": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions github/data_source_github_organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 4bb9ffc

Please sign in to comment.