8
8
9
9
"github.com/google/go-github/v32/github"
10
10
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
11
+ "github.com/shurcooL/githubv4"
11
12
)
12
13
13
14
func resourceGithubTeam () * schema.Resource {
@@ -43,6 +44,11 @@ func resourceGithubTeam() *schema.Resource {
43
44
Type : schema .TypeString ,
44
45
Optional : true ,
45
46
},
47
+ "create_default_maintainer" : {
48
+ Type : schema .TypeBool ,
49
+ Optional : true ,
50
+ Default : false ,
51
+ },
46
52
"slug" : {
47
53
Type : schema .TypeString ,
48
54
Computed : true ,
@@ -55,6 +61,10 @@ func resourceGithubTeam() *schema.Resource {
55
61
Type : schema .TypeString ,
56
62
Computed : true ,
57
63
},
64
+ "members_count" : {
65
+ Type : schema .TypeInt ,
66
+ Computed : true ,
67
+ },
58
68
},
59
69
}
60
70
}
@@ -67,26 +77,36 @@ func resourceGithubTeamCreate(d *schema.ResourceData, meta interface{}) error {
67
77
68
78
client := meta .(* Owner ).v3client
69
79
70
- orgName := meta .(* Owner ).name
80
+ ownerName := meta .(* Owner ).name
71
81
name := d .Get ("name" ).(string )
82
+
72
83
newTeam := github.NewTeam {
73
84
Name : name ,
74
85
Description : github .String (d .Get ("description" ).(string )),
75
86
Privacy : github .String (d .Get ("privacy" ).(string )),
76
87
}
88
+
77
89
if parentTeamID , ok := d .GetOk ("parent_team_id" ); ok {
78
90
id := int64 (parentTeamID .(int ))
79
91
newTeam .ParentTeamID = & id
80
92
}
81
93
ctx := context .Background ()
82
94
83
- log .Printf ("[DEBUG] Creating team: %s (%s)" , name , orgName )
95
+ log .Printf ("[DEBUG] Creating team: %s (%s)" , name , ownerName )
84
96
githubTeam , _ , err := client .Teams .CreateTeam (ctx ,
85
- orgName , newTeam )
97
+ ownerName , newTeam )
86
98
if err != nil {
87
99
return err
88
100
}
89
101
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
+
90
110
if ldapDN := d .Get ("ldap_dn" ).(string ); ldapDN != "" {
91
111
mapping := & github.TeamLDAPMapping {
92
112
LDAPDN : github .String (ldapDN ),
@@ -148,6 +168,7 @@ func resourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
148
168
d .Set ("ldap_dn" , team .GetLDAPDN ())
149
169
d .Set ("slug" , team .GetSlug ())
150
170
d .Set ("node_id" , team .GetNodeID ())
171
+ d .Set ("members_count" , team .GetMembersCount ())
151
172
152
173
return nil
153
174
}
@@ -217,3 +238,43 @@ func resourceGithubTeamDelete(d *schema.ResourceData, meta interface{}) error {
217
238
_ , err = client .Teams .DeleteTeamByID (ctx , orgId , id )
218
239
return err
219
240
}
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