-
Notifications
You must be signed in to change notification settings - Fork 960
Expand file tree
/
Copy pathdata_source_github_release_asset.go
More file actions
184 lines (168 loc) · 4.76 KB
/
data_source_github_release_asset.go
File metadata and controls
184 lines (168 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package github
import (
"context"
"encoding/base64"
"io"
"net/http"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceGithubReleaseAsset() *schema.Resource {
return &schema.Resource{
Description: "Retrieve information about a GitHub release asset.",
ReadContext: dataSourceGithubReleaseAssetRead,
Schema: map[string]*schema.Schema{
"asset_id": {
Type: schema.TypeInt,
Required: true,
Description: "ID of the release asset to retrieve",
},
"owner": {
Type: schema.TypeString,
Required: true,
Description: "Owner of the repository",
},
"repository": {
Type: schema.TypeString,
Required: true,
Description: "Name of the repository to retrieve the release asset from",
},
"download_file_contents": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether to download the asset file content into the `file_contents` attribute",
},
"file_contents": {
Type: schema.TypeString,
Computed: true,
Description: "The base64-encoded release asset file contents (requires `download_file_contents` to be `true`)",
},
"url": {
Type: schema.TypeString,
Computed: true,
Description: "URL of the asset",
},
"node_id": {
Type: schema.TypeString,
Computed: true,
Description: "Node ID of the asset",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "File name of the asset",
},
"label": {
Type: schema.TypeString,
Computed: true,
Description: "Label for the asset",
},
"content_type": {
Type: schema.TypeString,
Computed: true,
Description: "MIME type of the asset",
},
"size": {
Type: schema.TypeInt,
Computed: true,
Description: "Asset size in bytes",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "Date the asset was created",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "Date the asset was updated",
},
"browser_download_url": {
Type: schema.TypeString,
Computed: true,
Description: "Browser URL from which the release asset can be downloaded",
},
},
}
}
func dataSourceGithubReleaseAssetRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
repository := d.Get("repository").(string)
owner := d.Get("owner").(string)
client := meta.(*Owner).v3client
assetID := int64(d.Get("asset_id").(int))
asset, _, err := client.Repositories.GetReleaseAsset(ctx, owner, repository, assetID)
if err != nil {
return diag.FromErr(err)
}
d.SetId(buildThreePartID(owner, repository, strconv.FormatInt(assetID, 10)))
if err := d.Set("url", asset.URL); err != nil {
return diag.FromErr(err)
}
if err := d.Set("node_id", asset.NodeID); err != nil {
return diag.FromErr(err)
}
if err := d.Set("name", asset.Name); err != nil {
return diag.FromErr(err)
}
if err := d.Set("label", asset.Label); err != nil {
return diag.FromErr(err)
}
if err := d.Set("content_type", asset.ContentType); err != nil {
return diag.FromErr(err)
}
if err := d.Set("size", asset.Size); err != nil {
return diag.FromErr(err)
}
if err := d.Set("created_at", asset.CreatedAt.String()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("updated_at", asset.UpdatedAt.String()); err != nil {
return diag.FromErr(err)
}
if err := d.Set("browser_download_url", asset.BrowserDownloadURL); err != nil {
return diag.FromErr(err)
}
if !d.Get("download_file_contents").(bool) {
return nil
}
// Use a client copy to avoid possible mutation of shared GitHub client state
// by client.Repositories.DownloadReleaseAsset.
clientCopy := client.Client()
req, err := http.NewRequestWithContext(ctx, "GET", asset.GetBrowserDownloadURL(), nil)
if err != nil {
return diag.FromErr(err)
}
resp, err := clientCopy.Do(req)
if err != nil {
return diag.FromErr(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return diag.Errorf("failed to get release asset (%s/%s %d): %s", owner, repository, assetID, resp.Status)
}
buf := new(strings.Builder)
encoder := base64.NewEncoder(base64.StdEncoding, buf)
defer encoder.Close()
buffer := make([]byte, 4096)
for {
n, err := resp.Body.Read(buffer)
if err != nil && err != io.EOF {
return diag.FromErr(err)
}
if n > 0 {
if _, err := encoder.Write(buffer[:n]); err != nil {
return diag.FromErr(err)
}
}
if err == io.EOF {
break
}
}
if err := d.Set("file_contents", buf.String()); err != nil {
return diag.FromErr(err)
}
return nil
}