Skip to content

Commit b02693f

Browse files
Petr Válatimofurrer
authored andcommitted
feat: Add gitlab_repository_file data source
This data source is part of Gitlab API. Closes: #927
1 parent d8c97a0 commit b02693f

File tree

3 files changed

+222
-0
lines changed

3 files changed

+222
-0
lines changed

docs/data-sources/repository_file.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_repository_file Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
Allows you to receive information about file in repository like name, size, content. File content is Base64 encoded. This endpoint can be accessed without authentication if the repository is publicly accessible.
7+
---
8+
9+
# gitlab_repository_file (Data Source)
10+
11+
Allows you to receive information about file in repository like name, size, content. File content is Base64 encoded. This endpoint can be accessed without authentication if the repository is publicly accessible.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- **file_path** (String) The full path of the file. It must be relative to the root of the project without a leading slash `/`.
21+
- **project** (String) The ID of the project.
22+
- **ref** (String) The name of branch, tag or commit.
23+
24+
### Optional
25+
26+
- **id** (String) The ID of this resource.
27+
28+
### Read-Only
29+
30+
- **blob_id** (String) String, blob id.
31+
- **commit_id** (String) String, commit id.
32+
- **content** (String) String, base64 encoded file content.
33+
- **content_sha256** (String) String, content sha256 digest.
34+
- **encoding** (String) String, file encoding.
35+
- **file_name** (String) String, file name.
36+
- **last_commit_id** (String) String, last commit id.
37+
- **size** (Number) Integer, file size.
38+
39+
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
gitlab "github.com/xanzy/go-gitlab"
11+
)
12+
13+
var _ = registerDataSource("gitlab_repository_file", func() *schema.Resource {
14+
return &schema.Resource{
15+
Description: "Allows you to receive information about file in repository like name, size, content. File content is Base64 encoded. This endpoint can be accessed without authentication if the repository is publicly accessible.",
16+
ReadContext: dataSourceGitlabRepositoryFileRead,
17+
Schema: map[string]*schema.Schema{
18+
"project": {
19+
Description: "The ID of the project.",
20+
Type: schema.TypeString,
21+
Required: true,
22+
},
23+
"file_path": {
24+
Description: "The full path of the file. It must be relative to the root of the project without a leading slash `/`.",
25+
Type: schema.TypeString,
26+
Required: true,
27+
},
28+
"ref": {
29+
Description: "The name of branch, tag or commit.",
30+
Type: schema.TypeString,
31+
Required: true,
32+
},
33+
"file_name": {
34+
Description: "String, file name.",
35+
Type: schema.TypeString,
36+
Computed: true,
37+
},
38+
"size": {
39+
Description: "Integer, file size.",
40+
Type: schema.TypeInt,
41+
Computed: true,
42+
},
43+
"encoding": {
44+
Description: "String, file encoding.",
45+
Type: schema.TypeString,
46+
Computed: true,
47+
},
48+
"content": {
49+
Description: "String, base64 encoded file content.",
50+
Type: schema.TypeString,
51+
Computed: true,
52+
},
53+
"content_sha256": {
54+
Description: "String, content sha256 digest.",
55+
Type: schema.TypeString,
56+
Computed: true,
57+
},
58+
"blob_id": {
59+
Description: "String, blob id.",
60+
Type: schema.TypeString,
61+
Computed: true,
62+
},
63+
"commit_id": {
64+
Description: "String, commit id.",
65+
Type: schema.TypeString,
66+
Computed: true,
67+
},
68+
"last_commit_id": {
69+
Description: "String, last commit id.",
70+
Type: schema.TypeString,
71+
Computed: true,
72+
},
73+
},
74+
}
75+
})
76+
77+
func dataSourceGitlabRepositoryFileRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
78+
client := meta.(*gitlab.Client)
79+
project := d.Get("project").(string)
80+
filePath := d.Get("file_path").(string)
81+
82+
options := &gitlab.GetFileOptions{
83+
Ref: gitlab.String(d.Get("ref").(string)),
84+
}
85+
86+
repositoryFile, resp, err := client.RepositoryFiles.GetFile(project, filePath, options, gitlab.WithContext(ctx))
87+
if err != nil {
88+
log.Printf("[DEBUG] file %s not found, response %v", filePath, resp)
89+
return diag.FromErr(err)
90+
}
91+
92+
d.SetId(fmt.Sprintf("%s:%s:%s", project, repositoryFile.Ref, repositoryFile.FilePath))
93+
94+
d.Set("project", project)
95+
d.Set("file_name", repositoryFile.FileName)
96+
d.Set("file_path", repositoryFile.FilePath)
97+
d.Set("size", repositoryFile.Size)
98+
d.Set("encoding", repositoryFile.Encoding)
99+
d.Set("content", repositoryFile.Content)
100+
d.Set("content_sha256", repositoryFile.SHA256)
101+
d.Set("ref", repositoryFile.Ref)
102+
d.Set("blob_id", repositoryFile.BlobID)
103+
d.Set("commit_id", repositoryFile.CommitID)
104+
d.Set("last_commit_id", repositoryFile.LastCommitID)
105+
106+
return nil
107+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package provider
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9+
)
10+
11+
func TestAccDataGitlabRepositoryFile_basic(t *testing.T) {
12+
testAccCheck(t)
13+
project := testAccCreateProject(t)
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheck(t) },
16+
ProviderFactories: providerFactories,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccDataGitlabRepositoryFile(project.PathWithNamespace),
20+
Check: resource.ComposeTestCheckFunc(
21+
testAccDataSourceGitlabRepositoryFile("gitlab_repository_file.foo", "data.gitlab_repository_file.foo"),
22+
),
23+
},
24+
},
25+
})
26+
}
27+
28+
func testAccDataSourceGitlabRepositoryFile(src, n string) resource.TestCheckFunc {
29+
return func(s *terraform.State) error {
30+
31+
file := s.RootModule().Resources[src]
32+
fileAttr := file.Primary.Attributes
33+
34+
search := s.RootModule().Resources[n]
35+
searchAttr := search.Primary.Attributes
36+
37+
testAttributes := []string{
38+
"project",
39+
"file_path",
40+
"file_path",
41+
"size",
42+
"encoding",
43+
"content",
44+
"ref",
45+
"blob_id",
46+
"commit_id",
47+
"content_sha256",
48+
"last_commit_id",
49+
}
50+
51+
for _, attribute := range testAttributes {
52+
if searchAttr[attribute] != fileAttr[attribute] {
53+
return fmt.Errorf("expected file's parameter `%s` to be: %s, but got: `%s`", attribute, fileAttr[attribute], searchAttr[attribute])
54+
}
55+
}
56+
return nil
57+
}
58+
}
59+
60+
func testAccDataGitlabRepositoryFile(project string) string {
61+
return fmt.Sprintf(`
62+
resource "gitlab_repository_file" "foo" {
63+
project = "%s"
64+
file_path = "testfile-meow"
65+
branch = "main"
66+
content = base64encode("Meow goes the cat")
67+
commit_message = "feat: Meow"
68+
}
69+
70+
data "gitlab_repository_file" "foo" {
71+
project = %s
72+
file_path = "testfile-meow"
73+
ref = "main"
74+
}
75+
`, project, project)
76+
}

0 commit comments

Comments
 (0)