@@ -29,11 +29,12 @@ func TestAccGitLabProjectApprovalRule_basic(t *testing.T) {
29
29
Check : resource .ComposeTestCheckFunc (
30
30
testAccCheckGitlabProjectApprovalRuleExists ("gitlab_project_approval_rule.foo" , & projectApprovalRule ),
31
31
testAccCheckGitlabProjectApprovalRuleAttributes (& projectApprovalRule , & testAccGitlabProjectApprovalRuleExpectedAttributes {
32
- ApproverUsernames : []string {fmt .Sprintf ("foo-user-%d" , randomInt )},
33
- ApprovalsRequired : 3 ,
34
- GroupPaths : []string {fmt .Sprintf ("foo-group-%d" , randomInt )},
35
- Name : fmt .Sprintf ("foo rule %d" , randomInt ),
36
- RandomInt : randomInt ,
32
+ ApproverUsernames : []string {fmt .Sprintf ("foo-user-%d" , randomInt )},
33
+ ApprovalsRequired : 3 ,
34
+ GroupPaths : []string {fmt .Sprintf ("foo-group-%d" , randomInt )},
35
+ ProtectedBranchNames : []string {"master" },
36
+ Name : fmt .Sprintf ("foo rule %d" , randomInt ),
37
+ RandomInt : randomInt ,
37
38
}),
38
39
),
39
40
},
@@ -53,8 +54,9 @@ func TestAccGitLabProjectApprovalRule_basic(t *testing.T) {
53
54
fmt .Sprintf ("bar-group-%d" , randomInt ),
54
55
fmt .Sprintf ("foo-group-%d" , randomInt ),
55
56
},
56
- Name : fmt .Sprintf ("foo rule %d" , randomInt ),
57
- RandomInt : randomInt ,
57
+ ProtectedBranchNames : []string {"master" },
58
+ Name : fmt .Sprintf ("foo rule %d" , randomInt ),
59
+ RandomInt : randomInt ,
58
60
}),
59
61
),
60
62
},
@@ -68,10 +70,11 @@ func TestAccGitLabProjectApprovalRule_basic(t *testing.T) {
68
70
fmt .Sprintf ("bar-user-%d" , randomInt ),
69
71
fmt .Sprintf ("qux-user-%d" , randomInt ),
70
72
},
71
- ApprovalsRequired : 1 ,
72
- GroupPaths : []string {fmt .Sprintf ("bar-group-%d" , randomInt )},
73
- Name : fmt .Sprintf ("foo rule %d" , randomInt ),
74
- RandomInt : randomInt ,
73
+ ApprovalsRequired : 1 ,
74
+ GroupPaths : []string {fmt .Sprintf ("bar-group-%d" , randomInt )},
75
+ ProtectedBranchNames : []string {"master" },
76
+ Name : fmt .Sprintf ("foo rule %d" , randomInt ),
77
+ RandomInt : randomInt ,
75
78
}),
76
79
),
77
80
},
@@ -102,11 +105,12 @@ func TestAccGitLabProjectApprovalRule_import(t *testing.T) {
102
105
}
103
106
104
107
type testAccGitlabProjectApprovalRuleExpectedAttributes struct {
105
- ApprovalsRequired int
106
- ApproverUsernames []string
107
- GroupPaths []string
108
- Name string
109
- RandomInt int
108
+ ApprovalsRequired int
109
+ ApproverUsernames []string
110
+ GroupPaths []string
111
+ ProtectedBranchNames []string
112
+ Name string
113
+ RandomInt int
110
114
}
111
115
112
116
func testAccCheckGitlabProjectApprovalRuleAttributes (projectApprovalRule * gitlab.ProjectApprovalRule , want * testAccGitlabProjectApprovalRuleExpectedAttributes ) resource.TestCheckFunc {
@@ -145,6 +149,16 @@ func testAccCheckGitlabProjectApprovalRuleAttributes(projectApprovalRule *gitlab
145
149
return fmt .Errorf ("got groups %s; want %s" , groupPaths , want .GroupPaths )
146
150
}
147
151
152
+ var protectedBranchNames []string
153
+ for _ , protectedBranch := range projectApprovalRule .ProtectedBranches {
154
+ protectedBranchNames = append (protectedBranchNames , protectedBranch .Name )
155
+ }
156
+ sort .Strings (protectedBranchNames )
157
+
158
+ if ! reflect .DeepEqual (protectedBranchNames , want .ProtectedBranchNames ) {
159
+ return fmt .Errorf ("got protected branches %v; want %v" , protectedBranchNames , want .ProtectedBranchNames )
160
+ }
161
+
148
162
return nil
149
163
}
150
164
}
@@ -157,100 +171,108 @@ func testAccGitLabProjectApprovalRuleConfig(
157
171
) string {
158
172
return fmt .Sprintf (`
159
173
resource "gitlab_user" "foo" {
160
- name = "foo user"
161
- username = "foo-user-%[1]d"
162
- password = "foo12345"
163
- email = "foo-user%[1][email protected] "
164
- is_admin = false
174
+ name = "foo user"
175
+ username = "foo-user-%[1]d"
176
+ password = "foo12345"
177
+ email = "foo-user%[1][email protected] "
178
+ is_admin = false
165
179
projects_limit = 2
166
180
can_create_group = false
167
181
is_external = false
168
182
}
169
183
170
184
resource "gitlab_user" "bar" {
171
- name = "bar user"
172
- username = "bar-user-%[1]d"
173
- password = "bar12345"
174
- email = "bar-user%[1][email protected] "
175
- is_admin = false
185
+ name = "bar user"
186
+ username = "bar-user-%[1]d"
187
+ password = "bar12345"
188
+ email = "bar-user%[1][email protected] "
189
+ is_admin = false
176
190
projects_limit = 2
177
191
can_create_group = false
178
192
is_external = false
179
193
}
180
194
181
195
resource "gitlab_user" "baz" {
182
- name = "baz user"
183
- username = "baz-user-%[1]d"
184
- password = "baz12345"
185
- email = "baz-user%[1][email protected] "
186
- is_admin = false
196
+ name = "baz user"
197
+ username = "baz-user-%[1]d"
198
+ password = "baz12345"
199
+ email = "baz-user%[1][email protected] "
200
+ is_admin = false
187
201
projects_limit = 2
188
202
can_create_group = false
189
203
is_external = false
190
204
}
191
205
192
206
resource "gitlab_user" "qux" {
193
- name = "qux user"
194
- username = "qux-user-%[1]d"
195
- password = "qux12345"
196
- email = "qux-user%[1][email protected] "
197
- is_admin = false
207
+ name = "qux user"
208
+ username = "qux-user-%[1]d"
209
+ password = "qux12345"
210
+ email = "qux-user%[1][email protected] "
211
+ is_admin = false
198
212
projects_limit = 2
199
213
can_create_group = false
200
214
is_external = false
201
215
}
202
216
203
217
resource "gitlab_project" "foo" {
204
- name = "foo project %[1]d"
205
- path = "foo-project-%[1]d"
206
- description = "Terraform acceptance test - Approval Rule"
207
- visibility_level = "public"
218
+ name = "foo project %[1]d"
219
+ path = "foo-project-%[1]d"
220
+ description = "Terraform acceptance test - Approval Rule"
221
+ visibility_level = "public"
222
+ }
223
+
224
+ resource "gitlab_branch_protection" "default" {
225
+ project = gitlab_project.foo.id
226
+ branch = gitlab_project.foo.default_branch
227
+ push_access_level = "maintainer"
228
+ merge_access_level = "developer"
208
229
}
209
230
210
231
resource "gitlab_project_membership" "baz" {
211
- project_id = gitlab_project.foo.id
212
- user_id = gitlab_user.baz.id
213
- access_level = "developer"
232
+ project_id = gitlab_project.foo.id
233
+ user_id = gitlab_user.baz.id
234
+ access_level = "developer"
214
235
}
215
236
216
237
resource "gitlab_project_membership" "qux" {
217
- project_id = gitlab_project.foo.id
218
- user_id = gitlab_user.qux.id
219
- access_level = "developer"
238
+ project_id = gitlab_project.foo.id
239
+ user_id = gitlab_user.qux.id
240
+ access_level = "developer"
220
241
}
221
242
222
243
resource "gitlab_group" "foo" {
223
- name = "foo-group %[1]d"
224
- path = "foo-group-%[1]d"
225
- description = "Terraform acceptance tests - Approval Rule"
226
- visibility_level = "public"
244
+ name = "foo-group %[1]d"
245
+ path = "foo-group-%[1]d"
246
+ description = "Terraform acceptance tests - Approval Rule"
247
+ visibility_level = "public"
227
248
}
228
249
229
250
resource "gitlab_group" "bar" {
230
- name = "bar-group %[1]d"
231
- path = "bar-group-%[1]d"
232
- description = "Terraform acceptance tests - Approval Rule"
233
- visibility_level = "public"
251
+ name = "bar-group %[1]d"
252
+ path = "bar-group-%[1]d"
253
+ description = "Terraform acceptance tests - Approval Rule"
254
+ visibility_level = "public"
234
255
}
235
256
236
257
resource "gitlab_group_membership" "foo" {
237
- group_id = gitlab_group.foo.id
238
- user_id = gitlab_user.foo.id
239
- access_level = "developer"
258
+ group_id = gitlab_group.foo.id
259
+ user_id = gitlab_user.foo.id
260
+ access_level = "developer"
240
261
}
241
262
242
263
resource "gitlab_group_membership" "bar" {
243
- group_id = gitlab_group.bar.id
244
- user_id = gitlab_user.bar.id
245
- access_level = "developer"
264
+ group_id = gitlab_group.bar.id
265
+ user_id = gitlab_user.bar.id
266
+ access_level = "developer"
246
267
}
247
268
248
269
resource "gitlab_project_approval_rule" "foo" {
249
- project = gitlab_project.foo.id
250
- name = "foo rule %[1]d"
251
- approvals_required = %d
252
- user_ids = [%s]
253
- group_ids = [%s]
270
+ project = gitlab_project.foo.id
271
+ name = "foo rule %[1]d"
272
+ approvals_required = %d
273
+ user_ids = [%s]
274
+ group_ids = [%s]
275
+ protected_branch_ids = [gitlab_branch_protection.default.id]
254
276
}
255
277
` ,
256
278
randomInt ,
0 commit comments