Skip to content

Commit 401790a

Browse files
authored
Add protected_branch_ids to gitlab_project_approval_rule (#542)
Signed-off-by: Sune Keller <[email protected]>
1 parent 92a2c60 commit 401790a

File tree

3 files changed

+143
-79
lines changed

3 files changed

+143
-79
lines changed

docs/resources/project_approval_rule.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@ resource "gitlab_project_approval_rule" "example-one" {
1717
group_ids = [51]
1818
}
1919
20-
resource "gitlab_project_approval_rule" "example-two" {
20+
resource "gitlab_branch_protection" "example" {
2121
project = 5
22-
name = "Example Rule 2"
23-
approvals_required = 1
24-
user_ids = []
25-
group_ids = [52]
22+
branch = "main"
23+
push_access_level = "developer"
24+
merge_access_level = "developer"
25+
}
26+
27+
resource "gitlab_project_approval_rule" "example-two" {
28+
project = 5
29+
name = "Example Rule 2"
30+
approvals_required = 1
31+
user_ids = []
32+
group_ids = [52]
33+
protected_branch_ids = [gitlab_branch_protection.example.id]
2634
}
2735
```
2836

@@ -38,7 +46,9 @@ The following arguments are supported:
3846

3947
* `user_ids` - (Optional) A list of specific User IDs to add to the list of approvers.
4048

41-
* `group_ids` - (Optional) A list of group IDs who's members can approve of the merge request
49+
* `group_ids` - (Optional) A list of group IDs whose members can approve of the merge request.
50+
51+
* `protected_branch_ids` - (Optional) A list of protected branch IDs for which the rule applies.
4252

4353
## Import
4454

gitlab/resource_gitlab_project_approval_rule.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,23 @@ func resourceGitlabProjectApprovalRule() *schema.Resource {
4747
Elem: &schema.Schema{Type: schema.TypeInt},
4848
Set: schema.HashInt,
4949
},
50+
"protected_branch_ids": {
51+
Type: schema.TypeSet,
52+
Optional: true,
53+
Elem: &schema.Schema{Type: schema.TypeInt},
54+
Set: schema.HashInt,
55+
},
5056
},
5157
}
5258
}
5359

5460
func resourceGitlabProjectApprovalRuleCreate(d *schema.ResourceData, meta interface{}) error {
5561
options := gitlab.CreateProjectLevelRuleOptions{
56-
Name: gitlab.String(d.Get("name").(string)),
57-
ApprovalsRequired: gitlab.Int(d.Get("approvals_required").(int)),
58-
UserIDs: expandApproverIds(d.Get("user_ids")),
59-
GroupIDs: expandApproverIds(d.Get("group_ids")),
62+
Name: gitlab.String(d.Get("name").(string)),
63+
ApprovalsRequired: gitlab.Int(d.Get("approvals_required").(int)),
64+
UserIDs: expandApproverIds(d.Get("user_ids")),
65+
GroupIDs: expandApproverIds(d.Get("group_ids")),
66+
ProtectedBranchIDs: expandProtectedBranchIDs(d.Get("protected_branch_ids")),
6067
}
6168

6269
project := d.Get("project").(string)
@@ -106,6 +113,10 @@ func resourceGitlabProjectApprovalRuleRead(d *schema.ResourceData, meta interfac
106113
return err
107114
}
108115

116+
if err := d.Set("protected_branch_ids", flattenProtectedBranchIDs(rule.ProtectedBranches)); err != nil {
117+
return err
118+
}
119+
109120
return nil
110121
}
111122

@@ -121,10 +132,11 @@ func resourceGitlabProjectApprovalRuleUpdate(d *schema.ResourceData, meta interf
121132
}
122133

123134
options := gitlab.UpdateProjectLevelRuleOptions{
124-
Name: gitlab.String(d.Get("name").(string)),
125-
ApprovalsRequired: gitlab.Int(d.Get("approvals_required").(int)),
126-
UserIDs: expandApproverIds(d.Get("user_ids")),
127-
GroupIDs: expandApproverIds(d.Get("group_ids")),
135+
Name: gitlab.String(d.Get("name").(string)),
136+
ApprovalsRequired: gitlab.Int(d.Get("approvals_required").(int)),
137+
UserIDs: expandApproverIds(d.Get("user_ids")),
138+
GroupIDs: expandApproverIds(d.Get("group_ids")),
139+
ProtectedBranchIDs: expandProtectedBranchIDs(d.Get("protected_branch_ids")),
128140
}
129141

130142
log.Printf("[DEBUG] Project %s update gitlab project-level approval rule %s", projectID, *options.Name)
@@ -215,6 +227,16 @@ func flattenApprovalRuleGroupIDs(groups []*gitlab.Group) []int {
215227
return groupIDs
216228
}
217229

230+
func flattenProtectedBranchIDs(protectedBranches []*gitlab.ProtectedBranch) []int {
231+
var protectedBranchIDs []int
232+
233+
for _, protectedBranch := range protectedBranches {
234+
protectedBranchIDs = append(protectedBranchIDs, protectedBranch.ID)
235+
}
236+
237+
return protectedBranchIDs
238+
}
239+
218240
// expandApproverIds Expands an interface into a list of ints to read from state.
219241
func expandApproverIds(ids interface{}) []int {
220242
var approverIDs []int
@@ -225,3 +247,13 @@ func expandApproverIds(ids interface{}) []int {
225247

226248
return approverIDs
227249
}
250+
251+
func expandProtectedBranchIDs(ids interface{}) []int {
252+
var protectedBranchIDs []int
253+
254+
for _, id := range ids.(*schema.Set).List() {
255+
protectedBranchIDs = append(protectedBranchIDs, id.(int))
256+
}
257+
258+
return protectedBranchIDs
259+
}

gitlab/resource_gitlab_project_approval_rule_test.go

Lines changed: 87 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ func TestAccGitLabProjectApprovalRule_basic(t *testing.T) {
2929
Check: resource.ComposeTestCheckFunc(
3030
testAccCheckGitlabProjectApprovalRuleExists("gitlab_project_approval_rule.foo", &projectApprovalRule),
3131
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,
3738
}),
3839
),
3940
},
@@ -53,8 +54,9 @@ func TestAccGitLabProjectApprovalRule_basic(t *testing.T) {
5354
fmt.Sprintf("bar-group-%d", randomInt),
5455
fmt.Sprintf("foo-group-%d", randomInt),
5556
},
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,
5860
}),
5961
),
6062
},
@@ -68,10 +70,11 @@ func TestAccGitLabProjectApprovalRule_basic(t *testing.T) {
6870
fmt.Sprintf("bar-user-%d", randomInt),
6971
fmt.Sprintf("qux-user-%d", randomInt),
7072
},
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,
7578
}),
7679
),
7780
},
@@ -102,11 +105,12 @@ func TestAccGitLabProjectApprovalRule_import(t *testing.T) {
102105
}
103106

104107
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
110114
}
111115

112116
func testAccCheckGitlabProjectApprovalRuleAttributes(projectApprovalRule *gitlab.ProjectApprovalRule, want *testAccGitlabProjectApprovalRuleExpectedAttributes) resource.TestCheckFunc {
@@ -145,6 +149,16 @@ func testAccCheckGitlabProjectApprovalRuleAttributes(projectApprovalRule *gitlab
145149
return fmt.Errorf("got groups %s; want %s", groupPaths, want.GroupPaths)
146150
}
147151

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+
148162
return nil
149163
}
150164
}
@@ -157,100 +171,108 @@ func testAccGitLabProjectApprovalRuleConfig(
157171
) string {
158172
return fmt.Sprintf(`
159173
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
165179
projects_limit = 2
166180
can_create_group = false
167181
is_external = false
168182
}
169183
170184
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
176190
projects_limit = 2
177191
can_create_group = false
178192
is_external = false
179193
}
180194
181195
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
187201
projects_limit = 2
188202
can_create_group = false
189203
is_external = false
190204
}
191205
192206
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
198212
projects_limit = 2
199213
can_create_group = false
200214
is_external = false
201215
}
202216
203217
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"
208229
}
209230
210231
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"
214235
}
215236
216237
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"
220241
}
221242
222243
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"
227248
}
228249
229250
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"
234255
}
235256
236257
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"
240261
}
241262
242263
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"
246267
}
247268
248269
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]
254276
}
255277
`,
256278
randomInt,

0 commit comments

Comments
 (0)