Skip to content

Commit acbd41c

Browse files
eddb7Edward ButlerEd BStromweldarmsnyder
authored
Added gitlab_branch as a resource and data source (#634)
* added create and basic read method * added basic read and delete methods * added base test case * uupdate logic for tests * fmt * updated branch read method * updated logic for setting branch id * added working check for branch exists test * added attributes and extra check to test case * added checks on attributes for test * updated tests * added read method and shcema to data source for branch * added data source to provider and created basic test case * added data source test * added check for nested ref * added test case with protected branch * fixed error with commit and removed protection attributes for better consistency users should get these attributes by using gitlab_branch_protection resources * updated attributes and created doc for data source branch * added docs for gitlab_branch resource * Update data_source_gitlab_branch.go remove unused import * update to two part id * use two part id for resource * fmt * remove unused * update resource commit * update flatten commit method * remove max items * updated logic for handling 404 * remove required * added importer * update refs * updated so markdown lint would pass * removed fmt * md lint suggestions * switch to set type * update * update tests * Update docs/resources/branch.md Co-authored-by: Corey Hemminger <[email protected]> * update docs * changed map type schema for commit * syntax' * fmt * added check for empty commit * force error * force error * update provider * updated error check * update to use set * changed to set type * update schema conversion * added set method * fmt * force error * update method * update to working test * update read logic for branch * build id correctly on create * set ref * add fmt method * added logic to handle import of ref on read * remove unused method * remove unused * added method to get ref * mapped all properties to commit * added type conversions for commit * added error checks into ref * added default for ref * update gitlab * update ref schema * update ref * update docs * updated to add ref on import * comment * updated functions with context method and added new method for ref added context to functions added method for ref updated tests added ctx into statement comment out data source test to debug added to test case updated test case fixed name lookup fix formatting added protected to data source fmt and added extra properties added test steps to branch resource switch syntax for test ints updated test cases fix typo remove unused fix type issues in test updated defaults updated docs added ref lookup updated data source test fix ref step fmt switched incorrect method in test update ref test fix ref updated ref upated ref logic update gitlab ref added examples and descriptions + generated docs updated data source test remove unused * suggestions from peer review suggestions from peer review update tests to use setup methods in helper_test ran make reviewable * added test step added test step remove max items fmt test fix * Apply suggestions from code review suggested changes from peer review Co-authored-by: Adam Snyder <[email protected]> * update ref logic from review suggestions Co-authored-by: Edward Butler <[email protected]> Co-authored-by: Ed B <[email protected]> Co-authored-by: Corey Hemminger <[email protected]> Co-authored-by: Adam Snyder <[email protected]>
1 parent 5b699f8 commit acbd41c

File tree

9 files changed

+788
-0
lines changed

9 files changed

+788
-0
lines changed

docs/data-sources/branch.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_branch Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
Provide details about a gitlab project branch
7+
---
8+
9+
# gitlab_branch (Data Source)
10+
11+
Provide details about a gitlab project branch
12+
13+
## Example Usage
14+
15+
```terraform
16+
# By project ID
17+
data "gitlab_branch" "foo" {
18+
name = "example"
19+
project = "12345"
20+
}
21+
22+
# By project full path
23+
data "gitlab_branch" "foo" {
24+
name = "example"
25+
project = "foo/bar"
26+
}
27+
```
28+
29+
<!-- schema generated by tfplugindocs -->
30+
## Schema
31+
32+
### Required
33+
34+
- **name** (String) The name of the branch.
35+
- **project** (String) The full path or id of the project.
36+
37+
### Optional
38+
39+
- **id** (String) The ID of this resource.
40+
41+
### Read-Only
42+
43+
- **can_push** (Boolean) Bool, true if you can push to the branch.
44+
- **commit** (Set of Object) The commit associated with the branch ref. (see [below for nested schema](#nestedatt--commit))
45+
- **default** (Boolean) Bool, true if branch is the default branch for the project.
46+
- **developer_can_merge** (Boolean) Bool, true if developer level access allows to merge branch.
47+
- **developer_can_push** (Boolean) Bool, true if developer level access allows git push.
48+
- **merged** (Boolean) Bool, true if the branch has been merged into it's parent.
49+
- **protected** (Boolean) Bool, true if branch has branch protection.
50+
- **web_url** (String) The url of the created branch (https.)
51+
52+
<a id="nestedatt--commit"></a>
53+
### Nested Schema for `commit`
54+
55+
Read-Only:
56+
57+
- **author_email** (String)
58+
- **author_name** (String)
59+
- **authored_date** (String)
60+
- **committed_date** (String)
61+
- **committer_email** (String)
62+
- **committer_name** (String)
63+
- **id** (String)
64+
- **message** (String)
65+
- **parent_ids** (Set of String)
66+
- **short_id** (String)
67+
- **title** (String)
68+
69+

docs/resources/branch.md

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_branch Resource - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
This resource allows you to create and manage GitLab branches.
7+
---
8+
9+
# gitlab_branch (Resource)
10+
11+
This resource allows you to create and manage GitLab branches.
12+
13+
## Example Usage
14+
15+
```terraform
16+
# Create a project for the branch to use
17+
resource "gitlab_project" "example" {
18+
name = "example"
19+
description = "An example project"
20+
namespace_id = gitlab_group.example.id
21+
}
22+
23+
resource "gitlab_branch" "example" {
24+
name = "example"
25+
ref = "main"
26+
project = gitlab_project.example.id
27+
}
28+
```
29+
30+
<!-- schema generated by tfplugindocs -->
31+
## Schema
32+
33+
### Required
34+
35+
- **name** (String) The name for this branch.
36+
- **project** (String) The ID or full path of the project which the branch is created against.
37+
- **ref** (String) The ref which the branch is created from.
38+
39+
### Optional
40+
41+
- **id** (String) The ID of this resource.
42+
43+
### Read-Only
44+
45+
- **can_push** (Boolean) Bool, true if you can push to the branch.
46+
- **commit** (Set of Object) The commit associated with the branch ref. (see [below for nested schema](#nestedatt--commit))
47+
- **default** (Boolean) Bool, true if branch is the default branch for the project.
48+
- **developer_can_merge** (Boolean) Bool, true if developer level access allows to merge branch.
49+
- **developer_can_push** (Boolean) Bool, true if developer level access allows git push.
50+
- **merged** (Boolean) Bool, true if the branch has been merged into it's parent.
51+
- **protected** (Boolean) Bool, true if branch has branch protection.
52+
- **web_url** (String) The url of the created branch (https).
53+
54+
<a id="nestedatt--commit"></a>
55+
### Nested Schema for `commit`
56+
57+
Read-Only:
58+
59+
- **author_email** (String)
60+
- **author_name** (String)
61+
- **authored_date** (String)
62+
- **committed_date** (String)
63+
- **committer_email** (String)
64+
- **committer_name** (String)
65+
- **id** (String)
66+
- **message** (String)
67+
- **parent_ids** (Set of String)
68+
- **short_id** (String)
69+
- **title** (String)
70+
71+
## Import
72+
73+
Import is supported using the following syntax:
74+
75+
```shell
76+
# Gitlab protected branches can be imported with a key composed of `<project_id>:<branch_name>`, e.g.
77+
terraform import gitlab_branch.example "12345:develop"
78+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# By project ID
2+
data "gitlab_branch" "foo" {
3+
name = "example"
4+
project = "12345"
5+
}
6+
7+
# By project full path
8+
data "gitlab_branch" "foo" {
9+
name = "example"
10+
project = "foo/bar"
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Gitlab protected branches can be imported with a key composed of `<project_id>:<branch_name>`, e.g.
2+
terraform import gitlab_branch.example "12345:develop"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Create a project for the branch to use
2+
resource "gitlab_project" "example" {
3+
name = "example"
4+
description = "An example project"
5+
namespace_id = gitlab_group.example.id
6+
}
7+
8+
resource "gitlab_branch" "example" {
9+
name = "example"
10+
ref = "main"
11+
project = gitlab_project.example.id
12+
}
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"log"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/xanzy/go-gitlab"
10+
)
11+
12+
var _ = registerDataSource("gitlab_branch", func() *schema.Resource {
13+
return &schema.Resource{
14+
Description: "Provide details about a gitlab project branch",
15+
16+
ReadContext: dataSourceGitlabBranchRead,
17+
Schema: map[string]*schema.Schema{
18+
"name": {
19+
Description: "The name of the branch.",
20+
Type: schema.TypeString,
21+
Required: true,
22+
},
23+
"project": {
24+
Description: "The full path or id of the project.",
25+
Type: schema.TypeString,
26+
Required: true,
27+
},
28+
"web_url": {
29+
Description: "The url of the created branch (https.)",
30+
Type: schema.TypeString,
31+
Computed: true,
32+
},
33+
"default": {
34+
Description: "Bool, true if branch is the default branch for the project.",
35+
Type: schema.TypeBool,
36+
Computed: true,
37+
},
38+
"can_push": {
39+
Description: "Bool, true if you can push to the branch.",
40+
Type: schema.TypeBool,
41+
Computed: true,
42+
},
43+
"protected": {
44+
Description: "Bool, true if branch has branch protection.",
45+
Type: schema.TypeBool,
46+
Computed: true,
47+
},
48+
"merged": {
49+
Description: "Bool, true if the branch has been merged into it's parent.",
50+
Type: schema.TypeBool,
51+
Computed: true,
52+
},
53+
"developer_can_merge": {
54+
Description: "Bool, true if developer level access allows to merge branch.",
55+
Type: schema.TypeBool,
56+
Computed: true,
57+
},
58+
"developer_can_push": {
59+
Description: "Bool, true if developer level access allows git push.",
60+
Type: schema.TypeBool,
61+
Computed: true,
62+
},
63+
"commit": {
64+
Description: "The commit associated with the branch ref.",
65+
Type: schema.TypeSet,
66+
Computed: true,
67+
Set: schema.HashResource(commitSchema),
68+
Elem: commitSchema,
69+
},
70+
},
71+
}
72+
})
73+
74+
func dataSourceGitlabBranchRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
75+
client := meta.(*gitlab.Client)
76+
name := d.Get("name").(string)
77+
project := d.Get("project").(string)
78+
log.Printf("[DEBUG] read gitlab branch %s", name)
79+
branch, resp, err := client.Branches.GetBranch(project, name, gitlab.WithContext(ctx))
80+
if err != nil {
81+
log.Printf("[DEBUG] failed to read gitlab branch %s response %v", name, resp)
82+
return diag.FromErr(err)
83+
}
84+
85+
d.SetId(buildTwoPartID(&project, &name))
86+
d.Set("name", branch.Name)
87+
d.Set("project", project)
88+
d.Set("web_url", branch.WebURL)
89+
d.Set("default", branch.Default)
90+
d.Set("can_push", branch.CanPush)
91+
d.Set("protected", branch.Protected)
92+
d.Set("merged", branch.Merged)
93+
d.Set("developer_can_merge", branch.DevelopersCanMerge)
94+
d.Set("developer_can_push", branch.DevelopersCanPush)
95+
if err := d.Set("commit", flattenCommit(branch.Commit)); err != nil {
96+
return diag.FromErr(err)
97+
}
98+
return nil
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package provider
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10+
)
11+
12+
func TestAccDataGitlabBranch_basic(t *testing.T) {
13+
rInt := acctest.RandInt()
14+
testAccCheck(t)
15+
project := testAccCreateProject(t)
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
ProviderFactories: providerFactories,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccDataGitlabBranch(rInt, project.PathWithNamespace),
22+
Check: resource.ComposeTestCheckFunc(
23+
testAccDataSourceGitlabBranch("gitlab_branch.foo", "data.gitlab_branch.foo"),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
func testAccDataSourceGitlabBranch(src, n string) resource.TestCheckFunc {
31+
return func(s *terraform.State) error {
32+
33+
branch := s.RootModule().Resources[src]
34+
branchAttr := branch.Primary.Attributes
35+
36+
search := s.RootModule().Resources[n]
37+
searchAttr := search.Primary.Attributes
38+
39+
testAttributes := []string{
40+
"id",
41+
"name",
42+
"web_url",
43+
"default",
44+
"project",
45+
"can_push",
46+
"merged",
47+
"commit",
48+
"parent_ids",
49+
"protected",
50+
"developer_can_merge",
51+
"developer_can_push",
52+
}
53+
54+
for _, attribute := range testAttributes {
55+
if searchAttr[attribute] != branchAttr[attribute] {
56+
return fmt.Errorf("expected branch's parameter `%s` to be: %s, but got: `%s`", attribute, branchAttr[attribute], searchAttr[attribute])
57+
}
58+
}
59+
return nil
60+
}
61+
}
62+
63+
func testAccDataGitlabBranch(rInt int, project string) string {
64+
return fmt.Sprintf(`
65+
%s
66+
67+
data "gitlab_branch" "foo" {
68+
name = "${gitlab_branch.foo.name}"
69+
project = "%s"
70+
}
71+
`, testAccDataGitlabBranchSetup(rInt, project), project)
72+
}
73+
74+
func testAccDataGitlabBranchSetup(rInt int, project string) string {
75+
return fmt.Sprintf(`
76+
resource "gitlab_branch" "foo" {
77+
name = "testbranch-%[1]d"
78+
ref = "main"
79+
project = "%s"
80+
}
81+
`, rInt, project)
82+
}

0 commit comments

Comments
 (0)