Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create_default_maintainer Option To github_team #661

Merged
merged 2 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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