|
4 | 4 | "context"
|
5 | 5 | "fmt"
|
6 | 6 | "log"
|
7 |
| - "net/http" |
8 | 7 |
|
9 | 8 | "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
10 | 9 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
@@ -43,6 +42,14 @@ var _ = registerResource("gitlab_branch_protection", func() *schema.Resource {
|
43 | 42 | return &schema.Resource{
|
44 | 43 | Description: `The ` + "`gitlab_branch_protection`" + ` resource allows to manage the lifecycle of a protected branch of a repository.
|
45 | 44 |
|
| 45 | +~> **Branch Protection Behavior for the default branch** |
| 46 | + Depending on the GitLab instance, group or project setting the default branch of a project is created automatically by GitLab behind the scenes. |
| 47 | + Due to [some](https://github.com/gitlabhq/terraform-provider-gitlab/issues/792) [limitations](https://discuss.hashicorp.com/t/ignore-the-order-of-a-complex-typed-list/42242) in the Terraform Provider SDK and the GitLab API, |
| 48 | + when creating a new project and trying to manage the branch protection setting for its default branch the ` + "`gitlab_branch_protection`" + ` resource will |
| 49 | + automatically take ownership of the default branch without an explicit import by unprotecting and properly protecting it again. |
| 50 | + Having multiple ` + "`gitlab_branch_protection`" + ` resources for the same project and default branch will result in them overriding each other - make sure to only have a single one. |
| 51 | + This behavior might change in the future. |
| 52 | +
|
46 | 53 | ~> The ` + "`allowed_to_push`" + `, ` + "`allowed_to_merge`" + `, ` + "`allowed_to_unprotect`" + `, ` + "`unprotect_access_level`" + ` and ` + "`code_owner_approval_required`" + ` attributes require a GitLab Enterprise instance.
|
47 | 54 |
|
48 | 55 | **Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/protected_branches.html)`,
|
@@ -124,12 +131,25 @@ func resourceGitlabBranchProtectionCreate(ctx context.Context, d *schema.Resourc
|
124 | 131 | log.Printf("[DEBUG] create gitlab branch protection on branch %q for project %s", branch, project)
|
125 | 132 |
|
126 | 133 | if d.IsNewResource() {
|
127 |
| - existing, resp, err := client.ProtectedBranches.GetProtectedBranch(project, branch, gitlab.WithContext(ctx)) |
128 |
| - if err != nil && resp.StatusCode != http.StatusNotFound { |
129 |
| - return diag.Errorf("error looking up protected branch %q on project %q: %v", branch, project, err) |
| 134 | + existing, _, err := client.ProtectedBranches.GetProtectedBranch(project, branch, gitlab.WithContext(ctx)) |
| 135 | + if err == nil { |
| 136 | + projectDetails, _, err := client.Projects.GetProject(project, nil, gitlab.WithContext(ctx)) |
| 137 | + if err != nil { |
| 138 | + return diag.Errorf("Failed to get project details for %q to get the name of the default branch: %v", project, err) |
| 139 | + } |
| 140 | + |
| 141 | + if projectDetails.DefaultBranch == branch { |
| 142 | + log.Printf("[DEBUG] this branch protection is for the default branch %q in project %q, thus we allow configuring it even though this is a new resource! We do this by quickly unprotect it, because it's not editable ...!", branch, project) |
| 143 | + _, err := client.ProtectedBranches.UnprotectRepositoryBranches(project, branch, gitlab.WithContext(ctx)) |
| 144 | + if err != nil { |
| 145 | + return diag.Errorf("Failed to unprotect default branch %q in project %q while trying to 'import' it: %v", branch, project, err) |
| 146 | + } |
| 147 | + } else { |
| 148 | + return diag.Errorf("protected branch %q on project %q already exists: %+v", branch, project, *existing) |
| 149 | + } |
130 | 150 | }
|
131 |
| - if resp.StatusCode != http.StatusNotFound { |
132 |
| - return diag.Errorf("protected branch %q on project %q already exists: %+v", branch, project, *existing) |
| 151 | + if err != nil && !is404(err) { |
| 152 | + return diag.Errorf("error looking up protected branch %q on project %q: %v", branch, project, err) |
133 | 153 | }
|
134 | 154 | }
|
135 | 155 |
|
|
0 commit comments