Skip to content

Commit 9cc7dd4

Browse files
committed
Ensure in acceptance test that master branch is protected
This is normally the case when creating from a template project, but not in an empty one. Signed-off-by: Sune Keller <[email protected]>
1 parent daceb44 commit 9cc7dd4

3 files changed

+24
-14
lines changed

gitlab/data_source_gitlab_project_protected_branch.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66

77
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
89
"github.com/xanzy/go-gitlab"
910
)
1011

@@ -13,14 +14,16 @@ func dataSourceGitlabProjectProtectedBranch() *schema.Resource {
1314
Read: dataSourceGitlabProjectProtectedBranchRead,
1415
Schema: map[string]*schema.Schema{
1516
"project_id": {
16-
Type: schema.TypeString,
17-
Description: "ID or URL encoded name of project",
18-
Required: true,
17+
Type: schema.TypeString,
18+
Description: "ID or URL encoded name of project",
19+
Required: true,
20+
ValidateFunc: validation.StringIsNotEmpty,
1921
},
2022
"name": {
21-
Type: schema.TypeString,
22-
Description: "Name of the protected branch",
23-
Required: true,
23+
Type: schema.TypeString,
24+
Description: "Name of the protected branch",
25+
Required: true,
26+
ValidateFunc: validation.StringIsNotEmpty,
2427
},
2528
"id": {
2629
Type: schema.TypeInt,
@@ -75,7 +78,7 @@ func dataSourceGitlabProjectProtectedBranchRead(d *schema.ResourceData, meta int
7578
// Get protected branch by project ID/path and branch name
7679
pb, _, err := client.ProtectedBranches.GetProtectedBranch(project, name)
7780
if err != nil {
78-
return err
81+
return fmt.Errorf("error getting protected branch (Project: %v / Name %v): %v", project, name, err)
7982
}
8083

8184
if err := d.Set("push_access_levels", convertBranchAccessDescriptionsToStateBranchAccessDescriptions(pb.PushAccessLevels)); err != nil {

gitlab/data_source_gitlab_project_protected_branch_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ func TestAccDataGitlabProjectProtectedBranchSearch(t *testing.T) {
1919
Config: testAccDataGitlabProjectProtectedBranchConfigGetProjectSearch(projectName),
2020
Check: resource.ComposeAggregateTestCheckFunc(
2121
resource.TestCheckResourceAttr(
22-
"data.gitlab_project_protected_branches.test",
22+
"data.gitlab_project_protected_branch.test",
2323
"name",
2424
"master",
2525
),
2626
resource.TestCheckResourceAttr(
2727
"data.gitlab_project_protected_branch.test",
2828
"push_access_levels.0.access_level",
29-
"40",
29+
"maintainer",
3030
),
3131
),
3232
},
@@ -42,16 +42,23 @@ resource "gitlab_project" "test" {
4242
default_branch = "master"
4343
}
4444
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+
4552
resource "gitlab_branch_protection" "test" {
4653
project = gitlab_project.test.id
47-
branch = gitlab_project.test.default_branch
54+
branch = "master"
4855
push_access_level = "maintainer"
4956
merge_access_level = "developer"
5057
}
5158
5259
data "gitlab_project_protected_branch" "test" {
5360
project_id = gitlab_project.test.id
54-
name = gitlab_project.test.default_branch
61+
name = gitlab_branch_protection.master.branch
5562
}
5663
`, projectName, projectName)
5764
}

gitlab/data_source_gitlab_project_protected_branches_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestAccDataGitlabProjectProtectedBranchesSearch(t *testing.T) {
2626
resource.TestCheckResourceAttr(
2727
"data.gitlab_project_protected_branches.test",
2828
"protected_branches.0.push_access_levels.0.access_level",
29-
"40",
29+
"maintainer",
3030
),
3131
),
3232
},
@@ -44,13 +44,13 @@ resource "gitlab_project" "test" {
4444
4545
resource "gitlab_branch_protection" "test" {
4646
project = gitlab_project.test.id
47-
branch = gitlab_project.test.default_branch
47+
branch = "master"
4848
push_access_level = "maintainer"
4949
merge_access_level = "developer"
5050
}
5151
5252
data "gitlab_project_protected_branches" "test" {
53-
project_id = gitlab_project.test.id
53+
project_id = gitlab_branch_protection.test.project # This expresses the dependency of the data source on the protected branch having first been configured
5454
}
5555
`, projectName, projectName)
5656
}

0 commit comments

Comments
 (0)