github.com/jfrog/jfrog-client-go@v1.40.2/distribution/services/create.go (about)

     1  package services
     2  
     3  import (
     4  	"encoding/json"
     5  	artifactoryUtils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
     6  	"github.com/jfrog/jfrog-client-go/auth"
     7  	distributionServiceUtils "github.com/jfrog/jfrog-client-go/distribution/services/utils"
     8  	"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
     9  	"github.com/jfrog/jfrog-client-go/utils"
    10  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    11  	"github.com/jfrog/jfrog-client-go/utils/log"
    12  	"net/http"
    13  )
    14  
    15  type CreateReleaseBundleService struct {
    16  	UpdateReleaseBundleService
    17  }
    18  
    19  func NewCreateReleaseBundleService(client *jfroghttpclient.JfrogHttpClient) *CreateReleaseBundleService {
    20  	return &CreateReleaseBundleService{UpdateReleaseBundleService{client: client}}
    21  }
    22  
    23  func (cb *CreateReleaseBundleService) GetDistDetails() auth.ServiceDetails {
    24  	return cb.DistDetails
    25  }
    26  
    27  func (cb *CreateReleaseBundleService) CreateReleaseBundle(createBundleParams CreateReleaseBundleParams) (*utils.Sha256Summary, error) {
    28  	releaseBundleBody, err := distributionServiceUtils.CreateBundleBody(createBundleParams.ReleaseBundleParams, cb.DryRun)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	body := &createReleaseBundleBody{
    34  		Name:              createBundleParams.Name,
    35  		Version:           createBundleParams.Version,
    36  		ReleaseBundleBody: *releaseBundleBody,
    37  	}
    38  
    39  	return cb.execCreateReleaseBundle(createBundleParams.GpgPassphrase, body)
    40  }
    41  
    42  // In case of an immediate sign- release bundle detailed summary (containing sha256) will be returned.
    43  // In other cases summary will be nil.
    44  func (cb *CreateReleaseBundleService) execCreateReleaseBundle(gpgPassphrase string, releaseBundle *createReleaseBundleBody) (*utils.Sha256Summary, error) {
    45  	var summary *utils.Sha256Summary = nil
    46  	if *releaseBundle.SignImmediately {
    47  		summary = utils.NewSha256Summary()
    48  	}
    49  	httpClientsDetails := cb.DistDetails.CreateHttpClientDetails()
    50  	content, err := json.Marshal(releaseBundle)
    51  	if err != nil {
    52  		return summary, errorutils.CheckError(err)
    53  	}
    54  	dryRunStr := ""
    55  	if releaseBundle.DryRun {
    56  		dryRunStr = "[Dry run] "
    57  	}
    58  	log.Info(dryRunStr + "Creating: " + releaseBundle.Name + "/" + releaseBundle.Version)
    59  
    60  	url := cb.DistDetails.GetUrl() + "api/v1/release_bundle"
    61  	distributionServiceUtils.AddGpgPassphraseHeader(gpgPassphrase, &httpClientsDetails.Headers)
    62  	artifactoryUtils.SetContentType("application/json", &httpClientsDetails.Headers)
    63  	resp, body, err := cb.client.SendPost(url, content, &httpClientsDetails)
    64  	if err != nil {
    65  		return summary, err
    66  	}
    67  	if !(resp.StatusCode == http.StatusCreated || (resp.StatusCode == http.StatusOK && releaseBundle.DryRun)) {
    68  		return summary, errorutils.CheckErrorf("Distribution response: " + resp.Status + "\n" + utils.IndentJson(body))
    69  	}
    70  	if summary != nil {
    71  		summary.SetSucceeded(true)
    72  		summary.SetSha256(resp.Header.Get("X-Checksum-Sha256"))
    73  	}
    74  
    75  	log.Debug("Distribution response:", resp.Status)
    76  	log.Debug(utils.IndentJson(body))
    77  	return summary, nil
    78  }
    79  
    80  type createReleaseBundleBody struct {
    81  	Name    string `json:"name"`
    82  	Version string `json:"version"`
    83  	distributionServiceUtils.ReleaseBundleBody
    84  }
    85  
    86  type CreateReleaseBundleParams struct {
    87  	distributionServiceUtils.ReleaseBundleParams
    88  }
    89  
    90  func NewCreateReleaseBundleParams(name, version string) CreateReleaseBundleParams {
    91  	return CreateReleaseBundleParams{
    92  		distributionServiceUtils.ReleaseBundleParams{
    93  			Name:    name,
    94  			Version: version,
    95  		},
    96  	}
    97  }