github.com/cobalt77/jfrog-client-go@v0.14.5/artifactory/services/promote.go (about)

     1  package services
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"net/http"
     7  	"path"
     8  
     9  	rthttpclient "github.com/cobalt77/jfrog-client-go/artifactory/httpclient"
    10  	"github.com/cobalt77/jfrog-client-go/artifactory/services/utils"
    11  	"github.com/cobalt77/jfrog-client-go/auth"
    12  	clientutils "github.com/cobalt77/jfrog-client-go/utils"
    13  	"github.com/cobalt77/jfrog-client-go/utils/errorutils"
    14  	"github.com/cobalt77/jfrog-client-go/utils/log"
    15  )
    16  
    17  type PromoteService struct {
    18  	client     *rthttpclient.ArtifactoryHttpClient
    19  	ArtDetails auth.ServiceDetails
    20  	DryRun     bool
    21  }
    22  
    23  func NewPromotionService(client *rthttpclient.ArtifactoryHttpClient) *PromoteService {
    24  	return &PromoteService{client: client}
    25  }
    26  
    27  func (ps *PromoteService) isDryRun() bool {
    28  	return ps.DryRun
    29  }
    30  
    31  func (ps *PromoteService) BuildPromote(promotionParams PromotionParams) error {
    32  	message := "Promoting build..."
    33  	if ps.DryRun {
    34  		message = "[Dry run] " + message
    35  	}
    36  	log.Info(message)
    37  
    38  	promoteUrl := ps.ArtDetails.GetUrl()
    39  	restApi := path.Join("api/build/promote/", promotionParams.GetBuildName(), promotionParams.GetBuildNumber())
    40  	requestFullUrl, err := utils.BuildArtifactoryUrl(promoteUrl, restApi, make(map[string]string))
    41  	if err != nil {
    42  		return err
    43  	}
    44  	props, err := utils.ParseProperties(promotionParams.GetProperties(), utils.JoinCommas)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	data := BuildPromotionBody{
    50  		Status:              promotionParams.GetStatus(),
    51  		Comment:             promotionParams.GetComment(),
    52  		Copy:                promotionParams.IsCopy(),
    53  		IncludeDependencies: promotionParams.IsIncludeDependencies(),
    54  		SourceRepo:          promotionParams.GetSourceRepo(),
    55  		TargetRepo:          promotionParams.GetTargetRepo(),
    56  		DryRun:              ps.isDryRun(),
    57  		Properties:          props.ToBuildPromoteMap()}
    58  	requestContent, err := json.Marshal(data)
    59  	if err != nil {
    60  		return errorutils.CheckError(err)
    61  	}
    62  
    63  	httpClientsDetails := ps.ArtDetails.CreateHttpClientDetails()
    64  	utils.SetContentType("application/vnd.org.jfrog.artifactory.build.PromotionRequest+json", &httpClientsDetails.Headers)
    65  
    66  	resp, body, err := ps.client.SendPost(requestFullUrl, requestContent, &httpClientsDetails)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	if resp.StatusCode != http.StatusOK {
    72  		return errorutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + clientutils.IndentJson(body)))
    73  	}
    74  
    75  	log.Debug("Artifactory response:", resp.Status)
    76  	log.Info("Promoted build", promotionParams.GetBuildName()+"/"+promotionParams.GetBuildNumber(), "to:", promotionParams.GetTargetRepo(), "repository.")
    77  	return nil
    78  }
    79  
    80  type BuildPromotionBody struct {
    81  	Comment             string              `json:"comment,omitempty"`
    82  	SourceRepo          string              `json:"sourceRepo,omitempty"`
    83  	TargetRepo          string              `json:"targetRepo,omitempty"`
    84  	Status              string              `json:"status,omitempty"`
    85  	IncludeDependencies bool                `json:"dependencies,omitempty"`
    86  	Copy                bool                `json:"copy,omitempty"`
    87  	DryRun              bool                `json:"dryRun,omitempty"`
    88  	Properties          map[string][]string `json:"properties,omitempty"`
    89  }
    90  
    91  type PromotionParams struct {
    92  	BuildName           string
    93  	BuildNumber         string
    94  	TargetRepo          string
    95  	Status              string
    96  	Comment             string
    97  	Copy                bool
    98  	IncludeDependencies bool
    99  	SourceRepo          string
   100  	Properties          string
   101  }
   102  
   103  func (bp *PromotionParams) GetBuildName() string {
   104  	return bp.BuildName
   105  }
   106  
   107  func (bp *PromotionParams) GetBuildNumber() string {
   108  	return bp.BuildNumber
   109  }
   110  
   111  func (bp *PromotionParams) GetTargetRepo() string {
   112  	return bp.TargetRepo
   113  }
   114  
   115  func (bp *PromotionParams) GetStatus() string {
   116  	return bp.Status
   117  }
   118  
   119  func (bp *PromotionParams) GetComment() string {
   120  	return bp.Comment
   121  }
   122  
   123  func (bp *PromotionParams) IsCopy() bool {
   124  	return bp.Copy
   125  }
   126  
   127  func (bp *PromotionParams) IsIncludeDependencies() bool {
   128  	return bp.IncludeDependencies
   129  }
   130  
   131  func (bp *PromotionParams) GetSourceRepo() string {
   132  	return bp.SourceRepo
   133  }
   134  
   135  func (bp *PromotionParams) GetProperties() string {
   136  	return bp.Properties
   137  }
   138  
   139  func NewPromotionParams() PromotionParams {
   140  	return PromotionParams{}
   141  }