Skip to content

Commit a4a701a

Browse files
authored
Merge pull request #856 from timofurrer/bugfix/issue-759
Support creating project in group without default branch protection
2 parents 97f1aa7 + 7a6a188 commit a4a701a

File tree

4 files changed

+78
-9
lines changed

4 files changed

+78
-9
lines changed

internal/provider/resource_gitlab_group.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ func resourceGitlabGroupCreate(ctx context.Context, d *schema.ResourceData, meta
208208
options.ParentID = gitlab.Int(v.(int))
209209
}
210210

211-
if v, ok := d.GetOk("default_branch_protection"); ok {
211+
// nolint:staticcheck // SA1019 ignore deprecated GetOkExists
212+
// lintignore: XR001 // TODO: replace with alternative for GetOkExists
213+
if v, ok := d.GetOkExists("default_branch_protection"); ok {
212214
options.DefaultBranchProtection = gitlab.Int(v.(int))
213215
}
214216

internal/provider/resource_gitlab_group_test.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestAccGitlabGroup_basic(t *testing.T) {
4141
},
4242
// Update the group to change the description
4343
{
44-
Config: testAccGitlabGroupUpdateConfig(rInt),
44+
Config: testAccGitlabGroupUpdateConfig(rInt, 1),
4545
Check: resource.ComposeTestCheckFunc(
4646
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
4747
testAccCheckGitlabGroupAttributes(&group, &testAccGitlabGroupExpectedAttributes{
@@ -63,6 +63,30 @@ func TestAccGitlabGroup_basic(t *testing.T) {
6363
}),
6464
),
6565
},
66+
// Update the group to use zero-value `default_branch_protection`
67+
{
68+
Config: testAccGitlabGroupUpdateConfig(rInt, 0),
69+
Check: resource.ComposeTestCheckFunc(
70+
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
71+
testAccCheckGitlabGroupAttributes(&group, &testAccGitlabGroupExpectedAttributes{
72+
Name: fmt.Sprintf("bar-name-%d", rInt),
73+
Path: fmt.Sprintf("bar-path-%d", rInt),
74+
Description: "Terraform acceptance tests! Updated description",
75+
LFSEnabled: false,
76+
Visibility: "public", // default value
77+
RequestAccessEnabled: true,
78+
ProjectCreationLevel: "developer",
79+
SubGroupCreationLevel: "maintainer",
80+
RequireTwoFactorAuth: true,
81+
TwoFactorGracePeriod: 56,
82+
AutoDevopsEnabled: true,
83+
EmailsDisabled: true,
84+
MentionsDisabled: true,
85+
ShareWithGroupLock: true,
86+
DefaultBranchProtection: 0,
87+
}),
88+
),
89+
},
6690
// Update the group to put the name and description back
6791
{
6892
Config: testAccGitlabGroupConfig(rInt),
@@ -398,7 +422,7 @@ resource "gitlab_group" "foo" {
398422
`, rInt, rInt)
399423
}
400424

401-
func testAccGitlabGroupUpdateConfig(rInt int) string {
425+
func testAccGitlabGroupUpdateConfig(rInt int, defaultBranchProtection int) string {
402426
return fmt.Sprintf(`
403427
resource "gitlab_group" "foo" {
404428
name = "bar-name-%d"
@@ -414,13 +438,13 @@ resource "gitlab_group" "foo" {
414438
emails_disabled = true
415439
mentions_disabled = true
416440
share_with_group_lock = true
417-
default_branch_protection = 1
441+
default_branch_protection = %d
418442
419443
# So that acceptance tests can be run in a gitlab organization
420444
# with no billing
421445
visibility_level = "public"
422446
}
423-
`, rInt, rInt)
447+
`, rInt, rInt, defaultBranchProtection)
424448
}
425449

426450
func testAccGitlabNestedGroupConfig(rInt int) string {

internal/provider/resource_gitlab_project.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -607,10 +607,19 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
607607
return diag.Errorf("Failed to protect default branch %q for project %q: %s", newDefaultBranch, d.Id(), err)
608608
}
609609

610-
log.Printf("[DEBUG] unprotect old default branch %q for project %q", oldDefaultBranch, d.Id())
611-
_, err = client.ProtectedBranches.UnprotectRepositoryBranches(project.ID, oldDefaultBranch, gitlab.WithContext(ctx))
612-
if err != nil {
613-
return diag.Errorf("Failed to unprotect undesired default branch %q for project %q: %s", oldDefaultBranch, d.Id(), err)
610+
log.Printf("[DEBUG] check for protection on old default branch %q for project %q", oldDefaultBranch, d.Id())
611+
branch, _, err := client.ProtectedBranches.GetProtectedBranch(project.ID, oldDefaultBranch, gitlab.WithContext(ctx))
612+
if err != nil && !is404(err) {
613+
return diag.Errorf("Failed to check for protected default branch %q for project %q: %v", oldDefaultBranch, d.Id(), err)
614+
}
615+
if branch == nil {
616+
log.Printf("[DEBUG] Default protected branch %q for project %q does not exist", oldDefaultBranch, d.Id())
617+
} else {
618+
log.Printf("[DEBUG] unprotect old default branch %q for project %q", oldDefaultBranch, d.Id())
619+
_, err = client.ProtectedBranches.UnprotectRepositoryBranches(project.ID, oldDefaultBranch, gitlab.WithContext(ctx))
620+
if err != nil {
621+
return diag.Errorf("Failed to unprotect undesired default branch %q for project %q: %v", oldDefaultBranch, d.Id(), err)
622+
}
614623
}
615624

616625
log.Printf("[DEBUG] delete old default branch %q for project %q", oldDefaultBranch, d.Id())

internal/provider/resource_gitlab_project_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,23 @@ member_check = false
413413
})
414414
}
415415

416+
func TestAccGitlabProject_groupWithoutDefaultBranchProtection(t *testing.T) {
417+
var project gitlab.Project
418+
rInt := acctest.RandInt()
419+
420+
resource.Test(t, resource.TestCase{
421+
PreCheck: func() { testAccPreCheck(t) },
422+
ProviderFactories: providerFactories,
423+
CheckDestroy: testAccCheckGitlabProjectDestroy,
424+
Steps: []resource.TestStep{
425+
{
426+
Config: testAccGitlabProjectConfigWithoutDefaultBranchProtection(rInt),
427+
Check: testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
428+
},
429+
},
430+
})
431+
}
432+
416433
func TestAccGitlabProject_IssueMergeRequestTemplates(t *testing.T) {
417434
var project gitlab.Project
418435
rInt := acctest.RandInt()
@@ -1079,6 +1096,23 @@ resource "gitlab_project" "foo" {
10791096
`, rInt, rInt, rInt)
10801097
}
10811098

1099+
func testAccGitlabProjectConfigWithoutDefaultBranchProtection(rInt int) string {
1100+
return fmt.Sprintf(`
1101+
resource "gitlab_group" "foo" {
1102+
name = "foogroup-%d"
1103+
path = "foogroup-%d"
1104+
default_branch_protection = 0
1105+
visibility_level = "public"
1106+
}
1107+
1108+
resource "gitlab_project" "foo" {
1109+
name = "foo-%d"
1110+
description = "Terraform acceptance tests"
1111+
namespace_id = "${gitlab_group.foo.id}"
1112+
}
1113+
`, rInt, rInt, rInt)
1114+
}
1115+
10821116
func testAccGitlabProjectTransferBetweenGroups(rInt int) string {
10831117
return fmt.Sprintf(`
10841118
resource "gitlab_group" "foo" {

0 commit comments

Comments
 (0)