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

     1  package services
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"net/http"
     7  	"path"
     8  	"strconv"
     9  	"strings"
    10  	"time"
    11  
    12  	rthttpclient "github.com/cobalt77/jfrog-client-go/artifactory/httpclient"
    13  	"github.com/cobalt77/jfrog-client-go/artifactory/services/utils"
    14  	"github.com/cobalt77/jfrog-client-go/auth"
    15  	clientutils "github.com/cobalt77/jfrog-client-go/utils"
    16  	"github.com/cobalt77/jfrog-client-go/utils/errorutils"
    17  	"github.com/cobalt77/jfrog-client-go/utils/log"
    18  )
    19  
    20  type DiscardBuildsService struct {
    21  	client     *rthttpclient.ArtifactoryHttpClient
    22  	ArtDetails auth.ServiceDetails
    23  }
    24  
    25  func NewDiscardBuildsService(client *rthttpclient.ArtifactoryHttpClient) *DiscardBuildsService {
    26  	return &DiscardBuildsService{client: client}
    27  }
    28  
    29  func (ds *DiscardBuildsService) DiscardBuilds(params DiscardBuildsParams) error {
    30  	log.Info("Discarding builds...")
    31  
    32  	discardUrl := ds.ArtDetails.GetUrl()
    33  	restApi := path.Join("api/build/retention/", params.GetBuildName())
    34  	requestFullUrl, err := utils.BuildArtifactoryUrl(discardUrl, restApi, make(map[string]string))
    35  	if err != nil {
    36  		return err
    37  	}
    38  	requestFullUrl += "?async=" + strconv.FormatBool(params.IsAsync())
    39  
    40  	var excludeBuilds []string
    41  	if params.GetExcludeBuilds() != "" {
    42  		excludeBuilds = strings.Split(params.GetExcludeBuilds(), ",")
    43  	}
    44  
    45  	minimumBuildDateString, err := "", nil
    46  	if params.GetMaxDays() != "" {
    47  		minimumBuildDateString, err = calculateMinimumBuildDate(time.Now(), params.GetMaxDays())
    48  		if err != nil {
    49  			return err
    50  		}
    51  	}
    52  
    53  	data := DiscardBuildsBody{
    54  		ExcludeBuilds:    excludeBuilds,
    55  		MinimumBuildDate: minimumBuildDateString,
    56  		MaxBuilds:        params.GetMaxBuilds(),
    57  		DeleteArtifacts:  params.IsDeleteArtifacts()}
    58  	requestContent, err := json.Marshal(data)
    59  	if err != nil {
    60  		return errorutils.CheckError(err)
    61  	}
    62  
    63  	httpClientsDetails := ds.getArtifactoryDetails().CreateHttpClientDetails()
    64  	utils.SetContentType("application/json", &httpClientsDetails.Headers)
    65  
    66  	resp, body, err := ds.client.SendPost(requestFullUrl, requestContent, &httpClientsDetails)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	if resp.StatusCode != http.StatusNoContent {
    71  		return errorutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + clientutils.IndentJson(body)))
    72  	}
    73  
    74  	if params.IsAsync() {
    75  		log.Info("Builds are being discarded asynchronously.")
    76  		return nil
    77  	}
    78  
    79  	log.Info("Builds discarded.")
    80  	return nil
    81  }
    82  
    83  func calculateMinimumBuildDate(startingDate time.Time, maxDaysString string) (string, error) {
    84  	maxDays, err := strconv.Atoi(maxDaysString)
    85  	if err != nil {
    86  		return "", errorutils.CheckError(err)
    87  	}
    88  
    89  	minimumBuildDate := startingDate.Add(-24 * time.Hour * (time.Duration(maxDays)))
    90  	minimumBuildDateString := minimumBuildDate.Format("2006-01-02T15:04:05.000-0700")
    91  
    92  	return minimumBuildDateString, nil
    93  }
    94  
    95  func (ds *DiscardBuildsService) getArtifactoryDetails() auth.ServiceDetails {
    96  	return ds.ArtDetails
    97  }
    98  
    99  type DiscardBuildsBody struct {
   100  	MinimumBuildDate string   `json:"minimumBuildDate,omitempty"`
   101  	MaxBuilds        string   `json:"count,omitempty"`
   102  	ExcludeBuilds    []string `json:"buildNumbersNotToBeDiscarded,omitempty"`
   103  	DeleteArtifacts  bool     `json:"deleteBuildArtifacts"`
   104  }
   105  
   106  type DiscardBuildsParams struct {
   107  	DeleteArtifacts bool
   108  	BuildName       string
   109  	MaxDays         string
   110  	MaxBuilds       string
   111  	ExcludeBuilds   string
   112  	Async           bool
   113  }
   114  
   115  func (bd *DiscardBuildsParams) GetBuildName() string {
   116  	return bd.BuildName
   117  }
   118  
   119  func (bd *DiscardBuildsParams) GetMaxDays() string {
   120  	return bd.MaxDays
   121  }
   122  
   123  func (bd *DiscardBuildsParams) GetMaxBuilds() string {
   124  	return bd.MaxBuilds
   125  }
   126  
   127  func (bd *DiscardBuildsParams) GetExcludeBuilds() string {
   128  	return bd.ExcludeBuilds
   129  }
   130  
   131  func (bd *DiscardBuildsParams) IsDeleteArtifacts() bool {
   132  	return bd.DeleteArtifacts
   133  }
   134  
   135  func (bd *DiscardBuildsParams) IsAsync() bool {
   136  	return bd.Async
   137  }
   138  
   139  func NewDiscardBuildsParams() DiscardBuildsParams {
   140  	return DiscardBuildsParams{}
   141  }