Skip to content

Commit 3c3f596

Browse files
committed
Handle error when using code_owner_approval_required while creating a gitlab_branch_protection in GitLab CE
1 parent 7f9d2db commit 3c3f596

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

gitlab/resource_gitlab_branch_protection.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gitlab
22

33
import (
4+
"errors"
45
"fmt"
56
"log"
67

@@ -84,7 +85,17 @@ func resourceGitlabBranchProtectionCreate(d *schema.ResourceData, meta interface
8485

8586
d.SetId(buildTwoPartID(&project, &bp.Name))
8687

87-
return resourceGitlabBranchProtectionRead(d, meta)
88+
if err := resourceGitlabBranchProtectionRead(d, meta); err != nil {
89+
return err
90+
}
91+
92+
// If the GitLab tier does not support the code owner approval feature, the resulting plan will be inconsistent.
93+
// We return an error because otherwise Terraform would report this inconsistency as a "bug in the provider" to the user.
94+
if codeOwnerApprovalRequired && !d.Get("code_owner_approval_required").(bool) {
95+
return errors.New("feature unavailable: code owner approvals")
96+
}
97+
98+
return nil
8899
}
89100

90101
func resourceGitlabBranchProtectionRead(d *schema.ResourceData, meta interface{}) error {

gitlab/resource_gitlab_branch_protection_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,71 @@ func TestAccGitlabBranchProtection_basic(t *testing.T) {
8181
SkipFunc: isRunningInEE,
8282
Config: testAccGitlabBranchProtectionUpdateConfigCodeOwnerTrue(rInt),
8383
ExpectError: regexp.MustCompile("feature unavailable: code owner approvals"),
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+
}),
92+
),
93+
},
94+
// Update the Branch Protection to get back to initial settings
95+
{
96+
Config: testAccGitlabBranchProtectionConfig(rInt),
97+
Check: resource.ComposeTestCheckFunc(
98+
testAccCheckGitlabBranchProtectionExists("gitlab_branch_protection.BranchProtect", &pb),
99+
testAccCheckGitlabBranchProtectionPersistsInStateCorrectly("gitlab_branch_protection.BranchProtect", &pb),
100+
testAccCheckGitlabBranchProtectionAttributes(&pb, &testAccGitlabBranchProtectionExpectedAttributes{
101+
Name: fmt.Sprintf("BranchProtect-%d", rInt),
102+
PushAccessLevel: accessLevel[gitlab.DeveloperPermissions],
103+
MergeAccessLevel: accessLevel[gitlab.DeveloperPermissions],
104+
}),
105+
),
106+
},
107+
},
108+
})
109+
}
110+
111+
func TestAccGitlabBranchProtection_createWithCodeOwnerApproval(t *testing.T) {
112+
var pb gitlab.ProtectedBranch
113+
rInt := acctest.RandInt()
114+
115+
resource.Test(t, resource.TestCase{
116+
PreCheck: func() { testAccPreCheck(t) },
117+
Providers: testAccProviders,
118+
CheckDestroy: testAccCheckGitlabBranchProtectionDestroy,
119+
Steps: []resource.TestStep{
120+
// Create a project and Branch Protection with code owner approval enabled
121+
{
122+
SkipFunc: isRunningInCE,
123+
Config: testAccGitlabBranchProtectionUpdateConfigCodeOwnerTrue(rInt),
124+
Check: resource.ComposeTestCheckFunc(
125+
testAccCheckGitlabBranchProtectionExists("gitlab_branch_protection.BranchProtect", &pb),
126+
testAccCheckGitlabBranchProtectionPersistsInStateCorrectly("gitlab_branch_protection.BranchProtect", &pb),
127+
testAccCheckGitlabBranchProtectionAttributes(&pb, &testAccGitlabBranchProtectionExpectedAttributes{
128+
Name: fmt.Sprintf("BranchProtect-%d", rInt),
129+
PushAccessLevel: accessLevel[gitlab.DeveloperPermissions],
130+
MergeAccessLevel: accessLevel[gitlab.DeveloperPermissions],
131+
CodeOwnerApprovalRequired: true,
132+
}),
133+
),
134+
},
135+
// Attempting to update code owner approval setting on CE should fail safely and with an informative error message
136+
{
137+
SkipFunc: isRunningInEE,
138+
Config: testAccGitlabBranchProtectionUpdateConfigCodeOwnerTrue(rInt),
139+
ExpectError: regexp.MustCompile("feature unavailable: code owner approvals"),
140+
Check: resource.ComposeTestCheckFunc(
141+
testAccCheckGitlabBranchProtectionExists("gitlab_branch_protection.BranchProtect", &pb),
142+
testAccCheckGitlabBranchProtectionPersistsInStateCorrectly("gitlab_branch_protection.BranchProtect", &pb),
143+
testAccCheckGitlabBranchProtectionAttributes(&pb, &testAccGitlabBranchProtectionExpectedAttributes{
144+
Name: fmt.Sprintf("BranchProtect-%d", rInt),
145+
PushAccessLevel: accessLevel[gitlab.DeveloperPermissions],
146+
MergeAccessLevel: accessLevel[gitlab.DeveloperPermissions],
147+
}),
148+
),
84149
},
85150
// Update the Branch Protection to get back to initial settings
86151
{

0 commit comments

Comments
 (0)