Skip to content

Commit 88bd3f1

Browse files
committed
Fix compatibility issue where an existing code_owner_approval_required setting would be overwritten
1 parent 5583566 commit 88bd3f1

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

gitlab/resource_gitlab_branch_protection.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func resourceGitlabBranchProtection() *schema.Resource {
1616
return &schema.Resource{
1717
Create: resourceGitlabBranchProtectionCreate,
1818
Read: resourceGitlabBranchProtectionRead,
19+
Update: resourceGitlabBranchProtectionUpdate,
1920
Delete: resourceGitlabBranchProtectionDelete,
2021
Schema: map[string]*schema.Schema{
2122
"project": {
@@ -43,7 +44,7 @@ func resourceGitlabBranchProtection() *schema.Resource {
4344
"code_owner_approval_required": {
4445
Type: schema.TypeBool,
4546
Optional: true,
46-
ForceNew: true,
47+
Computed: true,
4748
},
4849
},
4950
}
@@ -112,6 +113,30 @@ func resourceGitlabBranchProtectionRead(d *schema.ResourceData, meta interface{}
112113
return nil
113114
}
114115

116+
func resourceGitlabBranchProtectionUpdate(d *schema.ResourceData, meta interface{}) error {
117+
// NOTE: At the time of writing, the only value that does not force re-creation is code_owner_approval_required,
118+
// so therefore that is the only update that needs to be handled.
119+
120+
client := meta.(*gitlab.Client)
121+
project := d.Get("project").(string)
122+
branch := d.Get("branch").(string)
123+
codeOwnerApprovalRequired := getOptionalBool(d, "code_owner_approval_required")
124+
125+
log.Printf("[DEBUG] update gitlab branch protection for project %s, branch %s", project, branch)
126+
127+
options := &gitlab.RequireCodeOwnerApprovalsOptions{
128+
CodeOwnerApprovalRequired: codeOwnerApprovalRequired,
129+
}
130+
131+
if _, err := client.ProtectedBranches.RequireCodeOwnerApprovals(project, branch, options); err != nil {
132+
return err
133+
}
134+
135+
d.SetId(buildTwoPartID(&project, &branch))
136+
137+
return resourceGitlabBranchProtectionRead(d, meta)
138+
}
139+
115140
func resourceGitlabBranchProtectionDelete(d *schema.ResourceData, meta interface{}) error {
116141
client := meta.(*gitlab.Client)
117142
project := d.Get("project").(string)

0 commit comments

Comments
 (0)