@@ -645,11 +645,11 @@ var _ = registerResource("gitlab_project", func() *schema.Resource {
645
645
646
646
A project can either be created in a group or user namespace.
647
647
648
- -> **Default Branch Protection Workaround** Projects are created with default branch protection.
649
- Since this default branch protection is not currently managed via Terraform, to workaround this limitation,
648
+ -> **Default Branch Protection Workaround** Projects are created with default branch protection.
649
+ Since this default branch protection is not currently managed via Terraform, to workaround this limitation,
650
650
you can remove the default branch protection via the API and create your desired Terraform managed branch protection.
651
- In the ` + "`gitlab_project`" + ` resource, define a ` + "`local-exec`" + ` provisioner which invokes
652
- the ` + "`/projects/:id/protected_branches/:name`" + ` API via curl to delete the branch protection on the default
651
+ In the ` + "`gitlab_project`" + ` resource, define a ` + "`local-exec`" + ` provisioner which invokes
652
+ the ` + "`/projects/:id/protected_branches/:name`" + ` API via curl to delete the branch protection on the default
653
653
branch using a ` + "`DELETE`" + ` request. Then define the desired branch protection using the ` + "`gitlab_branch_protection`" + ` resource.
654
654
655
655
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ce/api/projects.html)` ,
@@ -1018,59 +1018,66 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
1018
1018
}
1019
1019
}
1020
1020
1021
- // default_branch cannot always be set during creation.
1022
- // If the branch does not exist, the update will fail, so we also create it here.
1023
- // See: https://gitlab.com/gitlab-org/gitlab/-/issues/333426
1024
- // This logic may be removed when the above issue is resolved.
1025
- if v , ok := d .GetOk ("default_branch" ); ok && project .DefaultBranch != "" && project .DefaultBranch != v .(string ) {
1026
- oldDefaultBranch := project .DefaultBranch
1027
- newDefaultBranch := v .(string )
1021
+ // see: https://gitlab.com/gitlab-org/gitlab/-/issues/333426
1022
+ noDefaultBranchAPISupport , err := isGitLabVersionLessThan (ctx , client , "14.10" )()
1023
+ if err != nil {
1024
+ return diag .Errorf ("unable to get information if `default_branch` handling is supported in the GitLab instance: %v" , err )
1025
+ }
1026
+
1027
+ if noDefaultBranchAPISupport {
1028
+ // default_branch cannot always be set during creation.
1029
+ // If the branch does not exist, the update will fail, so we also create it here.
1030
+ // This logic may be removed when the above issue is resolved.
1031
+ if v , ok := d .GetOk ("default_branch" ); ok && project .DefaultBranch != "" && project .DefaultBranch != v .(string ) {
1032
+ oldDefaultBranch := project .DefaultBranch
1033
+ newDefaultBranch := v .(string )
1034
+
1035
+ log .Printf ("[DEBUG] create branch %q for project %q" , newDefaultBranch , d .Id ())
1036
+ _ , _ , err := client .Branches .CreateBranch (project .ID , & gitlab.CreateBranchOptions {
1037
+ Branch : gitlab .String (newDefaultBranch ),
1038
+ Ref : gitlab .String (oldDefaultBranch ),
1039
+ }, gitlab .WithContext (ctx ))
1040
+ if err != nil {
1041
+ return diag .Errorf ("Failed to create branch %q for project %q: %s" , newDefaultBranch , d .Id (), err )
1042
+ }
1028
1043
1029
- log .Printf ("[DEBUG] create branch %q for project %q" , newDefaultBranch , d .Id ())
1030
- _ , _ , err := client .Branches .CreateBranch (project .ID , & gitlab.CreateBranchOptions {
1031
- Branch : gitlab .String (newDefaultBranch ),
1032
- Ref : gitlab .String (oldDefaultBranch ),
1033
- }, gitlab .WithContext (ctx ))
1034
- if err != nil {
1035
- return diag .Errorf ("Failed to create branch %q for project %q: %s" , newDefaultBranch , d .Id (), err )
1036
- }
1044
+ log .Printf ("[DEBUG] set new default branch to %q for project %q" , newDefaultBranch , d .Id ())
1045
+ _ , _ , err = client .Projects .EditProject (project .ID , & gitlab.EditProjectOptions {
1046
+ DefaultBranch : gitlab .String (newDefaultBranch ),
1047
+ }, gitlab .WithContext (ctx ))
1048
+ if err != nil {
1049
+ return diag .Errorf ("Failed to set default branch to %q for project %q: %s" , newDefaultBranch , d .Id (), err )
1050
+ }
1037
1051
1038
- log .Printf ("[DEBUG] set new default branch to %q for project %q" , newDefaultBranch , d .Id ())
1039
- _ , _ , err = client .Projects . EditProject (project .ID , & gitlab.EditProjectOptions {
1040
- DefaultBranch : gitlab .String (newDefaultBranch ),
1041
- }, gitlab .WithContext (ctx ))
1042
- if err != nil {
1043
- return diag .Errorf ("Failed to set default branch to %q for project %q: %s" , newDefaultBranch , d .Id (), err )
1044
- }
1052
+ log .Printf ("[DEBUG] protect new default branch %q for project %q" , newDefaultBranch , d .Id ())
1053
+ _ , _ , err = client .ProtectedBranches . ProtectRepositoryBranches (project .ID , & gitlab.ProtectRepositoryBranchesOptions {
1054
+ Name : gitlab .String (newDefaultBranch ),
1055
+ }, gitlab .WithContext (ctx ))
1056
+ if err != nil {
1057
+ return diag .Errorf ("Failed to protect default branch %q for project %q: %s" , newDefaultBranch , d .Id (), err )
1058
+ }
1045
1059
1046
- log .Printf ("[DEBUG] protect new default branch %q for project %q" , newDefaultBranch , d .Id ())
1047
- _ , _ , err = client .ProtectedBranches .ProtectRepositoryBranches (project .ID , & gitlab.ProtectRepositoryBranchesOptions {
1048
- Name : gitlab .String (newDefaultBranch ),
1049
- }, gitlab .WithContext (ctx ))
1050
- if err != nil {
1051
- return diag .Errorf ("Failed to protect default branch %q for project %q: %s" , newDefaultBranch , d .Id (), err )
1052
- }
1060
+ log .Printf ("[DEBUG] check for protection on old default branch %q for project %q" , oldDefaultBranch , d .Id ())
1061
+ branch , _ , err := client .ProtectedBranches .GetProtectedBranch (project .ID , oldDefaultBranch , gitlab .WithContext (ctx ))
1062
+ if err != nil && ! is404 (err ) {
1063
+ return diag .Errorf ("Failed to check for protected default branch %q for project %q: %v" , oldDefaultBranch , d .Id (), err )
1064
+ }
1065
+ if branch == nil {
1066
+ log .Printf ("[DEBUG] Default protected branch %q for project %q does not exist" , oldDefaultBranch , d .Id ())
1067
+ } else {
1068
+ log .Printf ("[DEBUG] unprotect old default branch %q for project %q" , oldDefaultBranch , d .Id ())
1069
+ _ , err = client .ProtectedBranches .UnprotectRepositoryBranches (project .ID , oldDefaultBranch , gitlab .WithContext (ctx ))
1070
+ if err != nil {
1071
+ return diag .Errorf ("Failed to unprotect undesired default branch %q for project %q: %v" , oldDefaultBranch , d .Id (), err )
1072
+ }
1073
+ }
1053
1074
1054
- log .Printf ("[DEBUG] check for protection on old default branch %q for project %q" , oldDefaultBranch , d .Id ())
1055
- branch , _ , err := client .ProtectedBranches .GetProtectedBranch (project .ID , oldDefaultBranch , gitlab .WithContext (ctx ))
1056
- if err != nil && ! is404 (err ) {
1057
- return diag .Errorf ("Failed to check for protected default branch %q for project %q: %v" , oldDefaultBranch , d .Id (), err )
1058
- }
1059
- if branch == nil {
1060
- log .Printf ("[DEBUG] Default protected branch %q for project %q does not exist" , oldDefaultBranch , d .Id ())
1061
- } else {
1062
- log .Printf ("[DEBUG] unprotect old default branch %q for project %q" , oldDefaultBranch , d .Id ())
1063
- _ , err = client .ProtectedBranches .UnprotectRepositoryBranches (project .ID , oldDefaultBranch , gitlab .WithContext (ctx ))
1075
+ log .Printf ("[DEBUG] delete old default branch %q for project %q" , oldDefaultBranch , d .Id ())
1076
+ _ , err = client .Branches .DeleteBranch (project .ID , oldDefaultBranch , gitlab .WithContext (ctx ))
1064
1077
if err != nil {
1065
- return diag .Errorf ("Failed to unprotect undesired default branch %q for project %q: %v " , oldDefaultBranch , d .Id (), err )
1078
+ return diag .Errorf ("Failed to clean up undesired default branch %q for project %q: %s " , oldDefaultBranch , d .Id (), err )
1066
1079
}
1067
1080
}
1068
-
1069
- log .Printf ("[DEBUG] delete old default branch %q for project %q" , oldDefaultBranch , d .Id ())
1070
- _ , err = client .Branches .DeleteBranch (project .ID , oldDefaultBranch , gitlab .WithContext (ctx ))
1071
- if err != nil {
1072
- return diag .Errorf ("Failed to clean up undesired default branch %q for project %q: %s" , oldDefaultBranch , d .Id (), err )
1073
- }
1074
1081
}
1075
1082
1076
1083
// If the project is assigned to a group namespace and the group has *default branch protection*
0 commit comments