Skip to content

Commit 68f0d0c

Browse files
authored
Merge pull request #1129 from timofurrer/feature/do-not-execute-default-branch-handling-for-new-releases
resource/gitlab_project: Only execute custom `default_branch` logic for GitLab < 14.10
2 parents 8be3fde + 5dc436c commit 68f0d0c

File tree

2 files changed

+61
-54
lines changed

2 files changed

+61
-54
lines changed

docs/resources/project.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ The `gitlab_project` resource allows to manage the lifecycle of a project.
2020

2121
A project can either be created in a group or user namespace.
2222

23-
-> **Default Branch Protection Workaround** Projects are created with default branch protection.
24-
Since this default branch protection is not currently managed via Terraform, to workaround this limitation,
23+
-> **Default Branch Protection Workaround** Projects are created with default branch protection.
24+
Since this default branch protection is not currently managed via Terraform, to workaround this limitation,
2525
you can remove the default branch protection via the API and create your desired Terraform managed branch protection.
26-
In the `gitlab_project` resource, define a `local-exec` provisioner which invokes
27-
the `/projects/:id/protected_branches/:name` API via curl to delete the branch protection on the default
26+
In the `gitlab_project` resource, define a `local-exec` provisioner which invokes
27+
the `/projects/:id/protected_branches/:name` API via curl to delete the branch protection on the default
2828
branch using a `DELETE` request. Then define the desired branch protection using the `gitlab_branch_protection` resource.
2929

3030
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ce/api/projects.html)

internal/provider/resource_gitlab_project.go

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -645,11 +645,11 @@ var _ = registerResource("gitlab_project", func() *schema.Resource {
645645
646646
A project can either be created in a group or user namespace.
647647
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,
650650
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
653653
branch using a ` + "`DELETE`" + ` request. Then define the desired branch protection using the ` + "`gitlab_branch_protection`" + ` resource.
654654
655655
**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
10181018
}
10191019
}
10201020

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+
}
10281043

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+
}
10371051

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+
}
10451059

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+
}
10531074

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))
10641077
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)
10661079
}
10671080
}
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-
}
10741081
}
10751082

10761083
// If the project is assigned to a group namespace and the group has *default branch protection*

0 commit comments

Comments
 (0)