@@ -599,10 +599,19 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
599
599
return diag .Errorf ("Failed to protect default branch %q for project %q: %s" , newDefaultBranch , d .Id (), err )
600
600
}
601
601
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
+ }
606
615
}
607
616
608
617
log .Printf ("[DEBUG] delete old default branch %q for project %q" , oldDefaultBranch , d .Id ())
0 commit comments