Skip to content

Commit 89de4bd

Browse files
committed
New branch_protection_id attribute for gitlab_branch_protection
1 parent 607fd13 commit 89de4bd

4 files changed

+30
-11
lines changed

docs/resources/branch_protection.md

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ An `allowed_to_push` or `allowed_to_merge` block supports the following argument
5656

5757
The following attributes are exported:
5858

59+
* `branch_protection_id` - The ID of the branch protection (not the branch name).
60+
5961
* The `allowed_to_push` and `allowed_to_merge` blocks export the `access_level_description` field, which contains a textual description of the access level, user or group allowed to perform the relevant action.
6062

6163
## Import

docs/resources/project_approval_rule.md

+15-11
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,37 @@ This resource allows you to create and manage multiple approval rules for your G
44
projects. For further information on approval rules, consult the [gitlab
55
documentation](https://docs.gitlab.com/ee/api/merge_request_approvals.html#project-level-mr-approvals).
66

7-
-> This feature requires a GitLab Starter account or above.
7+
-> This feature requires GitLab Premium.
88

99
## Example Usage
1010

1111
```hcl
1212
resource "gitlab_project_approval_rule" "example-one" {
1313
project = 5
14-
name = "Example Rule 1"
14+
name = "Example Rule"
1515
approvals_required = 3
1616
user_ids = [50, 500]
1717
group_ids = [51]
1818
}
19+
```
20+
21+
### With Protected Branch IDs
1922

23+
```hcl
2024
resource "gitlab_branch_protection" "example" {
2125
project = 5
22-
branch = "main"
23-
push_access_level = "developer"
26+
branch = "release/*"
27+
push_access_level = "maintainer"
2428
merge_access_level = "developer"
2529
}
2630
27-
resource "gitlab_project_approval_rule" "example-two" {
31+
resource "gitlab_project_approval_rule" "example" {
2832
project = 5
29-
name = "Example Rule 2"
30-
approvals_required = 1
31-
user_ids = []
32-
group_ids = [52]
33-
protected_branch_ids = [gitlab_branch_protection.example.id]
33+
name = "Example Rule"
34+
approvals_required = 3
35+
user_ids = [50, 500]
36+
group_ids = [51]
37+
protected_branch_ids = [gitlab_branch_protection.example.branch_protection_id]
3438
}
3539
```
3640

@@ -48,7 +52,7 @@ The following arguments are supported:
4852

4953
* `group_ids` - (Optional) A list of group IDs whose members can approve of the merge request.
5054

51-
* `protected_branch_ids` - (Optional) A list of protected branch IDs for which the rule applies.
55+
* `protected_branch_ids` - (Optional) A list of protected branch IDs (not branch names) for which the rule applies.
5256

5357
## Import
5458

gitlab/resource_gitlab_branch_protection.go

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ func resourceGitlabBranchProtection() *schema.Resource {
7676
Optional: true,
7777
Default: false,
7878
},
79+
"branch_protection_id": {
80+
Type: schema.TypeInt,
81+
Computed: true,
82+
},
7983
},
8084
}
8185
}
@@ -172,6 +176,8 @@ func resourceGitlabBranchProtectionRead(d *schema.ResourceData, meta interface{}
172176
return fmt.Errorf("error setting code_owner_approval_required: %v", err)
173177
}
174178

179+
d.Set("branch_protection_id", pb.ID)
180+
175181
d.SetId(buildTwoPartID(&project, &pb.Name))
176182

177183
return nil

gitlab/resource_gitlab_branch_protection_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func TestAccGitlabBranchProtection_basic(t *testing.T) {
2828
Check: resource.ComposeTestCheckFunc(
2929
testAccCheckGitlabBranchProtectionExists("gitlab_branch_protection.branch_protect", &pb),
3030
testAccCheckGitlabBranchProtectionPersistsInStateCorrectly("gitlab_branch_protection.branch_protect", &pb),
31+
testAccCheckGitlabBranchProtectionComputedAttributes("gitlab_branch_protection.branch_protect", &pb),
3132
testAccCheckGitlabBranchProtectionAttributes(&pb, &testAccGitlabBranchProtectionExpectedAttributes{
3233
Name: fmt.Sprintf("BranchProtect-%d", rInt),
3334
PushAccessLevel: accessLevel[gitlab.DeveloperPermissions],
@@ -292,6 +293,12 @@ func testAccCheckGitlabBranchProtectionExists(n string, pb *gitlab.ProtectedBran
292293
}
293294
}
294295

296+
func testAccCheckGitlabBranchProtectionComputedAttributes(n string, pb *gitlab.ProtectedBranch) resource.TestCheckFunc {
297+
return func(s *terraform.State) error {
298+
return resource.TestCheckResourceAttr(n, "branch_protection_id", strconv.Itoa(pb.ID))(s)
299+
}
300+
}
301+
295302
type testAccGitlabBranchProtectionExpectedAttributes struct {
296303
Name string
297304
PushAccessLevel string

0 commit comments

Comments
 (0)