Skip to content

Commit 62d7edd

Browse files
committed
Added code for Repository Tree Datasource
1 parent e8a5cbd commit 62d7edd

File tree

3 files changed

+218
-0
lines changed

3 files changed

+218
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_repository_tree Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_repository_tree data source allows details of directories and files in a repository to be retrieved.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/repositories.html#list-repository-tree
8+
---
9+
10+
# gitlab_repository_tree (Data Source)
11+
12+
The `gitlab_repository_tree` data source allows details of directories and files in a repository to be retrieved.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/repositories.html#list-repository-tree)
15+
16+
17+
18+
<!-- schema generated by tfplugindocs -->
19+
## Schema
20+
21+
### Required
22+
23+
- `project` (String) The ID or full path of the project owned by the authenticated user.
24+
- `ref` (String) The name of a repository branch or tag.
25+
26+
### Optional
27+
28+
- `path` (String) The path inside repository. Used to get content of subdirectories.
29+
- `recursive` (Boolean) Boolean value used to get a recursive tree (false by default).
30+
31+
### Read-Only
32+
33+
- `id` (String) The ID of this resource.
34+
- `tree` (List of Object) The list of files/directories returned by the search (see [below for nested schema](#nestedatt--tree))
35+
36+
<a id="nestedatt--tree"></a>
37+
### Nested Schema for `tree`
38+
39+
Read-Only:
40+
41+
- `id` (String)
42+
- `mode` (String)
43+
- `name` (String)
44+
- `path` (String)
45+
- `type` (String)
46+
47+
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/mitchellh/hashstructure"
10+
gitlab "github.com/xanzy/go-gitlab"
11+
)
12+
13+
var _ = registerDataSource("gitlab_repository_tree", func() *schema.Resource {
14+
return &schema.Resource{
15+
Description: `The ` + "`gitlab_repository_tree`" + ` data source allows details of directories and files in a repository to be retrieved.
16+
17+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/repositories.html#list-repository-tree)`,
18+
19+
ReadContext: dataSourceGitlabRepositoryTreeRead,
20+
Schema: map[string]*schema.Schema{
21+
"project": {
22+
Description: "The ID or full path of the project owned by the authenticated user.",
23+
Type: schema.TypeString,
24+
Required: true,
25+
},
26+
"path": {
27+
Description: "The path inside repository. Used to get content of subdirectories.",
28+
Type: schema.TypeString,
29+
Optional: true,
30+
},
31+
"ref": {
32+
Description: "The name of a repository branch or tag.",
33+
Type: schema.TypeString,
34+
Required: true,
35+
},
36+
"recursive": {
37+
Description: "Boolean value used to get a recursive tree (false by default).",
38+
Type: schema.TypeBool,
39+
Optional: true,
40+
},
41+
"tree": {
42+
Description: "The list of files/directories returned by the search",
43+
Type: schema.TypeList,
44+
Computed: true,
45+
Elem: &schema.Resource{
46+
Schema: map[string]*schema.Schema{
47+
"id": {
48+
Description: "The SHA-1 hash of the tree or blob in the repository.",
49+
Type: schema.TypeString,
50+
Computed: true,
51+
},
52+
"name": {
53+
Description: "Name of the blob or tree in the repository",
54+
Type: schema.TypeString,
55+
Computed: true,
56+
},
57+
"type": {
58+
Description: "Type of object in the repository. Can be either type tree or of type blob",
59+
Type: schema.TypeString,
60+
Computed: true,
61+
},
62+
"path": {
63+
Description: "Path of the object inside of the repository.",
64+
Type: schema.TypeString,
65+
Computed: true,
66+
},
67+
"mode": {
68+
Description: "Unix access mode of the file in the repository.",
69+
Type: schema.TypeString,
70+
Computed: true,
71+
},
72+
},
73+
},
74+
},
75+
},
76+
}
77+
})
78+
79+
func dataSourceGitlabRepositoryTreeRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
80+
client := meta.(*gitlab.Client)
81+
project := d.Get("project").(string)
82+
83+
options := &gitlab.ListTreeOptions{
84+
ListOptions: gitlab.ListOptions{
85+
PerPage: 20,
86+
Page: 1,
87+
},
88+
Path: gitlab.String(d.Get("path").(string)),
89+
Ref: gitlab.String(d.Get("ref").(string)),
90+
Recursive: gitlab.Bool(d.Get("recursive").(bool)),
91+
}
92+
93+
var nodes []*gitlab.TreeNode
94+
for options.Page != 0 {
95+
96+
paginatedNodes, resp, err := client.Repositories.ListTree(project, options, gitlab.WithContext(ctx))
97+
if err != nil {
98+
return diag.FromErr(err)
99+
}
100+
101+
nodes = append(nodes, paginatedNodes...)
102+
103+
options.Page = resp.NextPage
104+
}
105+
106+
optionsHash, err := hashstructure.Hash(&options, nil)
107+
if err != nil {
108+
return diag.FromErr(err)
109+
}
110+
111+
d.SetId(fmt.Sprintf("%s:%d", project, optionsHash))
112+
if err := d.Set("tree", flattenGitlabRepositoryTree(project, nodes)); err != nil {
113+
return diag.Errorf("failed to set repository tree nodes to state: %v", err)
114+
}
115+
116+
return nil
117+
}
118+
119+
func flattenGitlabRepositoryTree(project string, treeNodes []*gitlab.TreeNode) []interface{} {
120+
treeNodeList := []interface{}{}
121+
122+
for _, node := range treeNodes {
123+
124+
values := map[string]interface{}{
125+
"id": project,
126+
"name": node.Name,
127+
"type": node.Type,
128+
"path": node.Path,
129+
"mode": node.Mode,
130+
}
131+
132+
treeNodeList = append(treeNodeList, values)
133+
}
134+
return treeNodeList
135+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//go:build acceptance
2+
// +build acceptance
3+
4+
package provider
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
)
12+
13+
func TestAccDataSourceGitlabRepositoryTree_basic(t *testing.T) {
14+
testProject := testAccCreateProject(t)
15+
16+
resource.ParallelTest(t, resource.TestCase{
17+
ProviderFactories: providerFactories,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: fmt.Sprintf(`
21+
data "gitlab_repository_tree" "this" {
22+
project = %d
23+
ref = "%s"
24+
}
25+
`, testProject.ID, testProject.DefaultBranch),
26+
Check: resource.ComposeTestCheckFunc(
27+
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.#", "1"),
28+
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.0.name", "README.md"),
29+
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.0.type", "blob"),
30+
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.0.path", "README.md"),
31+
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.0.mode", "100644"),
32+
),
33+
},
34+
},
35+
})
36+
}

0 commit comments

Comments
 (0)