Skip to content

Commit

Permalink
fix(data_source_github_rest_api): store raw body in state (#2152)
Browse files Browse the repository at this point in the history
Co-authored-by: Keegan Campbell <me@kfcampbell.com>
  • Loading branch information
riezebosch and kfcampbell authored Mar 12, 2024
1 parent 30e6176 commit af693fd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
7 changes: 3 additions & 4 deletions github/data_source_github_rest_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github
import (
"context"
"encoding/json"
"io"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -43,14 +44,12 @@ func dataSourceGithubRestApiRead(d *schema.ResourceData, meta interface{}) error
client := meta.(*Owner).v3client
ctx := context.Background()

var body map[string]interface{}

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

resp, err := client.Do(ctx, req, &body)
resp, err := client.Do(ctx, req, nil)
if err != nil && resp.StatusCode != 404 {
return err
}
Expand All @@ -60,7 +59,7 @@ func dataSourceGithubRestApiRead(d *schema.ResourceData, meta interface{}) error
return err
}

b, err := json.Marshal(body)
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
46 changes: 45 additions & 1 deletion github/data_source_github_rest_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestAccGithubRestApiDataSource(t *testing.T) {
resource.TestMatchResourceAttr(
"data.github_rest_api.test", "status", regexp.MustCompile("200 OK"),
),
resource.TestCheckResourceAttrSet("data.github_rest_api.test", "body"),
resource.TestMatchResourceAttr("data.github_rest_api.test", "body", regexp.MustCompile(".*refs/heads/.*")),
resource.TestCheckResourceAttrSet("data.github_rest_api.test", "headers"),
)

Expand Down Expand Up @@ -65,6 +65,50 @@ func TestAccGithubRestApiDataSource(t *testing.T) {

})

t.Run("queries a collection without error", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%[1]s"
auto_init = true
}
data "github_rest_api" "test" {
endpoint = "repos/${github_repository.test.full_name}/git/refs/heads/"
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("data.github_rest_api.test", "body", regexp.MustCompile(`\[.*refs/heads/.*\]`)),
)

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

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)
})

})

t.Run("queries an invalid branch without error", func(t *testing.T) {

config := fmt.Sprintf(`
Expand Down

0 comments on commit af693fd

Please sign in to comment.