Skip to content

Commit f888fd5

Browse files
committed
Updated order of required attributes in schemas, added example for repository tree datasource, added create repo file helper function
1 parent 705a44f commit f888fd5

File tree

7 files changed

+60
-28
lines changed

7 files changed

+60
-28
lines changed

docs/data-sources/repository_tree.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ The `gitlab_repository_tree` data source allows details of directories and files
1313

1414
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/repositories.html#list-repository-tree)
1515

16-
16+
## Example Usage
17+
18+
```terraform
19+
data "gitlab_repository_tree" "this" {
20+
project = "example"
21+
ref = "main"
22+
path = "ExampleSubFolder"
23+
recursive = true
24+
}
25+
```
1726

1827
<!-- schema generated by tfplugindocs -->
1928
## Schema
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
data "gitlab_repository_tree" "this" {
2+
project = "example"
3+
ref = "main"
4+
path = "ExampleSubFolder"
5+
recursive = true
6+
}

internal/provider/data_source_gitlab_repository_tree.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ var _ = registerDataSource("gitlab_repository_tree", func() *schema.Resource {
2323
Type: schema.TypeString,
2424
Required: true,
2525
},
26-
"path": {
27-
Description: "The path inside repository. Used to get content of subdirectories.",
28-
Type: schema.TypeString,
29-
Optional: true,
30-
},
3126
"ref": {
3227
Description: "The name of a repository branch or tag.",
3328
Type: schema.TypeString,
3429
Required: true,
3530
},
31+
"path": {
32+
Description: "The path inside repository. Used to get content of subdirectories.",
33+
Type: schema.TypeString,
34+
Optional: true,
35+
},
3636
"recursive": {
3737
Description: "Boolean value used to get a recursive tree (false by default).",
3838
Type: schema.TypeBool,

internal/provider/data_source_gitlab_repository_tree_test.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,28 @@ import (
1212

1313
func TestAccDataSourceGitlabRepositoryTree_basic(t *testing.T) {
1414
testProject := testAccCreateProject(t)
15+
testFile := testAccCreateProjectFile(t, testProject.ID, "content", "SomeFile", testProject.DefaultBranch)
1516

1617
resource.ParallelTest(t, resource.TestCase{
1718
ProviderFactories: providerFactories,
1819
Steps: []resource.TestStep{
1920
{
2021
Config: fmt.Sprintf(`
21-
resource "gitlab_repository_file" "foo" {
22-
project = "%[1]d"
23-
file_path = "testfile-meow"
24-
branch = "%[2]s"
25-
content = base64encode("Meow goes the cat")
26-
commit_message = "feat: Meow"
27-
}
28-
2922
data "gitlab_repository_tree" "this" {
3023
project = %[1]d
31-
ref = gitlab_repository_file.foo.branch
24+
ref = "%[2]s"
3225
}
33-
`, testProject.ID, testProject.DefaultBranch),
26+
`, testProject.ID, testFile.Branch),
3427
Check: resource.ComposeTestCheckFunc(
3528
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.#", "2"),
3629
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.0.name", "README.md"),
3730
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.0.type", "blob"),
3831
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.0.path", "README.md"),
3932
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.0.mode", "100644"),
4033

41-
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.1.name", "testfile-meow"),
34+
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.1.name", testFile.FilePath),
4235
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.1.type", "blob"),
43-
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.1.path", "testfile-meow"),
36+
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.1.path", testFile.FilePath),
4437
resource.TestCheckResourceAttr("data.gitlab_repository_tree.this", "tree.1.mode", "100644"),
4538
),
4639
},

internal/provider/helper_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,30 @@ func testAccCreateInstanceVariable(t *testing.T) *gitlab.InstanceVariable {
519519
return variable
520520
}
521521

522+
func testAccCreateProjectFile(t *testing.T, projectID int, fileContent string, filePath string, branch string) *gitlab.FileInfo {
523+
524+
file, _, err := testGitlabClient.RepositoryFiles.CreateFile(projectID, filePath, &gitlab.CreateFileOptions{
525+
Branch: &branch,
526+
Encoding: gitlab.String("base64"),
527+
Content: &fileContent,
528+
CommitMessage: gitlab.String(fmt.Sprintf("Random_Commit_Message_%d", acctest.RandInt())),
529+
})
530+
if err != nil {
531+
t.Fatal(err)
532+
}
533+
534+
t.Cleanup(func() {
535+
if _, err := testGitlabClient.RepositoryFiles.DeleteFile(projectID, filePath, &gitlab.DeleteFileOptions{
536+
Branch: &branch,
537+
CommitMessage: gitlab.String(fmt.Sprintf("Delete_Random_Commit_Message_%d", acctest.RandInt())),
538+
}); err != nil {
539+
t.Fatal(err)
540+
}
541+
})
542+
543+
return file
544+
}
545+
522546
// testAccGitlabProjectContext encapsulates a GitLab client and test project to be used during an
523547
// acceptance test.
524548
type testAccGitlabProjectContext struct {

internal/provider/resource_gitlab_repository_file.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ var _ = registerResource("gitlab_repository_file", func() *schema.Resource {
6969
Required: true,
7070
ForceNew: true,
7171
},
72+
"commit_message": {
73+
Description: "Commit message.",
74+
Type: schema.TypeString,
75+
Required: true,
76+
},
7277
"start_branch": {
7378
Description: "Name of the branch to start the new commit from.",
7479
Type: schema.TypeString,
@@ -84,11 +89,6 @@ var _ = registerResource("gitlab_repository_file", func() *schema.Resource {
8489
Type: schema.TypeString,
8590
Optional: true,
8691
},
87-
"commit_message": {
88-
Description: "Commit message.",
89-
Type: schema.TypeString,
90-
Required: true,
91-
},
9292
},
9393
gitlabRepositoryFileGetSchema(),
9494
),

internal/provider/schema_gitlab_repository_file.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ func gitlabRepositoryFileGetSchema() map[string]*schema.Schema {
1919
Required: true,
2020
ForceNew: true,
2121
},
22+
"content": {
23+
Description: "File content. If the content is not yet base64 encoded, it will be encoded automatically. No other encoding is currently supported, because of a [GitLab API bug](https://gitlab.com/gitlab-org/gitlab/-/issues/342430).",
24+
Type: schema.TypeString,
25+
Required: true,
26+
},
2227
"ref": {
2328
Description: "The name of branch, tag or commit.",
2429
Type: schema.TypeString,
@@ -39,11 +44,6 @@ func gitlabRepositoryFileGetSchema() map[string]*schema.Schema {
3944
Type: schema.TypeString,
4045
Computed: true,
4146
},
42-
"content": {
43-
Description: "File content. If the content is not yet base64 encoded, it will be encoded automatically. No other encoding is currently supported, because of a [GitLab API bug](https://gitlab.com/gitlab-org/gitlab/-/issues/342430).",
44-
Type: schema.TypeString,
45-
Required: true,
46-
},
4747
"content_sha256": {
4848
Description: "File content sha256 digest.",
4949
Type: schema.TypeString,

0 commit comments

Comments
 (0)