-
Notifications
You must be signed in to change notification settings - Fork 960
Expand file tree
/
Copy pathresource_github_team_settings.go
More file actions
364 lines (322 loc) · 12.3 KB
/
resource_github_team_settings.go
File metadata and controls
364 lines (322 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package github
import (
"context"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/shurcooL/githubv4"
)
func resourceGithubTeamSettings() *schema.Resource {
return &schema.Resource{
CreateContext: resourceGithubTeamSettingsCreate,
ReadContext: resourceGithubTeamSettingsRead,
UpdateContext: resourceGithubTeamSettingsUpdate,
DeleteContext: resourceGithubTeamSettingsDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceGithubTeamSettingsImport,
},
Schema: map[string]*schema.Schema{
"team_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The GitHub team id or the GitHub team slug.",
},
"team_slug": {
Type: schema.TypeString,
Computed: true,
Description: "The slug of the Team within the Organization.",
},
"team_uid": {
Type: schema.TypeString,
Computed: true,
Description: "The unique ID of the Team on GitHub. Corresponds to the ID of the 'github_team_settings' resource.",
},
"notify": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether to notify the entire team when at least one member is also assigned to the pull request.",
ConflictsWith: []string{"review_request_delegation.0.notify"},
},
"review_request_delegation": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "The settings for delegating code reviews to individuals on behalf of the team. If this block is present, even without any fields, then review request delegation will be enabled for the team.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"algorithm": {
Type: schema.TypeString,
Optional: true,
Description: "The algorithm to use when assigning pull requests to team members. Supported values are " + string(githubv4.TeamReviewAssignmentAlgorithmRoundRobin) + " and " + string(githubv4.TeamReviewAssignmentAlgorithmLoadBalance) + ".",
Default: string(githubv4.TeamReviewAssignmentAlgorithmRoundRobin),
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{string(githubv4.TeamReviewAssignmentAlgorithmRoundRobin), string(githubv4.TeamReviewAssignmentAlgorithmLoadBalance)}, false)),
},
"member_count": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
Description: "The number of team members to assign to a pull request.",
ValidateDiagFunc: validation.ToDiagFunc(validation.All(
validation.IntAtLeast(1),
)),
},
"notify": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "whether to notify the entire team when at least one member is also assigned to the pull request.",
Deprecated: "Use the top-level notify attribute instead.",
ConflictsWith: []string{"notify"},
},
},
},
},
},
}
}
func resourceGithubTeamSettingsCreate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
meta := m.(*Owner)
if err := checkOrganization(m); err != nil {
return diag.FromErr(err)
}
graphql := meta.v4client
teamIDString := d.Get("team_id").(string)
tflog.Debug(ctx, "Resolving team_id to Team node_id and slug", map[string]any{
"team_id": teamIDString,
})
// Given a string that is either a team id or team slug, return the
// get the basic details of the team including node_id and slug
nodeId, slug, err := resolveTeamIDs(ctx, meta, teamIDString)
if err != nil {
return diag.FromErr(err)
}
tflog.Trace(ctx, "Resolved team_id to Team node_id and slug", map[string]any{
"node_id": nodeId,
"slug": slug,
})
d.SetId(nodeId)
if err = d.Set("team_slug", slug); err != nil {
return diag.FromErr(err)
}
if err = d.Set("team_uid", nodeId); err != nil {
return diag.FromErr(err)
}
reviewRequestDelegation := d.Get("review_request_delegation").([]any)
var mutation struct {
UpdateTeamReviewAssignment struct {
ClientMutationId githubv4.ID `graphql:"clientMutationId"`
} `graphql:"updateTeamReviewAssignment(input:$input)"`
}
tflog.Debug(ctx, "Review request delegation settings", map[string]any{
"team_id": d.Id(),
"team_slug": slug,
"review_request_delegation": reviewRequestDelegation,
"length_of_settings": len(reviewRequestDelegation),
})
notify := resolveNotify(ctx, d)
if len(reviewRequestDelegation) == 0 {
tflog.Debug(ctx, "No review request delegation settings provided, disabling review request delegation", map[string]any{
"team_id": d.Id(),
"team_slug": slug,
"notify": notify,
})
input := defaultTeamReviewAssignmentSettings(d.Id())
input.NotifyTeam = new(githubv4.Boolean(notify))
err := graphql.Mutate(ctx, &mutation, input, nil)
if err != nil {
return diag.FromErr(err)
}
} else {
tflog.Debug(ctx, "Review request delegation settings provided, setting according to provided configuration", map[string]any{
"team_id": d.Id(),
"team_slug": slug,
"review_request_delegation": reviewRequestDelegation,
"notify": notify,
})
settings := reviewRequestDelegation[0].(map[string]any)
teamReviewAlgorithm := githubv4.TeamReviewAssignmentAlgorithm(settings["algorithm"].(string))
updateTeamReviewAssignmentInput := githubv4.UpdateTeamReviewAssignmentInput{
ID: d.Id(),
Enabled: githubv4.Boolean(true),
Algorithm: &teamReviewAlgorithm,
TeamMemberCount: new(githubv4.Int(settings["member_count"].(int))),
NotifyTeam: new(githubv4.Boolean(notify)),
}
err := graphql.Mutate(ctx, &mutation, updateTeamReviewAssignmentInput, nil)
if err != nil {
return diag.FromErr(err)
}
}
return nil
}
func resourceGithubTeamSettingsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
if err := checkOrganization(meta); err != nil {
return diag.FromErr(err)
}
graphql := meta.(*Owner).v4client
orgName := meta.(*Owner).name
teamSlug := d.Get("team_slug").(string)
query := queryTeamSettings{}
variables := map[string]any{
"slug": githubv4.String(teamSlug),
"login": githubv4.String(orgName),
}
err := graphql.Query(ctx, &query, variables)
if err != nil {
return diag.FromErr(err)
}
notifyValue := query.Organization.Team.ReviewRequestDelegationNotifyAll
// Set notify in the location matching the user's config: top-level or
// deprecated nested field inside review_request_delegation.
_, usesDeprecatedNotify := d.GetOk("review_request_delegation.0.notify")
tflog.Debug(ctx, "Uses deprecated notify", map[string]any{
"uses_deprecated_notify": usesDeprecatedNotify,
"notify_value": notifyValue,
})
if !usesDeprecatedNotify {
if err = d.Set("notify", notifyValue); err != nil {
return diag.FromErr(err)
}
}
if query.Organization.Team.ReviewRequestDelegation {
reviewRequestDelegation := make(map[string]any)
reviewRequestDelegation["algorithm"] = query.Organization.Team.ReviewRequestDelegationAlgorithm
reviewRequestDelegation["member_count"] = query.Organization.Team.ReviewRequestDelegationCount
if usesDeprecatedNotify {
reviewRequestDelegation["notify"] = notifyValue
}
if err = d.Set("review_request_delegation", []any{reviewRequestDelegation}); err != nil {
return diag.FromErr(err)
}
} else {
if err = d.Set("review_request_delegation", []any{}); err != nil {
return diag.FromErr(err)
}
}
return nil
}
func resourceGithubTeamSettingsUpdate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
if d.HasChanges("review_request_delegation", "notify") {
meta := m.(*Owner)
graphql := meta.v4client
reviewRequestDelegation := d.Get("review_request_delegation").([]any)
notify := resolveNotify(ctx, d)
var mutation struct {
UpdateTeamReviewAssignment struct {
ClientMutationId githubv4.ID `graphql:"clientMutationId"`
} `graphql:"updateTeamReviewAssignment(input:$input)"`
}
if len(reviewRequestDelegation) == 0 {
tflog.Debug(ctx, "No review request delegation settings provided, disabling review request delegation", map[string]any{
"team_id": d.Id(),
"team_slug": d.Get("team_slug").(string),
"notify": notify,
})
input := defaultTeamReviewAssignmentSettings(d.Id())
input.NotifyTeam = new(githubv4.Boolean(notify))
err := graphql.Mutate(ctx, &mutation, input, nil)
if err != nil {
return diag.FromErr(err)
}
} else {
settings := reviewRequestDelegation[0].(map[string]any)
teamReviewAlgorithm := githubv4.TeamReviewAssignmentAlgorithm(settings["algorithm"].(string))
updateTeamReviewAssignmentInput := githubv4.UpdateTeamReviewAssignmentInput{
ID: d.Id(),
Enabled: githubv4.Boolean(true),
Algorithm: &teamReviewAlgorithm,
TeamMemberCount: new(githubv4.Int(settings["member_count"].(int))),
NotifyTeam: new(githubv4.Boolean(notify)),
}
err := graphql.Mutate(ctx, &mutation, updateTeamReviewAssignmentInput, nil)
if err != nil {
return diag.FromErr(err)
}
}
}
return nil
}
func resourceGithubTeamSettingsDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
graphql := meta.(*Owner).v4client
var mutation struct {
UpdateTeamReviewAssignment struct {
ClientMutationId githubv4.ID `graphql:"clientMutationId"`
} `graphql:"updateTeamReviewAssignment(input:$input)"`
}
err := graphql.Mutate(ctx, &mutation, defaultTeamReviewAssignmentSettings(d.Id()), nil)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceGithubTeamSettingsImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
nodeId, slug, err := resolveTeamIDs(ctx, meta.(*Owner), d.Id())
if err != nil {
return nil, err
}
if err = d.Set("team_id", d.Id()); err != nil {
return nil, err
}
d.SetId(nodeId)
if err = d.Set("team_slug", slug); err != nil {
return nil, err
}
if err = d.Set("team_uid", nodeId); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}
func resolveTeamIDs(ctx context.Context, meta *Owner, idOrSlug string) (string, string, error) {
team, err := getTeam(ctx, meta, idOrSlug)
if err != nil {
return "", "", err
}
return team.GetNodeID(), team.GetSlug(), nil
}
// resolveNotify returns the notify value from the top-level attribute or the
// deprecated nested attribute inside review_request_delegation. The top-level
// attribute takes precedence. Since ConflictsWith prevents both from being set,
// only one source can be active at a time.
func resolveNotify(ctx context.Context, d *schema.ResourceData) bool {
// Check if top-level notify is explicitly configured.
if v := d.Get("notify").(bool); v {
tflog.Debug(ctx, "Top-level notify is explicitly configured", map[string]any{
"notify": v,
})
return v
}
// Fall back to deprecated nested field
if v := d.Get("review_request_delegation.0.notify").(bool); v {
tflog.Debug(ctx, "Deprecated nested notify is explicitly configured", map[string]any{
"notify": v,
})
return v
}
return false
}
func defaultTeamReviewAssignmentSettings(id string) githubv4.UpdateTeamReviewAssignmentInput {
roundRobinAlgo := githubv4.TeamReviewAssignmentAlgorithmRoundRobin
return githubv4.UpdateTeamReviewAssignmentInput{
ID: id,
Enabled: githubv4.Boolean(false),
Algorithm: &roundRobinAlgo,
TeamMemberCount: githubv4.NewInt(githubv4.Int(1)),
NotifyTeam: githubv4.NewBoolean(true),
}
}
type queryTeamSettings struct {
Organization struct {
Team struct {
Name string `graphql:"name"`
Slug string `graphql:"slug"`
ID string `graphql:"id"`
ReviewRequestDelegation bool `graphql:"reviewRequestDelegationEnabled"`
ReviewRequestDelegationAlgorithm string `graphql:"reviewRequestDelegationAlgorithm"`
ReviewRequestDelegationCount int `graphql:"reviewRequestDelegationMemberCount"`
ReviewRequestDelegationNotifyAll bool `graphql:"reviewRequestDelegationNotifyTeam"`
} `graphql:"team(slug:$slug)"`
} `graphql:"organization(login:$login)"`
}