88
99 "github.com/google/go-github/v32/github"
1010 "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
11+ "github.com/shurcooL/githubv4"
1112)
1213
1314func resourceGithubTeam () * schema.Resource {
@@ -43,6 +44,11 @@ func resourceGithubTeam() *schema.Resource {
4344 Type : schema .TypeString ,
4445 Optional : true ,
4546 },
47+ "create_default_maintainer" : {
48+ Type : schema .TypeBool ,
49+ Optional : true ,
50+ Default : false ,
51+ },
4652 "slug" : {
4753 Type : schema .TypeString ,
4854 Computed : true ,
@@ -55,6 +61,10 @@ func resourceGithubTeam() *schema.Resource {
5561 Type : schema .TypeString ,
5662 Computed : true ,
5763 },
64+ "members_count" : {
65+ Type : schema .TypeInt ,
66+ Computed : true ,
67+ },
5868 },
5969 }
6070}
@@ -67,26 +77,36 @@ func resourceGithubTeamCreate(d *schema.ResourceData, meta interface{}) error {
6777
6878 client := meta .(* Owner ).v3client
6979
70- orgName := meta .(* Owner ).name
80+ ownerName := meta .(* Owner ).name
7181 name := d .Get ("name" ).(string )
82+
7283 newTeam := github.NewTeam {
7384 Name : name ,
7485 Description : github .String (d .Get ("description" ).(string )),
7586 Privacy : github .String (d .Get ("privacy" ).(string )),
7687 }
88+
7789 if parentTeamID , ok := d .GetOk ("parent_team_id" ); ok {
7890 id := int64 (parentTeamID .(int ))
7991 newTeam .ParentTeamID = & id
8092 }
8193 ctx := context .Background ()
8294
83- log .Printf ("[DEBUG] Creating team: %s (%s)" , name , orgName )
95+ log .Printf ("[DEBUG] Creating team: %s (%s)" , name , ownerName )
8496 githubTeam , _ , err := client .Teams .CreateTeam (ctx ,
85- orgName , newTeam )
97+ ownerName , newTeam )
8698 if err != nil {
8799 return err
88100 }
89101
102+ create_default_maintainer := d .Get ("create_default_maintainer" ).(bool )
103+ if ! create_default_maintainer {
104+ log .Printf ("[DEBUG] Removing default maintainer from team: %s (%s)" , name , ownerName )
105+ if err := removeDefaultMaintainer (* githubTeam .Slug , meta ); err != nil {
106+ return err
107+ }
108+ }
109+
90110 if ldapDN := d .Get ("ldap_dn" ).(string ); ldapDN != "" {
91111 mapping := & github.TeamLDAPMapping {
92112 LDAPDN : github .String (ldapDN ),
@@ -148,6 +168,7 @@ func resourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
148168 d .Set ("ldap_dn" , team .GetLDAPDN ())
149169 d .Set ("slug" , team .GetSlug ())
150170 d .Set ("node_id" , team .GetNodeID ())
171+ d .Set ("members_count" , team .GetMembersCount ())
151172
152173 return nil
153174}
@@ -217,3 +238,43 @@ func resourceGithubTeamDelete(d *schema.ResourceData, meta interface{}) error {
217238 _ , err = client .Teams .DeleteTeamByID (ctx , orgId , id )
218239 return err
219240}
241+
242+ func removeDefaultMaintainer (teamSlug string , meta interface {}) error {
243+
244+ client := meta .(* Owner ).v3client
245+ orgName := meta .(* Owner ).name
246+ v4client := meta .(* Owner ).v4client
247+
248+ type User struct {
249+ Login githubv4.String
250+ }
251+
252+ var query struct {
253+ Organization struct {
254+ Team struct {
255+ Members struct {
256+ Nodes []User
257+ }
258+ } `graphql:"team(slug:$slug)"`
259+ } `graphql:"organization(login:$login)"`
260+ }
261+ variables := map [string ]interface {}{
262+ "slug" : githubv4 .String (teamSlug ),
263+ "login" : githubv4 .String (orgName ),
264+ }
265+
266+ err := v4client .Query (meta .(* Owner ).StopContext , & query , variables )
267+ if err != nil {
268+ return err
269+ }
270+
271+ for _ , user := range query .Organization .Team .Members .Nodes {
272+ log .Printf ("[DEBUG] Removing default maintainer from team: %s" , user .Login )
273+ _ , err := client .Teams .RemoveTeamMembershipBySlug (meta .(* Owner ).StopContext , orgName , teamSlug , string (user .Login ))
274+ if err != nil {
275+ return err
276+ }
277+ }
278+
279+ return nil
280+ }
0 commit comments