Skip to content

Commit 42a38e4

Browse files
domcyrusMarco Cadetg
andauthored
support default_branch_protection on group (#706)
* support default_branch_protection on group Co-authored-by: Marco Cadetg <[email protected]>
1 parent cfce529 commit 42a38e4

File tree

6 files changed

+116
-74
lines changed

6 files changed

+116
-74
lines changed

docs/data-sources/group.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@ The resource exports the following attributes:
5858

5959
* `runners_token` - The group level registration token to use during runner setup.
6060

61+
* `default_branch_protection` - Whether developers and maintainers can push to the applicable default branch.
62+
6163
[doc]: https://docs.gitlab.com/ee/api/groups.html#details-of-a-group

docs/resources/group.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ The resource exports the following attributes:
8181

8282
* `runners_token` - The group level registration token to use during runner setup.
8383

84+
* `default_branch_protection` - (Optional) Int, defaults to 2.
85+
Whether developers and maintainers can push to the applicable default branch.
86+
0 no protection, 1 partial protection, 2 full protection
87+
https://docs.gitlab.com/ee/api/groups.html#options-for-default_branch_protection
88+
8489
## Import
8590

8691
You can import a group state using `terraform import <resource> <id>`. The

gitlab/data_source_gitlab_group.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ func dataSourceGitlabGroup() *schema.Resource {
6969
Computed: true,
7070
Sensitive: true,
7171
},
72+
"default_branch_protection": {
73+
Type: schema.TypeInt,
74+
Computed: true,
75+
},
7276
},
7377
}
7478
}
@@ -112,6 +116,7 @@ func dataSourceGitlabGroupRead(d *schema.ResourceData, meta interface{}) error {
112116
d.Set("visibility_level", group.Visibility)
113117
d.Set("parent_id", group.ParentID)
114118
d.Set("runners_token", group.RunnersToken)
119+
d.Set("default_branch_protection", group.DefaultBranchProtection)
115120

116121
d.SetId(fmt.Sprintf("%d", group.ID))
117122

gitlab/data_source_gitlab_group_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func testAccDataSourceGitlabGroup(src, n string) resource.TestCheckFunc {
5555
"request_access_enabled",
5656
"visibility_level",
5757
"parent_id",
58+
"default_branch_protection",
5859
}
5960

6061
for _, attribute := range testAttributes {

gitlab/resource_gitlab_group.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ func resourceGitlabGroup() *schema.Resource {
5353
Optional: true,
5454
Default: true,
5555
},
56+
"default_branch_protection": {
57+
Type: schema.TypeInt,
58+
Optional: true,
59+
Default: 2,
60+
ValidateFunc: validation.IntInSlice([]int{0, 1, 2}),
61+
},
5662
"request_access_enabled": {
5763
Type: schema.TypeBool,
5864
Optional: true,
@@ -177,6 +183,10 @@ func resourceGitlabGroupCreate(d *schema.ResourceData, meta interface{}) error {
177183
options.ParentID = gitlab.Int(v.(int))
178184
}
179185

186+
if v, ok := d.GetOk("default_branch_protection"); ok {
187+
options.DefaultBranchProtection = gitlab.Int(v.(int))
188+
}
189+
180190
log.Printf("[DEBUG] create gitlab group %q", *options.Name)
181191

182192
group, _, err := client.Groups.CreateGroup(options)
@@ -228,6 +238,7 @@ func resourceGitlabGroupRead(d *schema.ResourceData, meta interface{}) error {
228238
d.Set("parent_id", group.ParentID)
229239
d.Set("runners_token", group.RunnersToken)
230240
d.Set("share_with_group_lock", group.ShareWithGroupLock)
241+
d.Set("default_branch_protection", group.DefaultBranchProtection)
231242

232243
return nil
233244
}
@@ -295,6 +306,10 @@ func resourceGitlabGroupUpdate(d *schema.ResourceData, meta interface{}) error {
295306
options.ShareWithGroupLock = gitlab.Bool(d.Get("share_with_group_lock").(bool))
296307
}
297308

309+
if d.HasChange("default_branch_protection") {
310+
options.DefaultBranchProtection = gitlab.Int(d.Get("default_branch_protection").(int))
311+
}
312+
298313
log.Printf("[DEBUG] update gitlab group %s", d.Id())
299314

300315
_, _, err := client.Groups.UpdateGroup(d.Id(), options)

gitlab/resource_gitlab_group_test.go

Lines changed: 88 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package gitlab
22

33
import (
44
"fmt"
5+
"net/http"
6+
"testing"
7+
"time"
8+
59
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
610
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
711
"github.com/hashicorp/terraform-plugin-sdk/terraform"
812
"github.com/xanzy/go-gitlab"
9-
"net/http"
10-
"testing"
11-
"time"
1213
)
1314

1415
func TestAccGitlabGroup_basic(t *testing.T) {
@@ -26,14 +27,15 @@ func TestAccGitlabGroup_basic(t *testing.T) {
2627
Check: resource.ComposeTestCheckFunc(
2728
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
2829
testAccCheckGitlabGroupAttributes(&group, &testAccGitlabGroupExpectedAttributes{
29-
Name: fmt.Sprintf("foo-name-%d", rInt),
30-
Path: fmt.Sprintf("foo-path-%d", rInt),
31-
Description: "Terraform acceptance tests",
32-
LFSEnabled: true,
33-
Visibility: "public", // default value
34-
ProjectCreationLevel: "maintainer", // default value
35-
SubGroupCreationLevel: "owner", // default value
36-
TwoFactorGracePeriod: 48, // default value
30+
Name: fmt.Sprintf("foo-name-%d", rInt),
31+
Path: fmt.Sprintf("foo-path-%d", rInt),
32+
Description: "Terraform acceptance tests",
33+
LFSEnabled: true,
34+
Visibility: "public", // default value
35+
ProjectCreationLevel: "maintainer", // default value
36+
SubGroupCreationLevel: "owner", // default value
37+
TwoFactorGracePeriod: 48, // default value
38+
DefaultBranchProtection: 2, // default value
3739
}),
3840
),
3941
},
@@ -43,20 +45,21 @@ func TestAccGitlabGroup_basic(t *testing.T) {
4345
Check: resource.ComposeTestCheckFunc(
4446
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
4547
testAccCheckGitlabGroupAttributes(&group, &testAccGitlabGroupExpectedAttributes{
46-
Name: fmt.Sprintf("bar-name-%d", rInt),
47-
Path: fmt.Sprintf("bar-path-%d", rInt),
48-
Description: "Terraform acceptance tests! Updated description",
49-
LFSEnabled: false,
50-
Visibility: "public", // default value
51-
RequestAccessEnabled: true,
52-
ProjectCreationLevel: "developer",
53-
SubGroupCreationLevel: "maintainer",
54-
RequireTwoFactorAuth: true,
55-
TwoFactorGracePeriod: 56,
56-
AutoDevopsEnabled: true,
57-
EmailsDisabled: true,
58-
MentionsDisabled: true,
59-
ShareWithGroupLock: true,
48+
Name: fmt.Sprintf("bar-name-%d", rInt),
49+
Path: fmt.Sprintf("bar-path-%d", rInt),
50+
Description: "Terraform acceptance tests! Updated description",
51+
LFSEnabled: false,
52+
Visibility: "public", // default value
53+
RequestAccessEnabled: true,
54+
ProjectCreationLevel: "developer",
55+
SubGroupCreationLevel: "maintainer",
56+
RequireTwoFactorAuth: true,
57+
TwoFactorGracePeriod: 56,
58+
AutoDevopsEnabled: true,
59+
EmailsDisabled: true,
60+
MentionsDisabled: true,
61+
ShareWithGroupLock: true,
62+
DefaultBranchProtection: 1,
6063
}),
6164
),
6265
},
@@ -66,14 +69,15 @@ func TestAccGitlabGroup_basic(t *testing.T) {
6669
Check: resource.ComposeTestCheckFunc(
6770
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
6871
testAccCheckGitlabGroupAttributes(&group, &testAccGitlabGroupExpectedAttributes{
69-
Name: fmt.Sprintf("foo-name-%d", rInt),
70-
Path: fmt.Sprintf("foo-path-%d", rInt),
71-
Description: "Terraform acceptance tests",
72-
LFSEnabled: true,
73-
Visibility: "public", // default value
74-
ProjectCreationLevel: "maintainer", // default value
75-
SubGroupCreationLevel: "owner", // default value
76-
TwoFactorGracePeriod: 48, // default value
72+
Name: fmt.Sprintf("foo-name-%d", rInt),
73+
Path: fmt.Sprintf("foo-path-%d", rInt),
74+
Description: "Terraform acceptance tests",
75+
LFSEnabled: true,
76+
Visibility: "public", // default value
77+
ProjectCreationLevel: "maintainer", // default value
78+
SubGroupCreationLevel: "owner", // default value
79+
TwoFactorGracePeriod: 48, // default value
80+
DefaultBranchProtection: 2, // default value
7781
}),
7882
),
7983
},
@@ -120,15 +124,16 @@ func TestAccGitlabGroup_nested(t *testing.T) {
120124
testAccCheckGitlabGroupExists("gitlab_group.foo2", &group2),
121125
testAccCheckGitlabGroupExists("gitlab_group.nested_foo", &nestedGroup),
122126
testAccCheckGitlabGroupAttributes(&nestedGroup, &testAccGitlabGroupExpectedAttributes{
123-
Name: fmt.Sprintf("nfoo-name-%d", rInt),
124-
Path: fmt.Sprintf("nfoo-path-%d", rInt),
125-
Description: "Terraform acceptance tests",
126-
LFSEnabled: true,
127-
Visibility: "public", // default value
128-
ProjectCreationLevel: "maintainer", // default value
129-
SubGroupCreationLevel: "owner", // default value
130-
TwoFactorGracePeriod: 48, // default value
131-
Parent: &group,
127+
Name: fmt.Sprintf("nfoo-name-%d", rInt),
128+
Path: fmt.Sprintf("nfoo-path-%d", rInt),
129+
Description: "Terraform acceptance tests",
130+
LFSEnabled: true,
131+
Visibility: "public", // default value
132+
ProjectCreationLevel: "maintainer", // default value
133+
SubGroupCreationLevel: "owner", // default value
134+
TwoFactorGracePeriod: 48, // default value
135+
DefaultBranchProtection: 2, // default value
136+
Parent: &group,
132137
}),
133138
),
134139
},
@@ -139,15 +144,16 @@ func TestAccGitlabGroup_nested(t *testing.T) {
139144
testAccCheckGitlabGroupExists("gitlab_group.foo2", &group2),
140145
testAccCheckGitlabGroupExists("gitlab_group.nested_foo", &nestedGroup),
141146
testAccCheckGitlabGroupAttributes(&nestedGroup, &testAccGitlabGroupExpectedAttributes{
142-
Name: fmt.Sprintf("nfoo-name-%d", rInt),
143-
Path: fmt.Sprintf("nfoo-path-%d", rInt),
144-
Description: "Terraform acceptance tests - new parent",
145-
LFSEnabled: true,
146-
Visibility: "public", // default value
147-
ProjectCreationLevel: "maintainer", // default value
148-
SubGroupCreationLevel: "owner", // default value
149-
TwoFactorGracePeriod: 48, // default value
150-
Parent: &group2,
147+
Name: fmt.Sprintf("nfoo-name-%d", rInt),
148+
Path: fmt.Sprintf("nfoo-path-%d", rInt),
149+
Description: "Terraform acceptance tests - new parent",
150+
LFSEnabled: true,
151+
Visibility: "public", // default value
152+
ProjectCreationLevel: "maintainer", // default value
153+
SubGroupCreationLevel: "owner", // default value
154+
TwoFactorGracePeriod: 48, // default value
155+
DefaultBranchProtection: 2, // default value
156+
Parent: &group2,
151157
}),
152158
),
153159
},
@@ -158,14 +164,15 @@ func TestAccGitlabGroup_nested(t *testing.T) {
158164
testAccCheckGitlabGroupExists("gitlab_group.foo2", &group2),
159165
testAccCheckGitlabGroupExists("gitlab_group.nested_foo", &nestedGroup),
160166
testAccCheckGitlabGroupAttributes(&nestedGroup, &testAccGitlabGroupExpectedAttributes{
161-
Name: fmt.Sprintf("nfoo-name-%d", rInt),
162-
Path: fmt.Sprintf("nfoo-path-%d", rInt),
163-
Description: "Terraform acceptance tests - updated",
164-
LFSEnabled: true,
165-
Visibility: "public", // default value
166-
ProjectCreationLevel: "maintainer", // default value
167-
SubGroupCreationLevel: "owner", // default value
168-
TwoFactorGracePeriod: 48, // default value
167+
Name: fmt.Sprintf("nfoo-name-%d", rInt),
168+
Path: fmt.Sprintf("nfoo-path-%d", rInt),
169+
Description: "Terraform acceptance tests - updated",
170+
LFSEnabled: true,
171+
Visibility: "public", // default value
172+
ProjectCreationLevel: "maintainer", // default value
173+
SubGroupCreationLevel: "owner", // default value
174+
TwoFactorGracePeriod: 48, // default value
175+
DefaultBranchProtection: 2, // default value
169176
}),
170177
),
171178
},
@@ -185,6 +192,7 @@ func TestAccGitlabGroup_nested(t *testing.T) {
185192
// ProjectCreationLevel: "maintainer", // default value
186193
// SubGroupCreationLevel: "owner", // default value
187194
// TwoFactorGracePeriod: 48, // default value
195+
// DefaultBranchProtection: 2, // default value
188196
// Parent: &group,
189197
// }),
190198
// ),
@@ -263,21 +271,22 @@ func testAccCheckGitlabGroupExists(n string, group *gitlab.Group) resource.TestC
263271
}
264272

265273
type testAccGitlabGroupExpectedAttributes struct {
266-
Name string
267-
Path string
268-
Description string
269-
Parent *gitlab.Group
270-
LFSEnabled bool
271-
RequestAccessEnabled bool
272-
Visibility gitlab.VisibilityValue
273-
ShareWithGroupLock bool
274-
AutoDevopsEnabled bool
275-
EmailsDisabled bool
276-
MentionsDisabled bool
277-
ProjectCreationLevel gitlab.ProjectCreationLevelValue
278-
SubGroupCreationLevel gitlab.SubGroupCreationLevelValue
279-
RequireTwoFactorAuth bool
280-
TwoFactorGracePeriod int
274+
Name string
275+
Path string
276+
Description string
277+
Parent *gitlab.Group
278+
LFSEnabled bool
279+
RequestAccessEnabled bool
280+
Visibility gitlab.VisibilityValue
281+
ShareWithGroupLock bool
282+
AutoDevopsEnabled bool
283+
EmailsDisabled bool
284+
MentionsDisabled bool
285+
ProjectCreationLevel gitlab.ProjectCreationLevelValue
286+
SubGroupCreationLevel gitlab.SubGroupCreationLevelValue
287+
RequireTwoFactorAuth bool
288+
TwoFactorGracePeriod int
289+
DefaultBranchProtection int
281290
}
282291

283292
func testAccCheckGitlabGroupAttributes(group *gitlab.Group, want *testAccGitlabGroupExpectedAttributes) resource.TestCheckFunc {
@@ -338,6 +347,10 @@ func testAccCheckGitlabGroupAttributes(group *gitlab.Group, want *testAccGitlabG
338347
return fmt.Errorf("got share_with_group_lock %t; want %t", group.ShareWithGroupLock, want.ShareWithGroupLock)
339348
}
340349

350+
if group.DefaultBranchProtection != want.DefaultBranchProtection {
351+
return fmt.Errorf("got default_branch_protection %d; want %d", group.DefaultBranchProtection, want.DefaultBranchProtection)
352+
}
353+
341354
if want.Parent != nil {
342355
if group.ParentID != want.Parent.ID {
343356
return fmt.Errorf("got parent_id %d; want %d", group.ParentID, want.Parent.ID)
@@ -406,6 +419,7 @@ resource "gitlab_group" "foo" {
406419
emails_disabled = true
407420
mentions_disabled = true
408421
share_with_group_lock = true
422+
default_branch_protection = 1
409423
410424
# So that acceptance tests can be run in a gitlab organization
411425
# with no billing

0 commit comments

Comments
 (0)