@@ -2,6 +2,7 @@ package gitlab
2
2
3
3
import (
4
4
"fmt"
5
+ "strconv"
5
6
"testing"
6
7
7
8
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
@@ -59,6 +60,66 @@ func TestAccGitlabBranchProtection_basic(t *testing.T) {
59
60
}),
60
61
),
61
62
},
63
+ // Update the the Branch Protection code owner approval setting
64
+ {
65
+ SkipFunc : isRunningInCE ,
66
+ Config : testAccGitlabBranchProtectionUpdateConfigCodeOwnerTrue (rInt ),
67
+ Check : resource .ComposeTestCheckFunc (
68
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.BranchProtect" , & pb ),
69
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.BranchProtect" , & pb ),
70
+ testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
71
+ Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
72
+ PushAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
73
+ MergeAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
74
+ CodeOwnerApprovalRequired : true ,
75
+ }),
76
+ ),
77
+ },
78
+ // Update the Branch Protection to get back to initial settings.
79
+ // Since code_owner_approval_required is an optional setting, it does not revert to its original setting.
80
+ // This test ensures that the addition of this optional value is backward compatible and does not overwrite existing settings in GitLab when it is unset.
81
+ {
82
+ SkipFunc : isRunningInCE ,
83
+ Config : testAccGitlabBranchProtectionConfig (rInt ),
84
+ Check : resource .ComposeTestCheckFunc (
85
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.BranchProtect" , & pb ),
86
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.BranchProtect" , & pb ),
87
+ testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
88
+ Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
89
+ PushAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
90
+ MergeAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
91
+ CodeOwnerApprovalRequired : true ,
92
+ }),
93
+ ),
94
+ },
95
+ // Update the the Branch Protection to explicitly disable code owner approval, to truly reset state back to initial settings.
96
+ {
97
+ SkipFunc : isRunningInCE ,
98
+ Config : testAccGitlabBranchProtectionUpdateConfigCodeOwnerFalse (rInt ),
99
+ Check : resource .ComposeTestCheckFunc (
100
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.BranchProtect" , & pb ),
101
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.BranchProtect" , & pb ),
102
+ testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
103
+ Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
104
+ PushAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
105
+ MergeAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
106
+ }),
107
+ ),
108
+ },
109
+ // Update the Branch Protection to get back to initial settings.
110
+ {
111
+ SkipFunc : isRunningInCE ,
112
+ Config : testAccGitlabBranchProtectionConfig (rInt ),
113
+ Check : resource .ComposeTestCheckFunc (
114
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.BranchProtect" , & pb ),
115
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.BranchProtect" , & pb ),
116
+ testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
117
+ Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
118
+ PushAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
119
+ MergeAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
120
+ }),
121
+ ),
122
+ },
62
123
},
63
124
})
64
125
}
@@ -78,6 +139,10 @@ func testAccCheckGitlabBranchProtectionPersistsInStateCorrectly(n string, pb *gi
78
139
return fmt .Errorf ("push access level not persisted in state correctly" )
79
140
}
80
141
142
+ if rs .Primary .Attributes ["code_owner_approval_required" ] != strconv .FormatBool (pb .CodeOwnerApprovalRequired ) {
143
+ return fmt .Errorf ("code_owner_approval_required not persisted in state correctly" )
144
+ }
145
+
81
146
return nil
82
147
}
83
148
}
@@ -110,9 +175,10 @@ func testAccCheckGitlabBranchProtectionExists(n string, pb *gitlab.ProtectedBran
110
175
}
111
176
112
177
type testAccGitlabBranchProtectionExpectedAttributes struct {
113
- Name string
114
- PushAccessLevel string
115
- MergeAccessLevel string
178
+ Name string
179
+ PushAccessLevel string
180
+ MergeAccessLevel string
181
+ CodeOwnerApprovalRequired bool
116
182
}
117
183
118
184
func testAccCheckGitlabBranchProtectionAttributes (pb * gitlab.ProtectedBranch , want * testAccGitlabBranchProtectionExpectedAttributes ) resource.TestCheckFunc {
@@ -129,6 +195,10 @@ func testAccCheckGitlabBranchProtectionAttributes(pb *gitlab.ProtectedBranch, wa
129
195
return fmt .Errorf ("got Merge access levels %q; want %q" , pb .MergeAccessLevels [0 ].AccessLevel , accessLevelID [want .MergeAccessLevel ])
130
196
}
131
197
198
+ if pb .CodeOwnerApprovalRequired != want .CodeOwnerApprovalRequired {
199
+ return fmt .Errorf ("got code_owner_approval_required %v; want %v" , pb .CodeOwnerApprovalRequired , want .CodeOwnerApprovalRequired )
200
+ }
201
+
132
202
return nil
133
203
}
134
204
}
@@ -196,3 +266,45 @@ resource "gitlab_branch_protection" "BranchProtect" {
196
266
}
197
267
` , rInt , rInt )
198
268
}
269
+
270
+ func testAccGitlabBranchProtectionUpdateConfigCodeOwnerTrue (rInt int ) string {
271
+ return fmt .Sprintf (`
272
+ resource "gitlab_project" "foo" {
273
+ name = "foo-%d"
274
+ description = "Terraform acceptance tests"
275
+
276
+ # So that acceptance tests can be run in a gitlab organization
277
+ # with no billing
278
+ visibility_level = "public"
279
+ }
280
+
281
+ resource "gitlab_branch_protection" "BranchProtect" {
282
+ project = "${gitlab_project.foo.id}"
283
+ branch = "BranchProtect-%d"
284
+ push_access_level = "developer"
285
+ merge_access_level = "developer"
286
+ code_owner_approval_required = true
287
+ }
288
+ ` , rInt , rInt )
289
+ }
290
+
291
+ func testAccGitlabBranchProtectionUpdateConfigCodeOwnerFalse (rInt int ) string {
292
+ return fmt .Sprintf (`
293
+ resource "gitlab_project" "foo" {
294
+ name = "foo-%d"
295
+ description = "Terraform acceptance tests"
296
+
297
+ # So that acceptance tests can be run in a gitlab organization
298
+ # with no billing
299
+ visibility_level = "public"
300
+ }
301
+
302
+ resource "gitlab_branch_protection" "BranchProtect" {
303
+ project = "${gitlab_project.foo.id}"
304
+ branch = "BranchProtect-%d"
305
+ push_access_level = "developer"
306
+ merge_access_level = "developer"
307
+ code_owner_approval_required = false
308
+ }
309
+ ` , rInt , rInt )
310
+ }
0 commit comments