Skip to content

Commit

Permalink
Add create_default_maintainer Option To github_team (#661)
Browse files Browse the repository at this point in the history
* Add `create_default_maintainer` option to `github_team`

This adds a possible fix to the following issues by providing users with an option to remove the automatic addition of a default maintainer to a team during creation.

/cc #527
/cc #104
/cc #130
  • Loading branch information
Jeremy Udit authored Feb 5, 2021
1 parent c5eda72 commit 3ab313b
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 229 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 4.4.0 (February 5, 2021)

BUG FIXES:

- Add `create_default_maintainer` option to `github_team` ([#527](https://github.com/integrations/terraform-provider-github/pull/527)), ([#104](https://github.com/integrations/terraform-provider-github/pull/104)), ([#130](https://github.com/integrations/terraform-provider-github/pull/130))


## 4.3.2 (February 2, 2021)

BUG FIXES:
Expand Down
67 changes: 64 additions & 3 deletions github/resource_github_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

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

func resourceGithubTeam() *schema.Resource {
Expand Down Expand Up @@ -43,6 +44,11 @@ func resourceGithubTeam() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"create_default_maintainer": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"slug": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -55,6 +61,10 @@ func resourceGithubTeam() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"members_count": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
Expand All @@ -67,26 +77,36 @@ func resourceGithubTeamCreate(d *schema.ResourceData, meta interface{}) error {

client := meta.(*Owner).v3client

orgName := meta.(*Owner).name
ownerName := meta.(*Owner).name
name := d.Get("name").(string)

newTeam := github.NewTeam{
Name: name,
Description: github.String(d.Get("description").(string)),
Privacy: github.String(d.Get("privacy").(string)),
}

if parentTeamID, ok := d.GetOk("parent_team_id"); ok {
id := int64(parentTeamID.(int))
newTeam.ParentTeamID = &id
}
ctx := context.Background()

log.Printf("[DEBUG] Creating team: %s (%s)", name, orgName)
log.Printf("[DEBUG] Creating team: %s (%s)", name, ownerName)
githubTeam, _, err := client.Teams.CreateTeam(ctx,
orgName, newTeam)
ownerName, newTeam)
if err != nil {
return err
}

create_default_maintainer := d.Get("create_default_maintainer").(bool)
if !create_default_maintainer {
log.Printf("[DEBUG] Removing default maintainer from team: %s (%s)", name, ownerName)
if err := removeDefaultMaintainer(*githubTeam.Slug, meta); err != nil {
return err
}
}

if ldapDN := d.Get("ldap_dn").(string); ldapDN != "" {
mapping := &github.TeamLDAPMapping{
LDAPDN: github.String(ldapDN),
Expand Down Expand Up @@ -148,6 +168,7 @@ func resourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
d.Set("ldap_dn", team.GetLDAPDN())
d.Set("slug", team.GetSlug())
d.Set("node_id", team.GetNodeID())
d.Set("members_count", team.GetMembersCount())

return nil
}
Expand Down Expand Up @@ -217,3 +238,43 @@ func resourceGithubTeamDelete(d *schema.ResourceData, meta interface{}) error {
_, err = client.Teams.DeleteTeamByID(ctx, orgId, id)
return err
}

func removeDefaultMaintainer(teamSlug string, meta interface{}) error {

client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
v4client := meta.(*Owner).v4client

type User struct {
Login githubv4.String
}

var query struct {
Organization struct {
Team struct {
Members struct {
Nodes []User
}
} `graphql:"team(slug:$slug)"`
} `graphql:"organization(login:$login)"`
}
variables := map[string]interface{}{
"slug": githubv4.String(teamSlug),
"login": githubv4.String(orgName),
}

err := v4client.Query(meta.(*Owner).StopContext, &query, variables)
if err != nil {
return err
}

for _, user := range query.Organization.Team.Members.Nodes {
log.Printf("[DEBUG] Removing default maintainer from team: %s", user.Login)
_, err := client.Teams.RemoveTeamMembershipBySlug(meta.(*Owner).StopContext, orgName, teamSlug, string(user.Login))
if err != nil {
return err
}
}

return nil
}
Loading

0 comments on commit 3ab313b

Please sign in to comment.