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

     1  package services
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  
     9  	"github.com/cobalt77/jfrog-client-go/artifactory/buildinfo"
    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 BuildInfoService struct {
    19  	client     *rthttpclient.ArtifactoryHttpClient
    20  	ArtDetails auth.ServiceDetails
    21  	DryRun     bool
    22  }
    23  
    24  func NewBuildInfoService(client *rthttpclient.ArtifactoryHttpClient) *BuildInfoService {
    25  	return &BuildInfoService{client: client}
    26  }
    27  
    28  func (bis *BuildInfoService) GetArtifactoryDetails() auth.ServiceDetails {
    29  	return bis.ArtDetails
    30  }
    31  
    32  func (bis *BuildInfoService) SetArtifactoryDetails(rt auth.ServiceDetails) {
    33  	bis.ArtDetails = rt
    34  }
    35  
    36  func (bis *BuildInfoService) GetJfrogHttpClient() (*rthttpclient.ArtifactoryHttpClient, error) {
    37  	return bis.client, nil
    38  }
    39  
    40  func (bis *BuildInfoService) IsDryRun() bool {
    41  	return bis.DryRun
    42  }
    43  
    44  type BuildInfoParams struct {
    45  	BuildName   string
    46  	BuildNumber string
    47  }
    48  
    49  func NewBuildInfoParams() BuildInfoParams {
    50  	return BuildInfoParams{}
    51  }
    52  
    53  func (bis *BuildInfoService) GetBuildInfo(params BuildInfoParams) (*buildinfo.BuildInfo, error) {
    54  	// Resolve LATEST build number from Artifactory if required.
    55  	name, number, err := utils.GetBuildNameAndNumberFromArtifactory(params.BuildName, params.BuildNumber, bis)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	// Get build-info json from Artifactory.
    61  	httpClientsDetails := bis.GetArtifactoryDetails().CreateHttpClientDetails()
    62  	buildInfoUrl := fmt.Sprintf("%sapi/build/%s/%s", bis.GetArtifactoryDetails().GetUrl(), name, number)
    63  	log.Debug("Getting build-info from: ", buildInfoUrl)
    64  	_, body, _, err := bis.client.SendGet(buildInfoUrl, true, &httpClientsDetails)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	// Build BuildInfo struct from json.
    70  	var buildInfoJson struct {
    71  		BuildInfo buildinfo.BuildInfo `json:"buildInfo,omitempty"`
    72  	}
    73  	if err := json.Unmarshal(body, &buildInfoJson); err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	return &buildInfoJson.BuildInfo, nil
    78  }
    79  
    80  func (bis *BuildInfoService) PublishBuildInfo(build *buildinfo.BuildInfo) error {
    81  	content, err := json.Marshal(build)
    82  	if errorutils.CheckError(err) != nil {
    83  		return err
    84  	}
    85  	if bis.IsDryRun() {
    86  		log.Info("[Dry run] Logging Build info preview...")
    87  		log.Output(clientutils.IndentJson(content))
    88  		return nil
    89  	}
    90  	httpClientsDetails := bis.GetArtifactoryDetails().CreateHttpClientDetails()
    91  	utils.SetContentType("application/vnd.org.jfrog.artifactory+json", &httpClientsDetails.Headers)
    92  	log.Info("Deploying build info...")
    93  	resp, body, err := bis.client.SendPut(bis.ArtDetails.GetUrl()+"api/build/", content, &httpClientsDetails)
    94  	if err != nil {
    95  		return err
    96  	}
    97  	if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK {
    98  		return errorutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + clientutils.IndentJson(body)))
    99  	}
   100  
   101  	log.Debug("Artifactory response:", resp.Status)
   102  	log.Info("Build info successfully deployed. Browse it in Artifactory under " + bis.GetArtifactoryDetails().GetUrl() + "webapp/builds/" + build.Name + "/" + build.Number)
   103  	return nil
   104  }