Skip to content

Commit fadad7d

Browse files
committed
fix(data_source_github_release_asset): base64 encode file
Per code review feedback from @stevehipwell (#2514 (comment)), this ensures `file` is base64-encoded. Signed-off-by: Mike Ball <mikedball@gmail.com>
1 parent 0ed58c9 commit fadad7d

4 files changed

Lines changed: 23 additions & 6 deletions

File tree

github/acc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func TestMain(m *testing.M) {
117117
// https://github.com/integrations/terraform-provider-github/releases/tag/v6.4.0
118118
testPublicRelaseAssetId: "207956097",
119119
testPublicRelaseAssetName: "terraform-provider-github_6.4.0_manifest.json",
120-
testPublicReleaseAssetContent: "{\n \"version\": 1,\n \"metadata\": {\n \"protocol_versions\": [\n \"5.0\"\n ]\n }\n}\n",
120+
testPublicReleaseAssetContent: "{\n \"version\": 1,\n \"metadata\": {\n \"protocol_versions\": [\n \"5.0\"\n ]\n }\n}",
121121
testPublicTemplateRepository: "template-repository",
122122
testPublicTemplateRepositoryOwner: "template-repository",
123123
testGHActionsAppInstallationId: 15368,

github/data_source_github_release_asset.go

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

33
import (
44
"context"
5+
"encoding/base64"
56
"io"
67
"net/http"
78
"strconv"
@@ -41,7 +42,7 @@ func dataSourceGithubReleaseAsset() *schema.Resource {
4142
"file": {
4243
Type: schema.TypeString,
4344
Computed: true,
44-
Description: "The release asset file contents (requires `download_file` to be `true`",
45+
Description: "The base64-encoded release asset file contents (requires `download_file` to be `true`",
4546
},
4647
"url": {
4748
Type: schema.TypeString,
@@ -158,8 +159,22 @@ func dataSourceGithubReleaseAssetRead(ctx context.Context, d *schema.ResourceDat
158159
}
159160

160161
buf := new(strings.Builder)
161-
if _, err := io.Copy(buf, resp.Body); err != nil {
162-
return diag.FromErr(err)
162+
encoder := base64.NewEncoder(base64.StdEncoding, buf)
163+
defer encoder.Close()
164+
buffer := make([]byte, 4096)
165+
for {
166+
n, err := resp.Body.Read(buffer)
167+
if err != nil && err != io.EOF {
168+
return diag.FromErr(err)
169+
}
170+
if n > 0 {
171+
if _, err := encoder.Write(buffer[:n]); err != nil {
172+
return diag.FromErr(err)
173+
}
174+
}
175+
if err == io.EOF {
176+
break
177+
}
163178
}
164179

165180
if err := d.Set("file", buf.String()); err != nil {

github/data_source_github_release_asset_test.go

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

33
import (
4+
"encoding/base64"
45
"fmt"
56
"testing"
67

@@ -14,6 +15,7 @@ func TestAccGithubReleaseAssetDataSource(t *testing.T) {
1415
testReleaseAssetID := testAccConf.testPublicRelaseAssetId
1516
testReleaseAssetName := testAccConf.testPublicRelaseAssetName
1617
testReleaseAssetContent := testAccConf.testPublicReleaseAssetContent
18+
base64EncodedAssetContent := base64.StdEncoding.EncodeToString([]byte(testReleaseAssetContent))
1719

1820
t.Run("queries and downloads specified asset ID", func(t *testing.T) {
1921
config := fmt.Sprintf(`
@@ -38,7 +40,7 @@ func TestAccGithubReleaseAssetDataSource(t *testing.T) {
3840
"data.github_release_asset.test", "name", testReleaseAssetName,
3941
),
4042
resource.TestCheckResourceAttr(
41-
"data.github_release_asset.test", "file", testReleaseAssetContent,
43+
"data.github_release_asset.test", "file", base64EncodedAssetContent,
4244
),
4345
),
4446
},

website/docs/d/release_asset.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ data "github_release_asset" "example" {
8585
* `created_at` - Date the asset was created
8686
* `updated_at` - Date the asset was last updated
8787
* `browser_download_url` - Browser URL from which the release asset can be downloaded
88-
* `file` - The release asset file contents (requires `download_file` to be `true`)
88+
* `file` - The base64-encoded release asset file contents (requires `download_file` to be `true`)

0 commit comments

Comments
 (0)