From 3631fd8b865643ae9250d38efd1eb1dccff974be Mon Sep 17 00:00:00 2001 From: Isaac Blank Date: Wed, 17 Dec 2025 09:48:55 -0800 Subject: [PATCH] Support reading gh files above 1MB --- github/resource_github_repository_file.go | 29 ++++- .../resource_github_repository_file_test.go | 102 ++++++++++++++++++ 2 files changed, 128 insertions(+), 3 deletions(-) diff --git a/github/resource_github_repository_file.go b/github/resource_github_repository_file.go index 8a9c278cd9..57007fc553 100644 --- a/github/resource_github_repository_file.go +++ b/github/resource_github_repository_file.go @@ -1,6 +1,7 @@ package github import ( + "bytes" "context" "errors" "fmt" @@ -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 { diff --git a/github/resource_github_repository_file_test.go b/github/resource_github_repository_file_test.go index 03603e5d86..e4be8c4594 100644 --- a/github/resource_github_repository_file_test.go +++ b/github/resource_github_repository_file_test.go @@ -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) + }) + }) }