Skip to content

Commit dac6b1b

Browse files
committed
feat: add github_release_asset data source
This addresses issue #2513 and adds support for a `github_release_asset` data source. Example of passing acceptance tests: ``` $ GITHUB_ORGANIZATION=mdb GITHUB_OWNER=mdb GITHUB_TEMPLATE_REPOSITORY=terraputs GITHUB_TEMPLATE_REPOSITORY_RELEASE_ASSET_ID=206493547 GITHUB_TEMPLATE_REPOSITORY_RELEASE_ASSET_NAME=checksums.txt TF_ACC=1 go test -v ./... -run ^TestAccGithubReleaseAssetDataSource ? github.com/integrations/terraform-provider-github/v6 [no test files] === RUN TestAccGithubReleaseAssetDataSource === RUN TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID === RUN TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_anonymous_account provider_utils.go:51: GITHUB_TOKEN environment variable should be empty provider_utils.go:74: Skipping TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_anonymous_account which requires anonymous mode === RUN TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_individual_account === RUN TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_organization_account --- PASS: TestAccGithubReleaseAssetDataSource (5.20s) --- PASS: TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID (5.20s) --- SKIP: TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_anonymous_account (0.00s) --- PASS: TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_individual_account (2.57s) --- PASS: TestAccGithubReleaseAssetDataSource/queries_specified_asset_ID/with_an_organization_account (2.62s) PASS ok github.com/integrations/terraform-provider-github/v6/github 5.688s ``` Signed-off-by: Mike Ball <[email protected]>
1 parent 1c11053 commit dac6b1b

File tree

4 files changed

+295
-0
lines changed

4 files changed

+295
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"io"
6+
"net/http"
7+
"strconv"
8+
"strings"
9+
10+
"github.com/google/go-github/v66/github"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
)
13+
14+
func dataSourceGithubReleaseAsset() *schema.Resource {
15+
return &schema.Resource{
16+
Read: dataSourceGithubReleaseAssetRead,
17+
18+
Schema: map[string]*schema.Schema{
19+
"asset_id": {
20+
Type: schema.TypeInt,
21+
Required: true,
22+
},
23+
"owner": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
},
27+
"repository": {
28+
Type: schema.TypeString,
29+
Required: true,
30+
},
31+
"body": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
"url": {
36+
Type: schema.TypeString,
37+
Computed: true,
38+
},
39+
"node_id": {
40+
Type: schema.TypeString,
41+
Computed: true,
42+
},
43+
"name": {
44+
Type: schema.TypeString,
45+
Computed: true,
46+
},
47+
"label": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
},
51+
"content_type": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
},
55+
"size": {
56+
Type: schema.TypeInt,
57+
Computed: true,
58+
},
59+
"created_at": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
},
63+
"updated_at": {
64+
Type: schema.TypeString,
65+
Computed: true,
66+
},
67+
"browser_download_url": {
68+
Type: schema.TypeString,
69+
Computed: true,
70+
},
71+
},
72+
}
73+
}
74+
75+
func dataSourceGithubReleaseAssetRead(d *schema.ResourceData, meta interface{}) error {
76+
repository := d.Get("repository").(string)
77+
owner := d.Get("owner").(string)
78+
79+
client := meta.(*Owner).v3client
80+
ctx := context.Background()
81+
82+
var err error
83+
var asset *github.ReleaseAsset
84+
85+
assetID := int64(d.Get("asset_id").(int))
86+
asset, _, err = client.Repositories.GetReleaseAsset(ctx, owner, repository, assetID)
87+
if err != nil {
88+
return err
89+
}
90+
91+
var respBody io.ReadCloser
92+
respBody, _, err = client.Repositories.DownloadReleaseAsset(ctx, owner, repository, assetID, http.DefaultClient)
93+
if err != nil {
94+
return err
95+
}
96+
97+
buf := new(strings.Builder)
98+
_, err = io.Copy(buf, respBody)
99+
if err != nil {
100+
return err
101+
}
102+
103+
d.SetId(strconv.FormatInt(asset.GetID(), 10))
104+
err = d.Set("body", buf.String())
105+
if err != nil {
106+
return err
107+
}
108+
err = d.Set("url", asset.URL)
109+
if err != nil {
110+
return err
111+
}
112+
err = d.Set("node_id", asset.NodeID)
113+
if err != nil {
114+
return err
115+
}
116+
err = d.Set("name", asset.Name)
117+
if err != nil {
118+
return err
119+
}
120+
err = d.Set("label", asset.Label)
121+
if err != nil {
122+
return err
123+
}
124+
err = d.Set("content_type", asset.ContentType)
125+
if err != nil {
126+
return err
127+
}
128+
err = d.Set("size", asset.Size)
129+
if err != nil {
130+
return err
131+
}
132+
err = d.Set("created_at", asset.CreatedAt.String())
133+
if err != nil {
134+
return err
135+
}
136+
err = d.Set("created_at", asset.UpdatedAt.String())
137+
if err != nil {
138+
return err
139+
}
140+
err = d.Set("browser_download_url", asset.BrowserDownloadURL)
141+
if err != nil {
142+
return err
143+
}
144+
145+
return nil
146+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccGithubReleaseAssetDataSource(t *testing.T) {
12+
13+
testReleaseRepository := os.Getenv("GITHUB_TEMPLATE_REPOSITORY")
14+
testReleaseAssetID := os.Getenv("GITHUB_TEMPLATE_REPOSITORY_RELEASE_ASSET_ID")
15+
testReleaseAssetName := os.Getenv("GITHUB_TEMPLATE_REPOSITORY_RELEASE_ASSET_NAME")
16+
testReleaseOwner := testOrganizationFunc()
17+
18+
t.Run("queries specified asset ID", func(t *testing.T) {
19+
20+
config := fmt.Sprintf(`
21+
data "github_release_asset" "test" {
22+
repository = "%s"
23+
owner = "%s"
24+
asset_id = "%s"
25+
}
26+
`, testReleaseRepository, testReleaseOwner, testReleaseAssetID)
27+
28+
check := resource.ComposeTestCheckFunc(
29+
resource.TestCheckResourceAttr(
30+
"data.github_release_asset.test", "asset_id", testReleaseAssetID,
31+
),
32+
resource.TestCheckResourceAttr(
33+
"data.github_release_asset.test", "name", testReleaseAssetName,
34+
),
35+
)
36+
37+
testCase := func(t *testing.T, mode string) {
38+
resource.Test(t, resource.TestCase{
39+
PreCheck: func() { skipUnlessMode(t, mode) },
40+
Providers: testAccProviders,
41+
Steps: []resource.TestStep{
42+
{
43+
Config: config,
44+
Check: check,
45+
},
46+
},
47+
})
48+
}
49+
50+
t.Run("with an anonymous account", func(t *testing.T) {
51+
testCase(t, anonymous)
52+
})
53+
54+
t.Run("with an individual account", func(t *testing.T) {
55+
testCase(t, individual)
56+
})
57+
58+
t.Run("with an organization account", func(t *testing.T) {
59+
testCase(t, organization)
60+
})
61+
62+
})
63+
64+
}

github/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ func Provider() *schema.Provider {
237237
"github_organization_webhooks": dataSourceGithubOrganizationWebhooks(),
238238
"github_ref": dataSourceGithubRef(),
239239
"github_release": dataSourceGithubRelease(),
240+
"github_release_asset": dataSourceGithubReleaseAsset(),
240241
"github_repositories": dataSourceGithubRepositories(),
241242
"github_repository": dataSourceGithubRepository(),
242243
"github_repository_autolink_references": dataSourceGithubRepositoryAutolinkReferences(),
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
layout: "github"
3+
page_title: "GitHub: github_release_asset"
4+
description: |-
5+
Get information on a GitHub release asset.
6+
---
7+
8+
# github\_release\_asset
9+
10+
Use this data source to retrieve information about a GitHub release asset associated with a specific GitHub release.
11+
12+
## Example Usage
13+
To retrieve the latest release that is present in a repository:
14+
15+
```hcl
16+
data "github_release" "example" {
17+
repository = "example-repository"
18+
owner = "example-owner"
19+
retrieve_by = "latest"
20+
}
21+
```
22+
23+
To retrieve a specific release asset from a repository based on its ID:
24+
25+
```hcl
26+
data "github_release_asset" "example" {
27+
repository = "example-repository"
28+
owner = "example-owner"
29+
asset_id = 12345
30+
}
31+
```
32+
33+
To retrieve the first release asset associated with the the latest release in a repository:
34+
35+
```hcl
36+
data "github_release" "example" {
37+
repository = "example-repository"
38+
owner = "example-owner"
39+
retrieve_by = "latest"
40+
}
41+
42+
data "github_release_asset" "example" {
43+
repository = "example-repository"
44+
owner = "example-owner"
45+
asset_id = data.github_release.example.assets[0].id
46+
}
47+
```
48+
49+
To retrieve all release assets associated with the the latest release in a repository:
50+
51+
```hcl
52+
data "github_release" "example" {
53+
repository = "example-repository"
54+
owner = "example-owner"
55+
retrieve_by = "latest"
56+
}
57+
58+
data "github_release_asset" "example" {
59+
count = length(data.github_release.example.assets)
60+
repository = "example-repository"
61+
owner = "example-owner"
62+
asset_id = data.github_release.example.assets[count.index].id
63+
}
64+
```
65+
66+
## Argument Reference
67+
68+
* `repository` - (Required) Name of the repository to retrieve the release from.
69+
* `owner` - (Required) Owner of the repository.
70+
* `asset_id` - (Required) ID of the release asset to retrieve.
71+
72+
## Attributes Reference
73+
74+
* `id` - ID of the asset
75+
* `url` - URL of the asset
76+
* `node_id` - Node ID of the asset
77+
* `name` - The file name of the asset
78+
* `label` - Label for the asset
79+
* `content_type` - MIME type of the asset
80+
* `size` - Size in byte
81+
* `created_at` - Date the asset was created
82+
* `updated_at` - Date the asset was last updated
83+
* `browser_download_url` - Browser download URL
84+
* `body` - The release asset body

0 commit comments

Comments
 (0)