MCPcopy Create free account
hub / github.com/cli/cli / uploadAsset

Function uploadAsset

pkg/cmd/release/shared/upload.go:157–208  ·  view source on GitHub ↗
(ctx context.Context, httpClient *http.Client, uploadURL safeurl.SafeURL, asset AssetForUpload)

Source from the content-addressed store, hash-verified

155}
156
157func uploadAsset(ctx context.Context, httpClient *http.Client, uploadURL safeurl.SafeURL, asset AssetForUpload) (*ReleaseAsset, error) {
158 u, err := url.Parse(uploadURL.String())
159 if err != nil {
160 return nil, err
161 }
162 params := u.Query()
163 params.Set("name", asset.Name)
164 params.Set("label", asset.Label)
165 u.RawQuery = params.Encode()
166
167 // Since u is derived from uploadURL, an already-trusted safeurl.SafeURL, the resulting URL is safe to declare as such.
168 safeURL := safeurl.NewImmutableSafeURL(u.String())
169
170 f, err := asset.Open()
171 if err != nil {
172 return nil, err
173 }
174 defer f.Close()
175
176 req, err := http.NewRequestWithContext(ctx, "POST", safeURL.String(), f)
177 if err != nil {
178 return nil, err
179 }
180 req.ContentLength = asset.Size
181 req.Header.Set("Content-Type", asset.MIMEType)
182 req.GetBody = asset.Open
183
184 // DoRequest rather than Request because ContentLength and GetBody are set on the request
185 // itself, and neither can be expressed as a header. The upload URL is supplied by the API
186 // and points at the uploads host, so there is no endpoint resolution to delegate here.
187 // TODO(api-client-rollout)
188 // This line of code is part of a mechanical roll out of the api client.
189 // As a follow up, consider whether the api client can be injected to this call site, rather than constructed
190 resp, err := api.NewClientFromHTTP(httpClient).DoRequest(req)
191 if err != nil {
192 // Only transport failures are retryable as network errors. A response that arrived and
193 // carried a failing status is reported as is, so shouldRetry can judge it by status.
194 if _, ok := errors.AsType[api.HTTPError](err); ok {
195 return nil, err
196 }
197 return nil, errNetwork{err}
198 }
199 defer resp.Body.Close()
200
201 var newAsset ReleaseAsset
202 dec := json.NewDecoder(resp.Body)
203 if err := dec.Decode(&newAsset); err != nil {
204 return nil, err
205 }
206
207 return &newAsset, nil
208}
209
210func deleteAsset(ctx context.Context, httpClient *http.Client, hostname string, assetURL safeurl.SafeURL) error {
211 // The asset URL is supplied by the API, so it is absolute and requested as given.

Callers 1

uploadWithDeleteFunction · 0.85

Calls 9

StringMethod · 0.95
NewImmutableSafeURLFunction · 0.92
NewClientFromHTTPFunction · 0.92
OpenMethod · 0.80
DoRequestMethod · 0.80
StringMethod · 0.65
QueryMethod · 0.65
SetMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected