forked from jfrog/jfrog-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildinfo.go
102 lines (87 loc) · 3.23 KB
/
buildinfo.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
package services
import (
"encoding/json"
"errors"
"fmt"
"github.com/jfrog/jfrog-client-go/artifactory/auth"
"github.com/jfrog/jfrog-client-go/artifactory/buildinfo"
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"
)
type BuildInfoService struct {
client *rthttpclient.ArtifactoryHttpClient
ArtDetails auth.ArtifactoryDetails
DryRun bool
}
func NewBuildInfoService(client *rthttpclient.ArtifactoryHttpClient) *BuildInfoService {
return &BuildInfoService{client: client}
}
func (bis *BuildInfoService) GetArtifactoryDetails() auth.ArtifactoryDetails {
return bis.ArtDetails
}
func (bis *BuildInfoService) SetArtifactoryDetails(rt auth.ArtifactoryDetails) {
bis.ArtDetails = rt
}
func (bis *BuildInfoService) GetJfrogHttpClient() (*rthttpclient.ArtifactoryHttpClient, error) {
return bis.client, nil
}
func (bis *BuildInfoService) IsDryRun() bool {
return bis.DryRun
}
type BuildInfoParams struct {
BuildName string
BuildNumber string
}
func NewBuildInfoParams() BuildInfoParams {
return BuildInfoParams{}
}
func (bis *BuildInfoService) GetBuildInfo(params BuildInfoParams) (*buildinfo.BuildInfo, error) {
// Resolve LATEST build number from Artifactory if required.
name, number, err := utils.GetBuildNameAndNumberFromArtifactory(params.BuildName, params.BuildNumber, bis)
if err != nil {
return nil, err
}
// Get build-info json from Artifactory.
httpClientsDetails := bis.GetArtifactoryDetails().CreateHttpClientDetails()
buildInfoUrl := fmt.Sprintf("%sapi/build/%s/%s", bis.GetArtifactoryDetails().GetUrl(), name, number)
log.Debug("Getting build-info from: ", buildInfoUrl)
_, body, _, err := bis.client.SendGet(buildInfoUrl, true, &httpClientsDetails)
if err != nil {
return nil, err
}
// Build BuildInfo struct from json.
var buildInfoJson struct {
BuildInfo buildinfo.BuildInfo `json:"buildInfo,omitempty"`
}
if err := json.Unmarshal(body, &buildInfoJson); err != nil {
return nil, err
}
return &buildInfoJson.BuildInfo, nil
}
func (bis *BuildInfoService) PublishBuildInfo(build *buildinfo.BuildInfo) error {
content, err := json.Marshal(build)
if errorutils.CheckError(err) != nil {
return err
}
if bis.IsDryRun() {
log.Output(clientutils.IndentJson(content))
return nil
}
httpClientsDetails := bis.GetArtifactoryDetails().CreateHttpClientDetails()
utils.SetContentType("application/vnd.org.jfrog.artifactory+json", &httpClientsDetails.Headers)
log.Info("Deploying build info...")
resp, body, err := bis.client.SendPut(bis.ArtDetails.GetUrl()+"api/build/", content, &httpClientsDetails)
if err != nil {
return err
}
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusNoContent {
return errorutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + clientutils.IndentJson(body)))
}
log.Debug("Artifactory response:", resp.Status)
log.Info("Build info successfully deployed. Browse it in Artifactory under " + bis.GetArtifactoryDetails().GetUrl() + "webapp/builds/" + build.Name + "/" + build.Number)
return nil
}