Skip to content

Commit 8d4cf46

Browse files
switchboardOptimofurrer
authored andcommitted
check for protected branch before trying to delete protection
1 parent 295685c commit 8d4cf46

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

internal/provider/resource_gitlab_project.go

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

602-
log.Printf("[DEBUG] unprotect old default branch %q for project %q", oldDefaultBranch, d.Id())
603-
_, err = client.ProtectedBranches.UnprotectRepositoryBranches(project.ID, oldDefaultBranch, gitlab.WithContext(ctx))
604-
if err != nil {
605-
return diag.Errorf("Failed to unprotect undesired default branch %q for project %q: %s", oldDefaultBranch, d.Id(), err)
602+
log.Printf("[DEBUG] check for protection on old default branch %q for project %q", oldDefaultBranch, d.Id())
603+
branch, resp, err := client.ProtectedBranches.GetProtectedBranch(project.ID, oldDefaultBranch, gitlab.WithContext(ctx))
604+
if err != nil && resp.StatusCode != http.StatusNotFound {
605+
return diag.Errorf("Failed to check for protected default branch %q for project %q: %v", oldDefaultBranch, d.Id(), err)
606+
}
607+
if branch == nil {
608+
log.Printf("[DEBUG] Default protected branch %q for project %q does not exist", oldDefaultBranch, d.Id())
609+
} else {
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: %v", oldDefaultBranch, d.Id(), err)
614+
}
606615
}
607616

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

0 commit comments

Comments
 (0)