Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Artifactory Release Lifecycle Management - Support release bundles deletion #1136

Merged
merged 3 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ require (
gopkg.in/warnings.v0 v0.1.2 // indirect
)

replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240222135341-3cea121c58c7
replace github.com/jfrog/jfrog-client-go => github.com/RobiNino/jfrog-client-go v0.0.0-20240225133441-cd1999c6fa5d

replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20240222124058-bd9687a8666e

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0kC2U78=
github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/RobiNino/jfrog-client-go v0.0.0-20240225133441-cd1999c6fa5d h1:HFpWj5aRjWNnWLOtCSh/mcjspuIAZqdQvPNxhVvnT6E=
github.com/RobiNino/jfrog-client-go v0.0.0-20240225133441-cd1999c6fa5d/go.mod h1:jcZYTyo9H4GtZ6eAYIfKm1ulxeTbshcBBA+YUbWlHNc=
github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow=
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
Expand Down Expand Up @@ -87,8 +89,6 @@ github.com/jfrog/build-info-go v1.8.9-0.20240222124058-bd9687a8666e h1:NzB2yvEoj
github.com/jfrog/build-info-go v1.8.9-0.20240222124058-bd9687a8666e/go.mod h1:QHcKuesY4MrBVBuEwwBz4uIsX6mwYuMEDV09ng4AvAU=
github.com/jfrog/gofrog v1.6.0 h1:jOwb37nHY2PnxePNFJ6e6279Pgkr3di05SbQQw47Mq8=
github.com/jfrog/gofrog v1.6.0/go.mod h1:SZ1EPJUruxrVGndOzHd+LTiwWYKMlHqhKD+eu+v5Hqg=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240222135341-3cea121c58c7 h1:Xzul3R5zkiAGnay3YumuP0QjqSMXR59wdow+vIy8TN4=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240222135341-3cea121c58c7/go.mod h1:jcZYTyo9H4GtZ6eAYIfKm1ulxeTbshcBBA+YUbWlHNc=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
Expand Down
1 change: 0 additions & 1 deletion lifecycle/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type releaseBundleCmd struct {
serverDetails *config.ServerDetails
releaseBundleName string
releaseBundleVersion string
signingKeyName string
sync bool
rbProjectKey string
}
Expand Down
1 change: 1 addition & 0 deletions lifecycle/createcommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type ReleaseBundleCreateCommand struct {
releaseBundleCmd
signingKeyName string
buildsSpecPath string
releaseBundlesSpecPath string
}
Expand Down
132 changes: 132 additions & 0 deletions lifecycle/deletelocal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package lifecycle

import (
"errors"
"fmt"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-client-go/lifecycle"
"github.com/jfrog/jfrog-client-go/lifecycle/services"
"github.com/jfrog/jfrog-client-go/utils/log"
"strings"
)

type ReleaseBundleDeleteCommand struct {
releaseBundleCmd
environment string
quiet bool
}

func NewReleaseBundleDeleteCommand() *ReleaseBundleDeleteCommand {
return &ReleaseBundleDeleteCommand{}
}

func (rbd *ReleaseBundleDeleteCommand) SetServerDetails(serverDetails *config.ServerDetails) *ReleaseBundleDeleteCommand {
rbd.serverDetails = serverDetails
return rbd
}

func (rbd *ReleaseBundleDeleteCommand) SetReleaseBundleName(releaseBundleName string) *ReleaseBundleDeleteCommand {
rbd.releaseBundleName = releaseBundleName
return rbd
}

func (rbd *ReleaseBundleDeleteCommand) SetReleaseBundleVersion(releaseBundleVersion string) *ReleaseBundleDeleteCommand {
rbd.releaseBundleVersion = releaseBundleVersion
return rbd
}

func (rbd *ReleaseBundleDeleteCommand) SetSync(sync bool) *ReleaseBundleDeleteCommand {
rbd.sync = sync
return rbd
}

func (rbd *ReleaseBundleDeleteCommand) SetReleaseBundleProject(rbProjectKey string) *ReleaseBundleDeleteCommand {
rbd.rbProjectKey = rbProjectKey
return rbd
}

func (rbd *ReleaseBundleDeleteCommand) SetEnvironment(environment string) *ReleaseBundleDeleteCommand {
rbd.environment = environment
return rbd
}

func (rbd *ReleaseBundleDeleteCommand) SetQuiet(quiet bool) *ReleaseBundleDeleteCommand {
rbd.quiet = quiet
return rbd
}

func (rbd *ReleaseBundleDeleteCommand) CommandName() string {
return "rb_delete"
}

func (rbd *ReleaseBundleDeleteCommand) ServerDetails() (*config.ServerDetails, error) {
return rbd.serverDetails, nil
}

func (rbd *ReleaseBundleDeleteCommand) Run() error {
if err := validateArtifactoryVersionSupported(rbd.serverDetails); err != nil {
return err
}

servicesManager, rbDetails, queryParams, err := rbd.getPrerequisites()
if err != nil {
return err
}

if rbd.environment != "" {
return rbd.deletePromotionsOnly(servicesManager, rbDetails, queryParams)
}
return rbd.deleteLocalReleaseBundle(servicesManager, rbDetails, queryParams)
}

func (rbd *ReleaseBundleDeleteCommand) deletePromotionsOnly(servicesManager *lifecycle.LifecycleServicesManager,
rbDetails services.ReleaseBundleDetails, commonQueryParams services.CommonOptionalQueryParams) error {

deletionSubject := fmt.Sprintf("all promotions to environment '%s' of release bundle '%s/%s'", rbd.environment, rbd.releaseBundleName, rbd.releaseBundleVersion)
if !rbd.confirmDelete(deletionSubject) {
return nil
}

optionalQueryParams := services.GetPromotionsOptionalQueryParams{ProjectKey: commonQueryParams.ProjectKey}
response, err := servicesManager.GetReleaseBundleVersionPromotions(rbDetails, optionalQueryParams)
if err != nil {
return err
}
success := 0
fail := 0
for _, promotion := range response.Promotions {
if strings.EqualFold(promotion.Environment, rbd.environment) {
if curErr := servicesManager.DeleteReleaseBundleVersionPromotion(rbDetails, commonQueryParams, promotion.CreatedMillis.String()); curErr != nil {
err = errors.Join(err, curErr)
fail++
} else {
success++
}
}
}
if success == 0 && fail == 0 {
log.Info(fmt.Sprintf("No promotions were found for environment '%s'", rbd.environment))
} else {
log.Info(fmt.Sprintf("Promotions deleted successfuly: %d, failed: %d", success, fail))
RobiNino marked this conversation as resolved.
Show resolved Hide resolved
}

return err
}

func (rbd *ReleaseBundleDeleteCommand) deleteLocalReleaseBundle(servicesManager *lifecycle.LifecycleServicesManager,
rbDetails services.ReleaseBundleDetails, queryParams services.CommonOptionalQueryParams) error {
deletionSubject := fmt.Sprintf("release bundle '%s/%s' locally with all its promotions", rbd.releaseBundleName, rbd.releaseBundleVersion)
if !rbd.confirmDelete(deletionSubject) {
return nil
}
return servicesManager.DeleteReleaseBundleVersion(rbDetails, queryParams)
}

func (rbd *ReleaseBundleDeleteCommand) confirmDelete(deletionSubject string) bool {
if rbd.quiet {
return true
}
return coreutils.AskYesNo(
fmt.Sprintf("Are you sure you want to delete %s?\n"+avoidConfirmationMsg, deletionSubject), false)
}
159 changes: 159 additions & 0 deletions lifecycle/deleteremote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package lifecycle

import (
"encoding/json"
"fmt"
"github.com/jfrog/jfrog-cli-core/v2/common/spec"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-client-go/lifecycle"
"github.com/jfrog/jfrog-client-go/lifecycle/services"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/distribution"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
)

const avoidConfirmationMsg = "You can avoid this confirmation message by adding --quiet to the command."

type ReleaseBundleRemoteDeleteCommand struct {
releaseBundleCmd
distributionRules *spec.DistributionRules
dryRun bool
quiet bool
maxWaitMinutes int
}

func NewReleaseBundleRemoteDeleteCommand() *ReleaseBundleRemoteDeleteCommand {
return &ReleaseBundleRemoteDeleteCommand{}
}

func (rbd *ReleaseBundleRemoteDeleteCommand) SetServerDetails(serverDetails *config.ServerDetails) *ReleaseBundleRemoteDeleteCommand {
rbd.serverDetails = serverDetails
return rbd
}

func (rbd *ReleaseBundleRemoteDeleteCommand) SetReleaseBundleName(releaseBundleName string) *ReleaseBundleRemoteDeleteCommand {
rbd.releaseBundleName = releaseBundleName
return rbd
}

func (rbd *ReleaseBundleRemoteDeleteCommand) SetReleaseBundleVersion(releaseBundleVersion string) *ReleaseBundleRemoteDeleteCommand {
rbd.releaseBundleVersion = releaseBundleVersion
return rbd
}

func (rbd *ReleaseBundleRemoteDeleteCommand) SetSync(sync bool) *ReleaseBundleRemoteDeleteCommand {
rbd.sync = sync
return rbd
}

func (rbd *ReleaseBundleRemoteDeleteCommand) SetReleaseBundleProject(rbProjectKey string) *ReleaseBundleRemoteDeleteCommand {
rbd.rbProjectKey = rbProjectKey
return rbd
}

func (rbd *ReleaseBundleRemoteDeleteCommand) SetDistributionRules(distributionRules *spec.DistributionRules) *ReleaseBundleRemoteDeleteCommand {
rbd.distributionRules = distributionRules
return rbd
}

func (rbd *ReleaseBundleRemoteDeleteCommand) SetDryRun(dryRun bool) *ReleaseBundleRemoteDeleteCommand {
rbd.dryRun = dryRun
return rbd
}

func (rbd *ReleaseBundleRemoteDeleteCommand) SetQuiet(quiet bool) *ReleaseBundleRemoteDeleteCommand {
rbd.quiet = quiet
return rbd
}

func (rbd *ReleaseBundleRemoteDeleteCommand) SetMaxWaitMinutes(maxWaitMinutes int) *ReleaseBundleRemoteDeleteCommand {
rbd.maxWaitMinutes = maxWaitMinutes
return rbd
}

func (rbd *ReleaseBundleRemoteDeleteCommand) CommandName() string {
return "rb_remote_delete"
}

func (rbd *ReleaseBundleRemoteDeleteCommand) ServerDetails() (*config.ServerDetails, error) {
return rbd.serverDetails, nil
}

func (rbd *ReleaseBundleRemoteDeleteCommand) Run() error {
if err := validateArtifactoryVersionSupported(rbd.serverDetails); err != nil {
return err
}

servicesManager, rbDetails, queryParams, err := rbd.getPrerequisites()
if err != nil {
return err
}

return rbd.deleteRemote(servicesManager, rbDetails, queryParams)
}

func (rbd *ReleaseBundleRemoteDeleteCommand) deleteRemote(servicesManager *lifecycle.LifecycleServicesManager,
rbDetails services.ReleaseBundleDetails, queryParams services.CommonOptionalQueryParams) error {

confirm, err := rbd.confirmDelete()
if err != nil || !confirm {
return err
}

aggregatedRules := rbd.getAggregatedDistRules()

return servicesManager.RemoteDeleteReleaseBundle(rbDetails, services.ReleaseBundleRemoteDeleteParams{
DistributionRules: aggregatedRules,
DryRun: rbd.dryRun,
MaxWaitMinutes: rbd.maxWaitMinutes,
CommonOptionalQueryParams: queryParams,
})
}

func (rbd *ReleaseBundleRemoteDeleteCommand) distributionRulesEmpty() bool {
return rbd.distributionRules == nil ||
len(rbd.distributionRules.DistributionRules) == 0 ||
len(rbd.distributionRules.DistributionRules) == 1 && rbd.distributionRules.DistributionRules[0].IsEmpty()
}

func (rbd *ReleaseBundleRemoteDeleteCommand) confirmDelete() (bool, error) {
if rbd.quiet {
return true, nil
}

message := fmt.Sprintf("Are you sure you want to delete the release bundle '%s/%s' remotely ", rbd.releaseBundleName, rbd.releaseBundleVersion)
if rbd.distributionRulesEmpty() {
message += "from all edges?"
} else {
var distributionRulesBodies []distribution.DistributionRulesBody
for _, rule := range rbd.distributionRules.DistributionRules {
distributionRulesBodies = append(distributionRulesBodies, distribution.DistributionRulesBody{
SiteName: rule.SiteName,
CityName: rule.CityName,
CountryCodes: rule.CountryCodes,
})
}
bytes, err := json.Marshal(distributionRulesBodies)
if err != nil {
return false, errorutils.CheckError(err)
}

log.Output(clientutils.IndentJson(bytes))
message += "from all edges with the above distribution rules?"
}

return coreutils.AskYesNo(message+"\n"+avoidConfirmationMsg, false), nil
}

func (rbd *ReleaseBundleRemoteDeleteCommand) getAggregatedDistRules() (aggregatedRules []*distribution.DistributionCommonParams) {
if rbd.distributionRulesEmpty() {
aggregatedRules = append(aggregatedRules, &distribution.DistributionCommonParams{SiteName: "*"})
} else {
for _, rules := range rbd.distributionRules.DistributionRules {
aggregatedRules = append(aggregatedRules, rules.ToDistributionCommonParams())
}
}
return
}
9 changes: 8 additions & 1 deletion lifecycle/distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ func (rbd *ReleaseBundleDistributeCommand) Run() error {
Target: rbd.pathMappingTarget,
}

return servicesManager.DistributeReleaseBundle(rbd.distributeBundlesParams, rbd.autoCreateRepo, pathMapping)
return servicesManager.DistributeReleaseBundle(services.ReleaseBundleDetails{
ReleaseBundleName: rbd.distributeBundlesParams.Name,
ReleaseBundleVersion: rbd.distributeBundlesParams.Version,
}, services.DistributeReleaseBundleParams{
AutoCreateRepo: rbd.autoCreateRepo,
DistributionRules: rbd.distributeBundlesParams.DistributionRules,
PathMappings: []services.PathMapping{pathMapping},
})
}

func (rbd *ReleaseBundleDistributeCommand) ServerDetails() (*config.ServerDetails, error) {
Expand Down
1 change: 1 addition & 0 deletions lifecycle/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type ReleaseBundlePromoteCommand struct {
releaseBundleCmd
signingKeyName string
environment string
includeReposPatterns []string
excludeReposPatterns []string
Expand Down
Loading