Skip to content

Commit febe745

Browse files
authored
Merge pull request #551 from sirlatrom/fix-549
Add data sources gitlab_project_protected_branch(es)
2 parents 39b0b6d + 9cc7dd4 commit febe745

7 files changed

+465
-6
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# gitlab\_project\_protected\_branch
2+
3+
Provides details about a specific protected branch in a given project.
4+
5+
## Example Usage
6+
7+
```hcl
8+
data "gitlab_project_protected_branch" "example" {
9+
project_id = 30
10+
name = "main"
11+
}
12+
```
13+
14+
```hcl
15+
data "gitlab_project_protected_branch" "example" {
16+
project_id = "foo/bar/baz"
17+
name = "main"
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
The following arguments are supported:
24+
25+
* `project_id` - (Required) The integer or path with namespace that uniquely identifies the project.
26+
27+
* `name` - (Required) The name of the protected branch.
28+
29+
## Attributes Reference
30+
31+
The following attributes are exported:
32+
33+
* `push_access_levels`, `merge_access_levels`, `unprotect_access_levels` - Each block contains a list of which access levels, users or groups are allowed to perform the respective actions (documented below).
34+
35+
* `code_owner_approval_required` - Reject code pushes that change files listed in the CODEOWNERS file.
36+
37+
## Nested Blocks
38+
39+
### `push_access_levels`, `merge_access_levels`, `unprotect_access_levels`
40+
41+
#### Attributes
42+
43+
* `access_level` - The access level allowed to perform the respective action (shows as 40 - "maintainer" if `user_id` or `group_id` are present).
44+
45+
* `access_level_description` - A description of the allowed access level(s), or the name of the user or group if `user_id` or `group_id` are present.
46+
47+
* `user_id` - If present, indicates that the user is allowed to perform the respective action.
48+
49+
* `group_id` - If present, indicates that the group is allowed to perform the respective action.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# gitlab\_project\_protected\_branches
2+
3+
Provides details about all protected branches in a given project.
4+
5+
## Example Usage
6+
7+
```hcl
8+
data "gitlab_project_protected_branches" "example" {
9+
project_id = 30
10+
}
11+
```
12+
13+
```hcl
14+
data "gitlab_project_protected_branches" "example" {
15+
project_id = "foo/bar/baz"
16+
}
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `project_id` - (Required) The integer or path with namespace that uniquely identifies the project.
24+
25+
## Attributes Reference
26+
27+
The following attributes are exported:
28+
29+
* `protected_branches` - A list of protected branches, as defined below.
30+
31+
## Nested Blocks
32+
33+
### `protected_branches`
34+
35+
* `id` - The ID of the protected branch.
36+
37+
* `name` - The name of the protected branch.
38+
39+
* `push_access_levels`, `merge_access_levels`, `unprotect_access_levels` - Each block contains a list of which access levels, users or groups are allowed to perform the respective actions (documented below).
40+
41+
* `code_owner_approval_required` - Reject code pushes that change files listed in the CODEOWNERS file.
42+
43+
### `push_access_levels`, `merge_access_levels`, `unprotect_access_levels`
44+
45+
#### Attributes
46+
47+
* `access_level` - The access level allowed to perform the respective action (shows as 40 - "maintainer" if `user_id` or `group_id` are present).
48+
49+
* `access_level_description` - A description of the allowed access level(s), or the name of the user or group if `user_id` or `group_id` are present.
50+
51+
* `user_id` - If present, indicates that the user is allowed to perform the respective action.
52+
53+
* `group_id` - If present, indicates that the group is allowed to perform the respective action.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
9+
"github.com/xanzy/go-gitlab"
10+
)
11+
12+
func dataSourceGitlabProjectProtectedBranch() *schema.Resource {
13+
return &schema.Resource{
14+
Read: dataSourceGitlabProjectProtectedBranchRead,
15+
Schema: map[string]*schema.Schema{
16+
"project_id": {
17+
Type: schema.TypeString,
18+
Description: "ID or URL encoded name of project",
19+
Required: true,
20+
ValidateFunc: validation.StringIsNotEmpty,
21+
},
22+
"name": {
23+
Type: schema.TypeString,
24+
Description: "Name of the protected branch",
25+
Required: true,
26+
ValidateFunc: validation.StringIsNotEmpty,
27+
},
28+
"id": {
29+
Type: schema.TypeInt,
30+
Computed: true,
31+
},
32+
"push_access_levels": dataSourceGitlabProjectProtectedBranchSchemaAccessLevels(),
33+
"merge_access_levels": dataSourceGitlabProjectProtectedBranchSchemaAccessLevels(),
34+
"unprotect_access_levels": dataSourceGitlabProjectProtectedBranchSchemaAccessLevels(),
35+
"code_owner_approval_required": {
36+
Type: schema.TypeBool,
37+
Computed: true,
38+
},
39+
},
40+
}
41+
}
42+
43+
func dataSourceGitlabProjectProtectedBranchSchemaAccessLevels() *schema.Schema {
44+
return &schema.Schema{
45+
Type: schema.TypeList,
46+
Computed: true,
47+
Elem: &schema.Resource{
48+
Schema: map[string]*schema.Schema{
49+
"access_level": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
},
53+
"access_level_description": {
54+
Type: schema.TypeString,
55+
Computed: true,
56+
},
57+
"user_id": {
58+
Type: schema.TypeInt,
59+
Computed: true,
60+
},
61+
"group_id": {
62+
Type: schema.TypeInt,
63+
Computed: true,
64+
},
65+
},
66+
},
67+
}
68+
}
69+
70+
func dataSourceGitlabProjectProtectedBranchRead(d *schema.ResourceData, meta interface{}) error {
71+
client := meta.(*gitlab.Client)
72+
73+
log.Printf("[INFO] Reading Gitlab protected branch")
74+
75+
project := d.Get("project_id")
76+
name := d.Get("name").(string)
77+
78+
// Get protected branch by project ID/path and branch name
79+
pb, _, err := client.ProtectedBranches.GetProtectedBranch(project, name)
80+
if err != nil {
81+
return fmt.Errorf("error getting protected branch (Project: %v / Name %v): %v", project, name, err)
82+
}
83+
84+
if err := d.Set("push_access_levels", convertBranchAccessDescriptionsToStateBranchAccessDescriptions(pb.PushAccessLevels)); err != nil {
85+
return err
86+
}
87+
if err := d.Set("merge_access_levels", convertBranchAccessDescriptionsToStateBranchAccessDescriptions(pb.MergeAccessLevels)); err != nil {
88+
return err
89+
}
90+
if err := d.Set("unprotect_access_levels", convertBranchAccessDescriptionsToStateBranchAccessDescriptions(pb.UnprotectAccessLevels)); err != nil {
91+
return err
92+
}
93+
if err := d.Set("code_owner_approval_required", pb.CodeOwnerApprovalRequired); err != nil {
94+
return err
95+
}
96+
97+
d.SetId(fmt.Sprintf("%d", pb.ID))
98+
99+
return nil
100+
}
101+
102+
type stateBranchAccessDescription struct {
103+
AccessLevel string `json:"access_level" mapstructure:"access_level"`
104+
AccessLevelDescription string `json:"access_level_description" mapstructure:"access_level_description"`
105+
GroupID *int `json:"group_id,omitempty" mapstructure:"group_id,omitempty"`
106+
UserID *int `json:"user_id,omitempty" mapstructure:"user_id,omitempty"`
107+
}
108+
109+
func convertBranchAccessDescriptionsToStateBranchAccessDescriptions(descriptions []*gitlab.BranchAccessDescription) []stateBranchAccessDescription {
110+
result := make([]stateBranchAccessDescription, 0)
111+
112+
for _, description := range descriptions {
113+
result = append(result, convertBranchAccessDescriptionToStateBranchAccessDescription(description))
114+
}
115+
116+
return result
117+
}
118+
119+
func convertBranchAccessDescriptionToStateBranchAccessDescription(description *gitlab.BranchAccessDescription) stateBranchAccessDescription {
120+
stateDescription := stateBranchAccessDescription{
121+
AccessLevel: accessLevel[description.AccessLevel],
122+
AccessLevelDescription: description.AccessLevelDescription,
123+
}
124+
if description.UserID != 0 {
125+
stateDescription.UserID = &description.UserID
126+
}
127+
if description.GroupID != 0 {
128+
stateDescription.GroupID = &description.GroupID
129+
}
130+
return stateDescription
131+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccDataGitlabProjectProtectedBranchSearch(t *testing.T) {
12+
projectName := fmt.Sprintf("tf-%s", acctest.RandString(5))
13+
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheck(t) },
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccDataGitlabProjectProtectedBranchConfigGetProjectSearch(projectName),
20+
Check: resource.ComposeAggregateTestCheckFunc(
21+
resource.TestCheckResourceAttr(
22+
"data.gitlab_project_protected_branch.test",
23+
"name",
24+
"master",
25+
),
26+
resource.TestCheckResourceAttr(
27+
"data.gitlab_project_protected_branch.test",
28+
"push_access_levels.0.access_level",
29+
"maintainer",
30+
),
31+
),
32+
},
33+
},
34+
})
35+
}
36+
37+
func testAccDataGitlabProjectProtectedBranchConfigGetProjectSearch(projectName string) string {
38+
return fmt.Sprintf(`
39+
resource "gitlab_project" "test" {
40+
name = "%s"
41+
path = "%s"
42+
default_branch = "master"
43+
}
44+
45+
resource "gitlab_branch_protection" "master" {
46+
project = gitlab_project.test.id
47+
branch = "master"
48+
push_access_level = "maintainer"
49+
merge_access_level = "developer"
50+
}
51+
52+
resource "gitlab_branch_protection" "test" {
53+
project = gitlab_project.test.id
54+
branch = "master"
55+
push_access_level = "maintainer"
56+
merge_access_level = "developer"
57+
}
58+
59+
data "gitlab_project_protected_branch" "test" {
60+
project_id = gitlab_project.test.id
61+
name = gitlab_branch_protection.master.branch
62+
}
63+
`, projectName, projectName)
64+
}

0 commit comments

Comments
 (0)