Skip to content

Commit 5d665e5

Browse files
committed
resource/gitlab_repository_file: Store content in text format if config is text format too
1 parent e1416fe commit 5d665e5

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

docs/resources/repository_file.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ resource "gitlab_repository_file" "this" {
4747
author_name = "Terraform"
4848
commit_message = "feature: add meow file"
4949
}
50+
51+
resource "gitlab_repository_file" "readme" {
52+
project = gitlab_project.this.id
53+
file_path = "readme.txt"
54+
branch = "main"
55+
// content will be auto base64 encoded
56+
content = "Meow goes the cat"
57+
author_email = "[email protected]"
58+
author_name = "Terraform"
59+
commit_message = "feature: add readme file"
60+
}
5061
```
5162

5263
<!-- schema generated by tfplugindocs -->

examples/resources/gitlab_repository_file/resource.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,14 @@ resource "gitlab_repository_file" "this" {
1717
author_name = "Terraform"
1818
commit_message = "feature: add meow file"
1919
}
20+
21+
resource "gitlab_repository_file" "readme" {
22+
project = gitlab_project.this.id
23+
file_path = "readme.txt"
24+
branch = "main"
25+
// content will be auto base64 encoded
26+
content = "Meow goes the cat"
27+
author_email = "[email protected]"
28+
author_name = "Terraform"
29+
commit_message = "feature: add readme file"
30+
}

internal/provider/resource_gitlab_repository_file.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ func resourceGitlabRepositoryFileRead(ctx context.Context, d *schema.ResourceDat
167167
return diag.FromErr(err)
168168
}
169169

170+
configContent := d.Get("content").(string)
171+
log.Printf("[DEBUG] gitlab_repository_file: comparing content of %s with %s", repositoryFile.Content, configContent)
172+
// NOTE: for backwards-compatibility reasons, we also support an already given base64 encoding,
173+
// otherwise we encode the `content` to base64.
174+
if _, err := base64.StdEncoding.DecodeString(configContent); err != nil {
175+
// if `content` is config is not a base64 encoded string, we decode the one from the API, too
176+
// in case it's base64 encoded, else we don't decode it.
177+
if decodedContent, err := base64.StdEncoding.DecodeString(repositoryFile.Content); err == nil {
178+
repositoryFile.Content = string(decodedContent)
179+
}
180+
}
181+
170182
d.SetId(resourceGitLabRepositoryFileBuildId(project, branch, repositoryFile.FilePath))
171183
d.Set("branch", repositoryFile.Ref)
172184
stateMap := gitlabRepositoryFileToStateMap(project, repositoryFile)

internal/provider/resource_gitlab_repository_file_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func TestAccGitlabRepositoryFile_basic(t *testing.T) {
2828
FilePath: "meow.txt",
2929
Content: "bWVvdyBtZW93IG1lb3c=",
3030
}),
31+
resource.TestCheckResourceAttr("gitlab_repository_file.this", "content", "bWVvdyBtZW93IG1lb3c="),
3132
),
3233
},
3334
{
@@ -44,6 +45,7 @@ func TestAccGitlabRepositoryFile_basic(t *testing.T) {
4445
FilePath: "meow.txt",
4546
Content: "bWVvdyBtZW93IG1lb3cgbWVvdyBtZW93Cg==",
4647
}),
48+
resource.TestCheckResourceAttr("gitlab_repository_file.this", "content", "bWVvdyBtZW93IG1lb3cgbWVvdyBtZW93Cg=="),
4749
),
4850
},
4951
{
@@ -168,6 +170,7 @@ func TestAccGitlabRepositoryFile_base64EncodingWithTextContent(t *testing.T) {
168170
FilePath: "meow.txt",
169171
Content: "SGVsbG8gV29ybGQsIG1lb3c=",
170172
}),
173+
resource.TestCheckResourceAttr("gitlab_repository_file.this", "content", "Hello World, meow"),
171174
),
172175
},
173176
// Update content
@@ -189,9 +192,10 @@ func TestAccGitlabRepositoryFile_base64EncodingWithTextContent(t *testing.T) {
189192
FilePath: "meow.txt",
190193
Content: "SGVsbG8gV29ybGQsIG1lb3cgVVBEQVRFRA==",
191194
}),
195+
resource.TestCheckResourceAttr("gitlab_repository_file.this", "content", "Hello World, meow UPDATED"),
192196
),
193197
},
194-
// Update to already base64 encoded content, but same content, yields in an empty plan
198+
// Update to already base64 encoded content, but same content
195199
{
196200
Config: fmt.Sprintf(`
197201
resource "gitlab_repository_file" "this" {
@@ -204,7 +208,7 @@ func TestAccGitlabRepositoryFile_base64EncodingWithTextContent(t *testing.T) {
204208
commit_message = "feature: add launch codes"
205209
}
206210
`, testProject.ID),
207-
PlanOnly: true,
211+
Check: resource.TestCheckResourceAttr("gitlab_repository_file.this", "content", "SGVsbG8gV29ybGQsIG1lb3cgVVBEQVRFRA=="),
208212
},
209213
// Update content and already base64 encode it
210214
{
@@ -225,9 +229,10 @@ func TestAccGitlabRepositoryFile_base64EncodingWithTextContent(t *testing.T) {
225229
FilePath: "meow.txt",
226230
Content: "bWVvdyBtZW93IG1lb3c=",
227231
}),
232+
resource.TestCheckResourceAttr("gitlab_repository_file.this", "content", "bWVvdyBtZW93IG1lb3c="),
228233
),
229234
},
230-
// Update to not-yet base64 encoded content, but same content, yields in an empty plan
235+
// Update to not-yet base64 encoded content, but same content
231236
{
232237
Config: fmt.Sprintf(`
233238
resource "gitlab_repository_file" "this" {
@@ -240,7 +245,7 @@ func TestAccGitlabRepositoryFile_base64EncodingWithTextContent(t *testing.T) {
240245
commit_message = "feature: add launch codes"
241246
}
242247
`, testProject.ID),
243-
PlanOnly: true,
248+
Check: resource.TestCheckResourceAttr("gitlab_repository_file.this", "content", "meow meow meow"),
244249
},
245250
},
246251
})

internal/provider/schema_gitlab_repository_file.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package provider
22

33
import (
4-
"encoding/base64"
5-
64
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
75
"github.com/xanzy/go-gitlab"
86
)
@@ -45,12 +43,6 @@ func gitlabRepositoryFileGetSchema() map[string]*schema.Schema {
4543
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).",
4644
Type: schema.TypeString,
4745
Required: true,
48-
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
49-
if _, err := base64.StdEncoding.DecodeString(new); err != nil {
50-
new = base64.StdEncoding.EncodeToString([]byte(new))
51-
}
52-
return old == new
53-
},
5446
},
5547
"content_sha256": {
5648
Description: "File content sha256 digest.",

0 commit comments

Comments
 (0)