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