github.com/cobalt77/jfrog-client-go@v0.14.5/artifactory/services/distribute.go (about) 1 package services 2 3 import ( 4 "encoding/json" 5 "errors" 6 "net/http" 7 "path" 8 "strings" 9 10 rthttpclient "github.com/cobalt77/jfrog-client-go/artifactory/httpclient" 11 "github.com/cobalt77/jfrog-client-go/artifactory/services/utils" 12 "github.com/cobalt77/jfrog-client-go/auth" 13 clientutils "github.com/cobalt77/jfrog-client-go/utils" 14 "github.com/cobalt77/jfrog-client-go/utils/errorutils" 15 "github.com/cobalt77/jfrog-client-go/utils/log" 16 ) 17 18 type DistributeService struct { 19 client *rthttpclient.ArtifactoryHttpClient 20 ArtDetails auth.ServiceDetails 21 DryRun bool 22 } 23 24 func NewDistributionService(client *rthttpclient.ArtifactoryHttpClient) *DistributeService { 25 return &DistributeService{client: client} 26 } 27 28 func (ds *DistributeService) getArtifactoryDetails() auth.ServiceDetails { 29 return ds.ArtDetails 30 } 31 32 func (ds *DistributeService) isDryRun() bool { 33 return ds.DryRun 34 } 35 36 func (ds *DistributeService) BuildDistribute(params BuildDistributionParams) error { 37 dryRun := "" 38 if ds.DryRun == true { 39 dryRun = "[Dry run] " 40 } 41 message := "Distributing build..." 42 log.Info(dryRun + message) 43 44 distributeUrl := ds.ArtDetails.GetUrl() 45 restApi := path.Join("api/build/distribute/", params.GetBuildName(), params.GetBuildNumber()) 46 requestFullUrl, err := utils.BuildArtifactoryUrl(distributeUrl, restApi, make(map[string]string)) 47 if err != nil { 48 return err 49 } 50 51 var sourceRepos []string 52 if params.GetSourceRepos() != "" { 53 sourceRepos = strings.Split(params.GetSourceRepos(), ",") 54 } 55 56 data := BuildDistributionBody{ 57 SourceRepos: sourceRepos, 58 TargetRepo: params.GetTargetRepo(), 59 Publish: params.IsPublish(), 60 OverrideExistingFiles: params.IsOverrideExistingFiles(), 61 GpgPassphrase: params.GetGpgPassphrase(), 62 Async: params.IsAsync(), 63 DryRun: ds.isDryRun()} 64 requestContent, err := json.Marshal(data) 65 if err != nil { 66 return errorutils.CheckError(err) 67 } 68 69 httpClientsDetails := ds.getArtifactoryDetails().CreateHttpClientDetails() 70 utils.SetContentType("application/json", &httpClientsDetails.Headers) 71 72 resp, body, err := ds.client.SendPost(requestFullUrl, requestContent, &httpClientsDetails) 73 if err != nil { 74 return err 75 } 76 if resp.StatusCode != http.StatusOK { 77 return errorutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + clientutils.IndentJson(body))) 78 } 79 80 log.Debug("Artifactory response:", resp.Status) 81 if params.IsAsync() && !ds.isDryRun() { 82 log.Info("Asynchronously distributed build", params.GetBuildName()+"/"+params.GetBuildNumber(), "to:", params.GetTargetRepo(), "repository, logs are available in Artifactory.") 83 return nil 84 } 85 86 log.Info(dryRun+"Distributed build", params.GetBuildName()+"/"+params.GetBuildNumber(), "to:", params.GetTargetRepo(), "repository.") 87 return nil 88 } 89 90 type BuildDistributionParams struct { 91 SourceRepos string 92 TargetRepo string 93 GpgPassphrase string 94 Publish bool 95 OverrideExistingFiles bool 96 Async bool 97 BuildName string 98 BuildNumber string 99 } 100 101 func (bd *BuildDistributionParams) GetSourceRepos() string { 102 return bd.SourceRepos 103 } 104 105 func (bd *BuildDistributionParams) GetTargetRepo() string { 106 return bd.TargetRepo 107 } 108 109 func (bd *BuildDistributionParams) GetGpgPassphrase() string { 110 return bd.GpgPassphrase 111 } 112 113 func (bd *BuildDistributionParams) IsAsync() bool { 114 return bd.Async 115 } 116 117 func (bd *BuildDistributionParams) IsPublish() bool { 118 return bd.Publish 119 } 120 121 func (bd *BuildDistributionParams) IsOverrideExistingFiles() bool { 122 return bd.OverrideExistingFiles 123 } 124 125 func (bd *BuildDistributionParams) GetBuildName() string { 126 return bd.BuildName 127 } 128 129 func (bd *BuildDistributionParams) GetBuildNumber() string { 130 return bd.BuildNumber 131 } 132 133 type BuildDistributionBody struct { 134 SourceRepos []string `json:"sourceRepos,omitempty"` 135 TargetRepo string `json:"targetRepo,omitempty"` 136 GpgPassphrase string `json:"gpgPassphrase,omitempty"` 137 Publish bool `json:"publish"` 138 OverrideExistingFiles bool `json:"overrideExistingFiles,omitempty"` 139 Async bool `json:"async,omitempty"` 140 DryRun bool `json:"dryRun,omitempty"` 141 } 142 143 func NewBuildDistributionParams() BuildDistributionParams { 144 return BuildDistributionParams{} 145 }