mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-03-17 20:47:50 +02:00
fix: remove location header requirement on http put responses (#794)
This commit is contained in:
parent
59a56ce8c9
commit
33a25e05a9
@ -106,9 +106,8 @@ func misconfigured(kind string, upload *config.Put, reason string) error {
|
||||
}
|
||||
|
||||
// ResponseChecker is a function capable of validating an http server response.
|
||||
// It must return the location of the uploaded asset or the error when the
|
||||
// response must be considered a failure.
|
||||
type ResponseChecker func(*h.Response) (string, error)
|
||||
// It must return and error when the response must be considered a failure.
|
||||
type ResponseChecker func(*h.Response) error
|
||||
|
||||
// Upload does the actual uploading work
|
||||
func Upload(ctx *context.Context, puts []config.Put, kind string, check ResponseChecker) error {
|
||||
@ -194,7 +193,7 @@ func uploadAsset(ctx *context.Context, put config.Put, artifact artifact.Artifac
|
||||
}
|
||||
targetURL += artifact.Name
|
||||
|
||||
location, _, err := uploadAssetToServer(ctx, targetURL, username, secret, asset, check)
|
||||
_, err = uploadAssetToServer(ctx, targetURL, username, secret, asset, check)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("%s: upload failed", kind)
|
||||
log.WithError(err).WithFields(log.Fields{
|
||||
@ -207,24 +206,19 @@ func uploadAsset(ctx *context.Context, put config.Put, artifact artifact.Artifac
|
||||
log.WithFields(log.Fields{
|
||||
"instance": put.Name,
|
||||
"mode": put.Mode,
|
||||
"uri": location,
|
||||
}).Info("uploaded successful")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// uploadAssetToServer uploads the asset file to target
|
||||
func uploadAssetToServer(ctx *context.Context, target, username, secret string, a *asset, check ResponseChecker) (string, *h.Response, error) {
|
||||
func uploadAssetToServer(ctx *context.Context, target, username, secret string, a *asset, check ResponseChecker) (*h.Response, error) {
|
||||
req, err := newUploadRequest(target, username, secret, a)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
loc, resp, err := executeHTTPRequest(ctx, req, check)
|
||||
if err != nil {
|
||||
return "", resp, err
|
||||
}
|
||||
return loc, resp, nil
|
||||
return executeHTTPRequest(ctx, req, check)
|
||||
}
|
||||
|
||||
// newUploadRequest creates a new h.Request for uploading
|
||||
@ -245,30 +239,30 @@ func newUploadRequest(target, username, secret string, a *asset) (*h.Request, er
|
||||
}
|
||||
|
||||
// executeHTTPRequest processes the http call with respect of context ctx
|
||||
func executeHTTPRequest(ctx *context.Context, req *h.Request, check ResponseChecker) (string, *h.Response, error) {
|
||||
func executeHTTPRequest(ctx *context.Context, req *h.Request, check ResponseChecker) (*h.Response, error) {
|
||||
resp, err := h.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
// If we got an error, and the context has been canceled,
|
||||
// the context's error is probably more useful.
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return "", nil, ctx.Err()
|
||||
return nil, ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
return "", nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close() // nolint: errcheck
|
||||
|
||||
loc, err := check(resp)
|
||||
err = check(resp)
|
||||
if err != nil {
|
||||
// even though there was an error, we still return the response
|
||||
// in case the caller wants to inspect it further
|
||||
return "", resp, err
|
||||
return resp, err
|
||||
}
|
||||
|
||||
return loc, resp, err
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// targetData is used as a template struct for
|
||||
|
@ -222,11 +222,11 @@ func TestUpload(t *testing.T) {
|
||||
}, nil
|
||||
}
|
||||
defer assetOpenReset()
|
||||
var is2xx ResponseChecker = func(r *h.Response) (string, error) {
|
||||
var is2xx ResponseChecker = func(r *h.Response) error {
|
||||
if r.StatusCode/100 == 2 {
|
||||
return r.Header.Get("Location"), nil
|
||||
return nil
|
||||
}
|
||||
return "", errors.Errorf("unexpected http status code: %v", r.StatusCode)
|
||||
return errors.Errorf("unexpected http status code: %v", r.StatusCode)
|
||||
}
|
||||
ctx := context.New(config.Project{ProjectName: "blah"})
|
||||
ctx.Env["TEST_A_SECRET"] = "x"
|
||||
|
@ -64,13 +64,13 @@ func (Pipe) Run(ctx *context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
return http.Upload(ctx, ctx.Config.Artifactories, "artifactory", func(res *h.Response) (string, error) {
|
||||
return http.Upload(ctx, ctx.Config.Artifactories, "artifactory", func(res *h.Response) error {
|
||||
if err := checkResponse(res); err != nil {
|
||||
return "", err
|
||||
return err
|
||||
}
|
||||
var r artifactoryResponse
|
||||
err := json.NewDecoder(res.Body).Decode(&r)
|
||||
return r.DownloadURI, err
|
||||
return err
|
||||
})
|
||||
|
||||
}
|
||||
|
@ -38,15 +38,11 @@ func (Pipe) Run(ctx *context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
return http.Upload(ctx, ctx.Config.Puts, "put", func(res *h.Response) (string, error) {
|
||||
return http.Upload(ctx, ctx.Config.Puts, "put", func(res *h.Response) error {
|
||||
if c := res.StatusCode; c < 200 || 299 < c {
|
||||
return "", errors.Errorf("unexpected http response status: %s", res.Status)
|
||||
return errors.Errorf("unexpected http response status: %s", res.Status)
|
||||
}
|
||||
loc, err := res.Location()
|
||||
if err != nil {
|
||||
return "", errors.Errorf("getting http response location: %s", err)
|
||||
}
|
||||
return loc.String(), err
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user