-
Notifications
You must be signed in to change notification settings - Fork 777
/
Copy pathresource_github_branch_protection_v3.go
408 lines (369 loc) · 11.9 KB
/
resource_github_branch_protection_v3.go
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package github
import (
"context"
"fmt"
"log"
"net/http"
"github.com/google/go-github/v66/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func resourceGithubBranchProtectionV3() *schema.Resource {
return &schema.Resource{
Create: resourceGithubBranchProtectionV3Create,
Read: resourceGithubBranchProtectionV3Read,
Update: resourceGithubBranchProtectionV3Update,
Delete: resourceGithubBranchProtectionV3Delete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The GitHub repository name.",
},
"branch": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The Git branch to protect.",
},
"required_status_checks": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Enforce restrictions for required status checks.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"include_admins": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Deprecated: "Use enforce_admins instead",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return true
},
},
"strict": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Require branches to be up to date before merging.",
},
"contexts": {
Type: schema.TypeSet,
Optional: true,
Deprecated: "GitHub is deprecating the use of `contexts`. Use a `checks` array instead.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"checks": {
Type: schema.TypeSet,
Optional: true,
Description: "The list of status checks to require in order to merge into this branch. No status checks are required by default. Checks should be strings containing the 'context' and 'app_id' like so 'context:app_id'",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"required_pull_request_reviews": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Enforce restrictions for pull request reviews.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// FIXME: Remove this deprecated field
"include_admins": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Deprecated: "Use enforce_admins instead",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return true
},
},
"dismiss_stale_reviews": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Dismiss approved reviews automatically when a new commit is pushed.",
},
"dismissal_users": {
Type: schema.TypeSet,
Optional: true,
Description: "The list of user logins with dismissal access.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"dismissal_teams": {
Type: schema.TypeSet,
Optional: true,
Description: "The list of team slugs with dismissal access. Always use slug of the team, not its name. Each team already has to have access to the repository.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"dismissal_apps": {
Type: schema.TypeSet,
Optional: true,
Description: "The list of apps slugs with dismissal access. Always use slug of the app, not its name. Each app already has to have access to the repository.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"require_code_owner_reviews": {
Type: schema.TypeBool,
Optional: true,
Description: "Require an approved review in pull requests including files with a designated code owner.",
},
"required_approving_review_count": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
Description: "Require 'x' number of approvals to satisfy branch protection requirements. If this is specified it must be a number between 0-6.",
ValidateDiagFunc: toDiagFunc(validation.IntBetween(0, 6), "required_approving_review_count"),
},
"require_last_push_approval": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Require that the most recent push must be approved by someone other than the last pusher.",
},
"bypass_pull_request_allowances": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"users": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"teams": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"apps": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
},
},
},
"restrictions": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Enforce restrictions for the users and teams that may push to the branch.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"users": {
Type: schema.TypeSet,
Optional: true,
Description: "The list of user logins with push access.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"teams": {
Type: schema.TypeSet,
Optional: true,
Description: "The list of team slugs with push access. Always use slug of the team, not its name. Each team already has to have access to the repository.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"apps": {
Type: schema.TypeSet,
Optional: true,
Description: "The list of app slugs with push access.",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"enforce_admins": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Setting this to 'true' enforces status checks for repository administrators.",
},
"require_signed_commits": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Setting this to 'true' requires all commits to be signed with GPG.",
},
"require_conversation_resolution": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Setting this to 'true' requires all conversations on code must be resolved before a pull request can be merged.",
},
"etag": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceGithubBranchProtectionV3Create(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
repoName := d.Get("repository").(string)
branch := d.Get("branch").(string)
protectionRequest, err := buildProtectionRequest(d)
if err != nil {
return err
}
ctx := context.Background()
protection, _, err := client.Repositories.UpdateBranchProtection(ctx,
orgName,
repoName,
branch,
protectionRequest,
)
if err != nil {
return err
}
if err := checkBranchRestrictionsUsers(protection.GetRestrictions(), protectionRequest.GetRestrictions()); err != nil {
return err
}
d.SetId(buildTwoPartID(repoName, branch))
if err = requireSignedCommitsUpdate(d, meta); err != nil {
return err
}
return resourceGithubBranchProtectionV3Read(d, meta)
}
func resourceGithubBranchProtectionV3Read(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
repoName, branch, err := parseTwoPartID(d.Id(), "repository", "branch")
if err != nil {
return err
}
orgName := meta.(*Owner).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())
if !d.IsNewResource() {
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
}
githubProtection, resp, err := client.Repositories.GetBranchProtection(ctx,
orgName, repoName, branch)
if err != nil {
if ghErr, ok := err.(*github.ErrorResponse); ok {
if ghErr.Response.StatusCode == http.StatusNotModified {
if err := requireSignedCommitsRead(d, meta); err != nil {
return fmt.Errorf("error setting signed commit restriction: %v", err)
}
return nil
}
if ghErr.Response.StatusCode == http.StatusNotFound {
log.Printf("[INFO] Removing branch protection %s/%s (%s) from state because it no longer exists in GitHub",
orgName, repoName, branch)
d.SetId("")
return nil
}
}
return err
}
if err = d.Set("etag", resp.Header.Get("ETag")); err != nil {
return err
}
if err = d.Set("repository", repoName); err != nil {
return err
}
if err = d.Set("branch", branch); err != nil {
return err
}
if err = d.Set("enforce_admins", githubProtection.GetEnforceAdmins().Enabled); err != nil {
return err
}
if rcr := githubProtection.GetRequiredConversationResolution(); rcr != nil {
if err = d.Set("require_conversation_resolution", rcr.Enabled); err != nil {
return err
}
}
if err := flattenAndSetRequiredStatusChecks(d, githubProtection); err != nil {
return fmt.Errorf("error setting required_status_checks: %v", err)
}
if err := flattenAndSetRequiredPullRequestReviews(d, githubProtection); err != nil {
return fmt.Errorf("error setting required_pull_request_reviews: %v", err)
}
if err := flattenAndSetRestrictions(d, githubProtection); err != nil {
return fmt.Errorf("error setting restrictions: %v", err)
}
if err := requireSignedCommitsRead(d, meta); err != nil {
return fmt.Errorf("error setting signed commit restriction: %v", err)
}
return nil
}
func resourceGithubBranchProtectionV3Update(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
repoName, branch, err := parseTwoPartID(d.Id(), "repository", "branch")
if err != nil {
return err
}
protectionRequest, err := buildProtectionRequest(d)
if err != nil {
return err
}
orgName := meta.(*Owner).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())
protection, _, err := client.Repositories.UpdateBranchProtection(ctx,
orgName,
repoName,
branch,
protectionRequest,
)
if err != nil {
return err
}
if err := checkBranchRestrictionsUsers(protection.GetRestrictions(), protectionRequest.GetRestrictions()); err != nil {
return err
}
if protectionRequest.RequiredPullRequestReviews == nil {
_, err = client.Repositories.RemovePullRequestReviewEnforcement(ctx,
orgName,
repoName,
branch,
)
if err != nil {
return err
}
}
d.SetId(buildTwoPartID(repoName, branch))
if err = requireSignedCommitsUpdate(d, meta); err != nil {
return err
}
return resourceGithubBranchProtectionV3Read(d, meta)
}
func resourceGithubBranchProtectionV3Delete(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
repoName, branch, err := parseTwoPartID(d.Id(), "repository", "branch")
if err != nil {
return err
}
orgName := meta.(*Owner).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())
_, err = client.Repositories.RemoveBranchProtection(ctx,
orgName, repoName, branch)
return err
}