From b2466926388ee72ca17f80ffb2c89b49f383ddd7 Mon Sep 17 00:00:00 2001 From: Richard LT Date: Wed, 11 Jan 2023 11:23:14 +0100 Subject: [PATCH] fix(contrib): artifactory promote always update artifacts (#6415) --- .../integrations/artifactory/artifactory.go | 39 +++++++++++-------- .../plugin-artifactory-promote/main.go | 4 +- .../plugin-artifactory-release/main.go | 4 +- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/contrib/integrations/artifactory/artifactory.go b/contrib/integrations/artifactory/artifactory.go index f55053fdd6..ea43d6798e 100644 --- a/contrib/integrations/artifactory/artifactory.go +++ b/contrib/integrations/artifactory/artifactory.go @@ -69,7 +69,7 @@ func CreateArtifactoryClient(ctx context.Context, url, token string) (artifactor return artifactory.New(serviceConfig) } -func PromoteFile(artiClient artifact_manager.ArtifactManager, data sdk.WorkflowRunResultArtifactManager, lowMaturity, highMaturity string, props *utils.Properties) error { +func PromoteFile(artiClient artifact_manager.ArtifactManager, data sdk.WorkflowRunResultArtifactManager, lowMaturity, highMaturity string, props *utils.Properties, skipExistingArtifacts bool) error { srcRepo := fmt.Sprintf("%s-%s", data.RepoName, lowMaturity) targetRepo := fmt.Sprintf("%s-%s", data.RepoName, highMaturity) params := services.NewMoveCopyParams() @@ -81,12 +81,15 @@ func PromoteFile(artiClient artifact_manager.ArtifactManager, data sdk.WorkflowR fmt.Printf("%s has been already promoted\n", data.Name) } else { // Check if artifact already exist on destination - exist, err := artiClient.CheckArtifactExists(targetRepo, data.Path) - if err != nil { - return err + var skipArtifact bool + if skipExistingArtifacts { + exist, err := artiClient.CheckArtifactExists(targetRepo, data.Path) + if err != nil { + return err + } + skipArtifact = exist } - - if !exist { + if !skipArtifact { // If source repository is a release repository, we should not move but copy the artifact // Get the properties of the source reposiytory maturity, err := artiClient.GetRepositoryMaturity(srcRepo) @@ -128,7 +131,7 @@ func PromoteFile(artiClient artifact_manager.ArtifactManager, data sdk.WorkflowR return nil } -func PromoteDockerImage(ctx context.Context, artiClient artifact_manager.ArtifactManager, data sdk.WorkflowRunResultArtifactManager, lowMaturity, highMaturity string, props *utils.Properties) error { +func PromoteDockerImage(ctx context.Context, artiClient artifact_manager.ArtifactManager, data sdk.WorkflowRunResultArtifactManager, lowMaturity, highMaturity string, props *utils.Properties, skipExistingArtifacts bool) error { sourceRepo := fmt.Sprintf("%s-%s", data.RepoName, lowMaturity) targetRepo := fmt.Sprintf("%s-%s", data.RepoName, highMaturity) params := services.NewDockerPromoteParams(data.Path, sourceRepo, targetRepo) @@ -136,17 +139,21 @@ func PromoteDockerImage(ctx context.Context, artiClient artifact_manager.Artifac if lowMaturity == highMaturity { fmt.Printf("%s has been already promoted\n", data.Name) } else { - maturity, err := artiClient.GetRepositoryMaturity(sourceRepo) - if err != nil { - fmt.Printf("Warning: unable to get repository maturity: %v\n", err) - } - // Check if artifact already exist on destination - exist, err := artiClient.CheckArtifactExists(targetRepo, data.Path) - if err != nil { - return err + var skipArtifact bool + if skipExistingArtifacts { + exist, err := artiClient.CheckArtifactExists(targetRepo, data.Path) + if err != nil { + return err + } + skipArtifact = exist } - if !exist { + if !skipArtifact { + maturity, err := artiClient.GetRepositoryMaturity(sourceRepo) + if err != nil { + fmt.Printf("Warning: unable to get repository maturity: %v\n", err) + } + if maturity == "release" { fmt.Printf("Copying docker image %s from %s to %s\n", data.Name, params.SourceRepo, params.TargetRepo) params.Copy = true diff --git a/contrib/integrations/artifactory/plugin-artifactory-promote/main.go b/contrib/integrations/artifactory/plugin-artifactory-promote/main.go index 7e1ae1904d..4c85cac0d5 100644 --- a/contrib/integrations/artifactory/plugin-artifactory-promote/main.go +++ b/contrib/integrations/artifactory/plugin-artifactory-promote/main.go @@ -124,11 +124,11 @@ func (e *artifactoryPromotePlugin) Run(ctx context.Context, opts *integrationplu } switch rData.RepoType { case "docker": - if err := art.PromoteDockerImage(ctx, artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props); err != nil { + if err := art.PromoteDockerImage(ctx, artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props, false); err != nil { return fail("unable to promote docker image: %s: %v", rData.Name+"-"+latestPromotion.ToMaturity, err) } default: - if err := art.PromoteFile(artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props); err != nil { + if err := art.PromoteFile(artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props, false); err != nil { return fail("unable to promote file: %s: %v", rData.Name, err) } } diff --git a/contrib/integrations/artifactory/plugin-artifactory-release/main.go b/contrib/integrations/artifactory/plugin-artifactory-release/main.go index d3d9a2a8cd..f89cd2193e 100644 --- a/contrib/integrations/artifactory/plugin-artifactory-release/main.go +++ b/contrib/integrations/artifactory/plugin-artifactory-release/main.go @@ -163,12 +163,12 @@ func (e *artifactoryReleasePlugin) Run(ctx context.Context, opts *integrationplu } switch rData.RepoType { case "docker": - if err := art.PromoteDockerImage(ctx, artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props); err != nil { + if err := art.PromoteDockerImage(ctx, artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props, true); err != nil { return fail("unable to promote docker image: %s: %v", rData.Name+"-"+latestPromotion.ToMaturity, err) } promotedArtifacts = append(promotedArtifacts, fmt.Sprintf("%s-%s/%s/manifest.json", rData.RepoName, latestPromotion.ToMaturity, rData.Path)) default: - if err := art.PromoteFile(artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props); err != nil { + if err := art.PromoteFile(artifactClient, rData, latestPromotion.FromMaturity, latestPromotion.ToMaturity, props, true); err != nil { return fail("unable to promote file: %s: %v", rData.Name, err) } promotedArtifacts = append(promotedArtifacts, fmt.Sprintf("%s-%s/%s", rData.RepoName, latestPromotion.ToMaturity, rData.Path))