Skip to content

Commit

Permalink
Use graphql rather than rest API for Github team membership (#1786)
Browse files Browse the repository at this point in the history
This change should provide better diffing for team membership for teams
with child teams. Currently, child team members are included in the parent
team's members list and so diffs are incorrect.

See [Issue 1193](#1193).
  • Loading branch information
joshua9519 authored Jul 25, 2023
1 parent b490535 commit 3159217
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 66 deletions.
92 changes: 27 additions & 65 deletions github/resource_github_team_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"log"
"reflect"
"strings"

"github.com/google/go-github/v53/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/shurcooL/githubv4"
)

type MemberChange struct {
Expand Down Expand Up @@ -53,10 +55,6 @@ func resourceGithubTeamMembers() *schema.Resource {
},
},
},
"etag": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -176,15 +174,15 @@ func resourceGithubTeamMembersUpdate(d *schema.ResourceData, meta interface{}) e
}

func resourceGithubTeamMembersRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
orgId := meta.(*Owner).id
client := meta.(*Owner).v4client
orgName := meta.(*Owner).name
teamIdString := d.Get("team_id").(string)
if teamIdString == "" && !d.IsNewResource() {
log.Printf("[DEBUG] Importing team with id %q", d.Id())
teamIdString = d.Id()
}

teamId, err := getTeamID(teamIdString, meta)
teamSlug, err := getTeamSlug(teamIdString, meta)
if err != nil {
return err
}
Expand All @@ -195,81 +193,45 @@ func resourceGithubTeamMembersRead(d *schema.ResourceData, meta interface{}) err
d.Set("team_id", teamIdString)

ctx := context.WithValue(context.Background(), ctxId, d.Id())
if !d.IsNewResource() {
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)
memberOptions := github.TeamListTeamMembersOptions{
ListOptions: github.ListOptions{
PerPage: maxPerPage,
},
Role: "member",
var q struct {
Organization struct {
Team struct {
Members struct {
Edges []struct {
Node struct {
Login string
}
Role string
}
} `graphql:"members(membership:IMMEDIATE)"`
} `graphql:"team(slug:$teamSlug)"`
} `graphql:"organization(login:$orgName)"`
}

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
variables := map[string]interface{}{
"teamSlug": githubv4.String(teamSlug),
"orgName": githubv4.String(orgName),
}

log.Printf("[DEBUG] Reading team maintainers: %s", teamIdString)
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
if err := client.Query(ctx, &q, variables); err != nil {
return err
}

teamMembersAndMaintainers := make([]interface{}, len(members)+len(maintainers))
teamMembersAndMaintainers := make([]interface{}, len(q.Organization.Team.Members.Edges))
// Add all members to the list
for i, member := range members {
for i, member := range q.Organization.Team.Members.Edges {
teamMembersAndMaintainers[i] = map[string]interface{}{
"username": member.Login,
"role": "member",
}
}
// Add all maintainers to the list
for i, member := range maintainers {
teamMembersAndMaintainers[i+len(members)] = map[string]interface{}{
"username": member.Login,
"role": "maintainer",
"username": member.Node.Login,
"role": strings.ToLower(member.Role),
}
}

if err := d.Set("members", teamMembersAndMaintainers); err != nil {
return err
}

// Combine etag of all requests
d.Set("etag", buildChecksumID(etags))

return nil
}

Expand Down
1 change: 0 additions & 1 deletion github/resource_github_team_members_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func TestAccGithubTeamMembers(t *testing.T) {
{
Config: testAccGithubTeamMembersConfig(randomID, testCollaborator, "member"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "etag"),
testAccCheckGithubTeamMembersExists(resourceName, &membership),
testAccCheckGithubTeamMembersRoleState(resourceName, "member", &membership),
),
Expand Down

0 comments on commit 3159217

Please sign in to comment.