From 61fe0289217320518a06852d130aff975f80b705 Mon Sep 17 00:00:00 2001 From: Manuel Riezebosch Date: Sat, 17 Feb 2024 16:40:27 +0100 Subject: [PATCH] fix(data_source_github_rest_api): only allow for 404 on err --- github/data_source_github_rest_api.go | 5 ++- github/data_source_github_rest_api_test.go | 38 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/github/data_source_github_rest_api.go b/github/data_source_github_rest_api.go index 2641990598..33d1a35958 100644 --- a/github/data_source_github_rest_api.go +++ b/github/data_source_github_rest_api.go @@ -50,7 +50,10 @@ func dataSourceGithubRestApiRead(d *schema.ResourceData, meta interface{}) error return err } - resp, _ := client.Do(ctx, req, &body) + resp, err := client.Do(ctx, req, &body) + if err != nil && resp.StatusCode != 404 { + return err + } h, err := json.Marshal(resp.Header) if err != nil { diff --git a/github/data_source_github_rest_api_test.go b/github/data_source_github_rest_api_test.go index f0c483ce68..664d611c87 100644 --- a/github/data_source_github_rest_api_test.go +++ b/github/data_source_github_rest_api_test.go @@ -3,6 +3,7 @@ package github import ( "fmt" "regexp" + "strings" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -114,4 +115,41 @@ func TestAccGithubRestApiDataSource(t *testing.T) { }) }) + + t.Run("fails for invalid endpoint", func(t *testing.T) { + + // 4096 characters is the maximum length for a URL + var endpoint = strings.Repeat("x", 4096) + config := fmt.Sprintf(` + data "github_rest_api" "test" { + endpoint = "/%v" + } + `, endpoint) + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile("Error: GET https://api.github.com/xx.*: 414"), + }, + }, + }) + } + + 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) + }) + + }) }