Skip to content

Commit b9e52ac

Browse files
committed
Support reading gh files above 1MB
1 parent 831092e commit b9e52ac

File tree

2 files changed

+128
-3
lines changed

2 files changed

+128
-3
lines changed

github/resource_github_repository_file.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package github
22

33
import (
4+
"bytes"
45
"context"
56
"errors"
67
"fmt"
@@ -314,9 +315,31 @@ func resourceGithubRepositoryFileRead(d *schema.ResourceData, meta any) error {
314315
return nil
315316
}
316317

317-
content, err := fc.GetContent()
318-
if err != nil {
319-
return err
318+
encoding := fc.GetEncoding()
319+
320+
var content string
321+
322+
if encoding == "" || encoding == "none" {
323+
rawURL := fc.GetURL()
324+
325+
req, err := client.NewRequest("GET", rawURL, nil)
326+
if err != nil {
327+
return err
328+
}
329+
330+
req.Header.Set("Accept", "application/vnd.github.raw+json")
331+
332+
var buf bytes.Buffer
333+
if _, err := client.Do(ctx, req, &buf); err != nil {
334+
return err
335+
}
336+
337+
content = buf.String()
338+
} else {
339+
content, err = fc.GetContent()
340+
if err != nil {
341+
return err
342+
}
320343
}
321344

322345
if err = d.Set("content", content); err != nil {

github/resource_github_repository_file_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,106 @@ func TestAccGithubRepositoryFile(t *testing.T) {
403403
testCase(t, organization)
404404
})
405405
})
406+
407+
t.Run("handles files larger than 1MB with raw encoding", func(t *testing.T) {
408+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
409+
410+
initialContent := strings.Repeat("A", 1200000)
411+
updatedContent := strings.Repeat("B", 1200000)
412+
413+
initialConfig := fmt.Sprintf(`
414+
resource "github_repository" "test" {
415+
name = "tf-acc-test-large-file-%s"
416+
auto_init = true
417+
vulnerability_alerts = true
418+
}
419+
420+
resource "github_repository_file" "test" {
421+
repository = github_repository.test.name
422+
branch = "main"
423+
file = "large-file.txt"
424+
content = %q
425+
commit_message = "Add large file (>1MB) to test raw encoding"
426+
commit_author = "Terraform User"
427+
commit_email = "terraform@example.com"
428+
}
429+
`, randomID, initialContent)
430+
431+
updatedConfig := fmt.Sprintf(`
432+
resource "github_repository" "test" {
433+
name = "tf-acc-test-large-file-%s"
434+
auto_init = true
435+
vulnerability_alerts = true
436+
}
437+
438+
resource "github_repository_file" "test" {
439+
repository = github_repository.test.name
440+
branch = "main"
441+
file = "large-file.txt"
442+
content = %q
443+
commit_message = "Update large file (>1MB) to test raw encoding on read"
444+
commit_author = "Terraform User"
445+
commit_email = "terraform@example.com"
446+
}
447+
`, randomID, updatedContent)
448+
449+
initialCheck := resource.ComposeTestCheckFunc(
450+
resource.TestCheckResourceAttr(
451+
"github_repository_file.test", "content",
452+
initialContent,
453+
),
454+
resource.TestCheckResourceAttrSet(
455+
"github_repository_file.test", "sha",
456+
),
457+
resource.TestCheckResourceAttrSet(
458+
"github_repository_file.test", "commit_sha",
459+
),
460+
resource.TestCheckResourceAttr(
461+
"github_repository_file.test", "file",
462+
"large-file.txt",
463+
),
464+
)
465+
466+
updatedCheck := resource.ComposeTestCheckFunc(
467+
resource.TestCheckResourceAttr(
468+
"github_repository_file.test", "content",
469+
updatedContent,
470+
),
471+
resource.TestCheckResourceAttrSet(
472+
"github_repository_file.test", "sha",
473+
),
474+
resource.TestCheckResourceAttrSet(
475+
"github_repository_file.test", "commit_sha",
476+
),
477+
)
478+
479+
testCase := func(t *testing.T, mode string) {
480+
resource.Test(t, resource.TestCase{
481+
PreCheck: func() { skipUnlessMode(t, mode) },
482+
Providers: testAccProviders,
483+
Steps: []resource.TestStep{
484+
{
485+
Config: initialConfig,
486+
Check: initialCheck,
487+
},
488+
{
489+
Config: updatedConfig,
490+
Check: updatedCheck,
491+
},
492+
},
493+
})
494+
}
495+
496+
t.Run("with an anonymous account", func(t *testing.T) {
497+
t.Skip("anonymous account not supported for this operation")
498+
})
499+
500+
t.Run("with an individual account", func(t *testing.T) {
501+
testCase(t, individual)
502+
})
503+
504+
t.Run("with an organization account", func(t *testing.T) {
505+
testCase(t, organization)
506+
})
507+
})
406508
}

0 commit comments

Comments
 (0)