Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions github/resource_github_repository_file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package github

import (
"bytes"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -314,9 +315,31 @@ func resourceGithubRepositoryFileRead(d *schema.ResourceData, meta any) error {
return nil
}

content, err := fc.GetContent()
if err != nil {
return err
encoding := fc.GetEncoding()

var content string

if encoding == "" || encoding == "none" {
rawURL := fc.GetURL()

req, err := client.NewRequest("GET", rawURL, nil)
if err != nil {
return err
}

req.Header.Set("Accept", "application/vnd.github.raw+json")

var buf bytes.Buffer
if _, err := client.Do(ctx, req, &buf); err != nil {
return err
}

content = buf.String()
} else {
content, err = fc.GetContent()
if err != nil {
return err
}
}

if err = d.Set("content", content); err != nil {
Expand Down
102 changes: 102 additions & 0 deletions github/resource_github_repository_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,106 @@ func TestAccGithubRepositoryFile(t *testing.T) {
testCase(t, organization)
})
})

t.Run("handles files larger than 1MB with raw encoding", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

initialContent := strings.Repeat("A", 1200000)
updatedContent := strings.Repeat("B", 1200000)

initialConfig := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-large-file-%s"
auto_init = true
vulnerability_alerts = true
}

resource "github_repository_file" "test" {
repository = github_repository.test.name
branch = "main"
file = "large-file.txt"
content = %q
commit_message = "Add large file (>1MB) to test raw encoding"
commit_author = "Terraform User"
commit_email = "terraform@example.com"
}
`, randomID, initialContent)

updatedConfig := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-large-file-%s"
auto_init = true
vulnerability_alerts = true
}

resource "github_repository_file" "test" {
repository = github_repository.test.name
branch = "main"
file = "large-file.txt"
content = %q
commit_message = "Update large file (>1MB) to test raw encoding on read"
commit_author = "Terraform User"
commit_email = "terraform@example.com"
}
`, randomID, updatedContent)

initialCheck := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository_file.test", "content",
initialContent,
),
resource.TestCheckResourceAttrSet(
"github_repository_file.test", "sha",
),
resource.TestCheckResourceAttrSet(
"github_repository_file.test", "commit_sha",
),
resource.TestCheckResourceAttr(
"github_repository_file.test", "file",
"large-file.txt",
),
)

updatedCheck := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository_file.test", "content",
updatedContent,
),
resource.TestCheckResourceAttrSet(
"github_repository_file.test", "sha",
),
resource.TestCheckResourceAttrSet(
"github_repository_file.test", "commit_sha",
),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: initialConfig,
Check: initialCheck,
},
{
Config: updatedConfig,
Check: updatedCheck,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
testCase(t, individual)
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})
})
}