@@ -62,6 +62,33 @@ func TestAccGitlabBranchProtection_basic(t *testing.T) {
62
62
}),
63
63
),
64
64
},
65
+ // Update the Branch Protection with allow force push enabled
66
+ {
67
+ Config : testAccGitlabBranchProtectionUpdateConfigAllowForcePushTrue (rInt ),
68
+ Check : resource .ComposeTestCheckFunc (
69
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.branch_protect" , & pb ),
70
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.branch_protect" , & pb ),
71
+ testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
72
+ Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
73
+ PushAccessLevel : accessLevelValueToName [gitlab .DeveloperPermissions ],
74
+ MergeAccessLevel : accessLevelValueToName [gitlab .DeveloperPermissions ],
75
+ AllowForcePush : true ,
76
+ }),
77
+ ),
78
+ },
79
+ // Update the Branch Protection to get back to initial settings
80
+ {
81
+ Config : testAccGitlabBranchProtectionConfig (rInt ),
82
+ Check : resource .ComposeTestCheckFunc (
83
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.branch_protect" , & pb ),
84
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.branch_protect" , & pb ),
85
+ testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
86
+ Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
87
+ PushAccessLevel : accessLevelValueToName [gitlab .DeveloperPermissions ],
88
+ MergeAccessLevel : accessLevelValueToName [gitlab .DeveloperPermissions ],
89
+ }),
90
+ ),
91
+ },
65
92
// Update the the Branch Protection code owner approval setting
66
93
{
67
94
SkipFunc : isRunningInCE ,
@@ -180,6 +207,59 @@ func TestAccGitlabBranchProtection_createWithCodeOwnerApproval(t *testing.T) {
180
207
})
181
208
}
182
209
210
+ func TestAccGitlabBranchProtection_createWithAllowForcePush (t * testing.T ) {
211
+ var pb gitlab.ProtectedBranch
212
+ rInt := acctest .RandInt ()
213
+
214
+ resource .Test (t , resource.TestCase {
215
+ PreCheck : func () { testAccPreCheck (t ) },
216
+ ProviderFactories : providerFactories ,
217
+ CheckDestroy : testAccCheckGitlabBranchProtectionDestroy ,
218
+ Steps : []resource.TestStep {
219
+ // Start with allow force push disabled
220
+ {
221
+ Config : testAccGitlabBranchProtectionConfig (rInt ),
222
+ Check : resource .ComposeTestCheckFunc (
223
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.branch_protect" , & pb ),
224
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.branch_protect" , & pb ),
225
+ testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
226
+ Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
227
+ PushAccessLevel : accessLevelValueToName [gitlab .DeveloperPermissions ],
228
+ MergeAccessLevel : accessLevelValueToName [gitlab .DeveloperPermissions ],
229
+ }),
230
+ ),
231
+ },
232
+ // Create a project and Branch Protection with allow force push enabled
233
+ {
234
+ Config : testAccGitlabBranchProtectionUpdateConfigAllowForcePushTrue (rInt ),
235
+ Check : resource .ComposeTestCheckFunc (
236
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.branch_protect" , & pb ),
237
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.branch_protect" , & pb ),
238
+ testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
239
+ Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
240
+ PushAccessLevel : accessLevelValueToName [gitlab .DeveloperPermissions ],
241
+ MergeAccessLevel : accessLevelValueToName [gitlab .DeveloperPermissions ],
242
+ AllowForcePush : true ,
243
+ }),
244
+ ),
245
+ },
246
+ // Update the Branch Protection to get back to initial settings
247
+ {
248
+ Config : testAccGitlabBranchProtectionConfig (rInt ),
249
+ Check : resource .ComposeTestCheckFunc (
250
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.branch_protect" , & pb ),
251
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.branch_protect" , & pb ),
252
+ testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
253
+ Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
254
+ PushAccessLevel : accessLevelValueToName [gitlab .DeveloperPermissions ],
255
+ MergeAccessLevel : accessLevelValueToName [gitlab .DeveloperPermissions ],
256
+ }),
257
+ ),
258
+ },
259
+ },
260
+ })
261
+ }
262
+
183
263
func TestAccGitlabBranchProtection_createWithMultipleAccessLevels (t * testing.T ) {
184
264
var pb gitlab.ProtectedBranch
185
265
rInt := acctest .RandInt ()
@@ -258,6 +338,10 @@ func testAccCheckGitlabBranchProtectionPersistsInStateCorrectly(n string, pb *gi
258
338
return fmt .Errorf ("push access level not persisted in state correctly" )
259
339
}
260
340
341
+ if rs .Primary .Attributes ["allow_force_push" ] != strconv .FormatBool (pb .AllowForcePush ) {
342
+ return fmt .Errorf ("allow_force_push not persisted in state correctly" )
343
+ }
344
+
261
345
if rs .Primary .Attributes ["code_owner_approval_required" ] != strconv .FormatBool (pb .CodeOwnerApprovalRequired ) {
262
346
return fmt .Errorf ("code_owner_approval_required not persisted in state correctly" )
263
347
}
@@ -301,6 +385,7 @@ type testAccGitlabBranchProtectionExpectedAttributes struct {
301
385
Name string
302
386
PushAccessLevel string
303
387
MergeAccessLevel string
388
+ AllowForcePush bool
304
389
UsersAllowedToPush []string
305
390
UsersAllowedToMerge []string
306
391
GroupsAllowedToPush []string
@@ -336,6 +421,10 @@ func testAccCheckGitlabBranchProtectionAttributes(pb *gitlab.ProtectedBranch, wa
336
421
return fmt .Errorf ("got Merge access level %v; want %v" , mergeAccessLevel , accessLevelNameToValue [want .MergeAccessLevel ])
337
422
}
338
423
424
+ if pb .AllowForcePush != want .AllowForcePush {
425
+ return fmt .Errorf ("got allow_force_push %v; want %v" , pb .AllowForcePush , want .AllowForcePush )
426
+ }
427
+
339
428
remainingWantedUserIDsAllowedToPush := map [int ]struct {}{}
340
429
for _ , v := range want .UsersAllowedToPush {
341
430
users , _ , err := testGitlabClient .Users .ListUsers (& gitlab.ListUsersOptions {
@@ -448,6 +537,27 @@ resource "gitlab_branch_protection" "branch_protect" {
448
537
` , rInt )
449
538
}
450
539
540
+ func testAccGitlabBranchProtectionUpdateConfigAllowForcePushTrue (rInt int ) string {
541
+ return fmt .Sprintf (`
542
+ resource "gitlab_project" "foo" {
543
+ name = "foo-%[1]d"
544
+ description = "Terraform acceptance tests"
545
+
546
+ # So that acceptance tests can be run in a gitlab organization
547
+ # with no billing
548
+ visibility_level = "public"
549
+ }
550
+
551
+ resource "gitlab_branch_protection" "branch_protect" {
552
+ project = gitlab_project.foo.id
553
+ branch = "BranchProtect-%[1]d"
554
+ push_access_level = "developer"
555
+ merge_access_level = "developer"
556
+ allow_force_push = true
557
+ }
558
+ ` , rInt )
559
+ }
560
+
451
561
func testAccGitlabBranchProtectionUpdateConfigCodeOwnerTrue (rInt int ) string {
452
562
return fmt .Sprintf (`
453
563
resource "gitlab_project" "foo" {
0 commit comments