github.com/amane3/goreleaser@v0.182.0/internal/pipe/artifactory/artifactory.go (about)

     1  // Package artifactory provides a Pipe that push to artifactory
     2  package artifactory
     3  
     4  import (
     5  	"encoding/json"
     6  	"fmt"
     7  	"io/ioutil"
     8  	h "net/http"
     9  
    10  	"github.com/amane3/goreleaser/internal/http"
    11  	"github.com/amane3/goreleaser/internal/pipe"
    12  	"github.com/amane3/goreleaser/pkg/context"
    13  )
    14  
    15  // Pipe for Artifactory.
    16  type Pipe struct{}
    17  
    18  // String returns the description of the pipe.
    19  func (Pipe) String() string {
    20  	return "artifactory"
    21  }
    22  
    23  // Default sets the pipe defaults.
    24  func (Pipe) Default(ctx *context.Context) error {
    25  	for i := range ctx.Config.Artifactories {
    26  		ctx.Config.Artifactories[i].ChecksumHeader = "X-Checksum-SHA256"
    27  		ctx.Config.Artifactories[i].Method = h.MethodPut
    28  	}
    29  	return http.Defaults(ctx.Config.Artifactories)
    30  }
    31  
    32  // Publish artifacts to artifactory.
    33  //
    34  // Docs: https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API#ArtifactoryRESTAPI-Example-DeployinganArtifact
    35  func (Pipe) Publish(ctx *context.Context) error {
    36  	if len(ctx.Config.Artifactories) == 0 {
    37  		return pipe.Skip("artifactory section is not configured")
    38  	}
    39  
    40  	// Check requirements for every instance we have configured.
    41  	// If not fulfilled, we can skip this pipeline
    42  	for _, instance := range ctx.Config.Artifactories {
    43  		instance := instance
    44  		if skip := http.CheckConfig(ctx, &instance, "artifactory"); skip != nil {
    45  			return pipe.Skip(skip.Error())
    46  		}
    47  	}
    48  
    49  	return http.Upload(ctx, ctx.Config.Artifactories, "artifactory", checkResponse)
    50  }
    51  
    52  // An ErrorResponse reports one or more errors caused by an API request.
    53  type errorResponse struct {
    54  	Response *h.Response // HTTP response that caused this error
    55  	Errors   []Error     `json:"errors"` // more detail on individual errors
    56  }
    57  
    58  func (r *errorResponse) Error() string {
    59  	return fmt.Sprintf("%v %v: %d %+v",
    60  		r.Response.Request.Method, r.Response.Request.URL,
    61  		r.Response.StatusCode, r.Errors)
    62  }
    63  
    64  // An Error reports more details on an individual error in an ErrorResponse.
    65  type Error struct {
    66  	Status  int    `json:"status"`  // Error code
    67  	Message string `json:"message"` // Message describing the error.
    68  }
    69  
    70  // checkResponse checks the API response for errors, and returns them if
    71  // present. A response is considered an error if it has a status code outside
    72  // the 200 range.
    73  // API error responses are expected to have either no response
    74  // body, or a JSON response body that maps to ErrorResponse. Any other
    75  // response body will be silently ignored.
    76  func checkResponse(r *h.Response) error {
    77  	defer r.Body.Close()
    78  	if c := r.StatusCode; 200 <= c && c <= 299 {
    79  		return nil
    80  	}
    81  	errorResponse := &errorResponse{Response: r}
    82  	data, err := ioutil.ReadAll(r.Body)
    83  	if err == nil && data != nil {
    84  		err := json.Unmarshal(data, errorResponse)
    85  		if err != nil {
    86  			return err
    87  		}
    88  	}
    89  	return errorResponse
    90  }