From e9a0b7953326a6c1cf9571e53b0c355c844ff205 Mon Sep 17 00:00:00 2001 From: Steven Guiheux Date: Tue, 29 Aug 2023 10:02:28 +0200 Subject: [PATCH 1/2] feat(contrib): manage promote/release cargo crate --- .../integrations/artifactory/artifactory.go | 24 +++++++++++++++---- .../plugin-artifactory-release/main.go | 7 +++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/contrib/integrations/artifactory/artifactory.go b/contrib/integrations/artifactory/artifactory.go index 6755c89a9b..9f4cc1ad02 100644 --- a/contrib/integrations/artifactory/artifactory.go +++ b/contrib/integrations/artifactory/artifactory.go @@ -70,8 +70,17 @@ func CreateArtifactoryClient(ctx context.Context, url, token string) (artifactor } 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) + var srcRepo, targetRepo string + switch data.RepoType { + case "cargo": + repoParts := strings.Split(data.RepoName, "-") + srcRepo = fmt.Sprintf("%s-%s", strings.Join(repoParts[:len(repoParts)-1], "-"), lowMaturity) + targetRepo = fmt.Sprintf("%s-%s", strings.Join(repoParts[:len(repoParts)-1], "-"), highMaturity) + default: + srcRepo = fmt.Sprintf("%s-%s", data.RepoName, lowMaturity) + targetRepo = fmt.Sprintf("%s-%s", data.RepoName, highMaturity) + } + params := services.NewMoveCopyParams() params.Pattern = fmt.Sprintf("%s/%s", srcRepo, data.Path) params.Target = fmt.Sprintf("%s/%s", targetRepo, data.Path) @@ -170,7 +179,7 @@ func PromoteDockerImage(ctx context.Context, artiClient artifact_manager.Artifac } if props != nil { - if err := SetPropertiesRecursive(ctx, artiClient, data.RepoName, highMaturity, data.Path, props); err != nil { + if err := SetPropertiesRecursive(ctx, artiClient, data.RepoType, data.RepoName, highMaturity, data.Path, props); err != nil { return err } } @@ -334,7 +343,7 @@ func computeBuildInfoModules(ctx context.Context, artiClient artifact_manager.Ar props.AddProperty("build.number", execContext.version) props.AddProperty("build.timestamp", strconv.FormatInt(time.Now().Unix(), 10)) - if err := SetPropertiesRecursive(ctx, artiClient, data.RepoName, currentMaturity, data.Path, props); err != nil { + if err := SetPropertiesRecursive(ctx, artiClient, data.RepoType, data.RepoName, currentMaturity, data.Path, props); err != nil { return nil, err } @@ -342,7 +351,7 @@ func computeBuildInfoModules(ctx context.Context, artiClient artifact_manager.Ar return modules, nil } -func SetPropertiesRecursive(ctx context.Context, client artifact_manager.ArtifactManager, repoName string, maturity string, path string, props *utils.Properties) error { +func SetPropertiesRecursive(ctx context.Context, client artifact_manager.ArtifactManager, repoType string, repoName string, maturity string, path string, props *utils.Properties) error { ctx, end := telemetry.Span(ctx, "artifactory.SetPropertiesRecursive") defer end() if props == nil { @@ -350,7 +359,12 @@ func SetPropertiesRecursive(ctx context.Context, client artifact_manager.Artifac } repoSrc := repoName + if repoType == "cargo" { + repoParts := strings.Split(repoName, "-") + repoSrc = fmt.Sprintf("%s", strings.Join(repoParts[:len(repoParts)-1], "-")) + } repoSrc += "-" + maturity + log.Debug(ctx, "setting properties %+v on repoSrc:%s path:%s", props, repoSrc, path) _, endc := telemetry.Span(ctx, "artifactory.SetProperties", telemetry.Tag("repoSrc", repoSrc)) if err := client.SetProperties(repoSrc, path, props); err != nil { diff --git a/contrib/integrations/artifactory/plugin-artifactory-release/main.go b/contrib/integrations/artifactory/plugin-artifactory-release/main.go index 7e0264fb9f..9bc85a2ec4 100644 --- a/contrib/integrations/artifactory/plugin-artifactory-release/main.go +++ b/contrib/integrations/artifactory/plugin-artifactory-release/main.go @@ -173,7 +173,12 @@ func (e *artifactoryReleasePlugin) Run(ctx context.Context, opts *integrationplu 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)) + if rData.RepoType == "cargo" { + repoParts := strings.Split(rData.RepoName, "-") + promotedArtifacts = append(promotedArtifacts, fmt.Sprintf("%s-%s/%s", strings.Join(repoParts[:len(repoParts)-1], "-"), latestPromotion.ToMaturity, rData.Path)) + } else { + promotedArtifacts = append(promotedArtifacts, fmt.Sprintf("%s-%s/%s", rData.RepoName, latestPromotion.ToMaturity, rData.Path)) + } } } From d20c146bd363aa235b36abe47f41ea94630819b0 Mon Sep 17 00:00:00 2001 From: Guiheux Steven Date: Thu, 31 Aug 2023 09:55:28 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Yvonnick Esnault --- contrib/integrations/artifactory/artifactory.go | 1 + .../integrations/artifactory/plugin-artifactory-release/main.go | 1 + 2 files changed, 2 insertions(+) diff --git a/contrib/integrations/artifactory/artifactory.go b/contrib/integrations/artifactory/artifactory.go index 9f4cc1ad02..b5bd6bebff 100644 --- a/contrib/integrations/artifactory/artifactory.go +++ b/contrib/integrations/artifactory/artifactory.go @@ -70,6 +70,7 @@ func CreateArtifactoryClient(ctx context.Context, url, token string) (artifactor } func PromoteFile(artiClient artifact_manager.ArtifactManager, data sdk.WorkflowRunResultArtifactManager, lowMaturity, highMaturity string, props *utils.Properties, skipExistingArtifacts bool) error { + // artifactory does not manage virtual cargo repositories var srcRepo, targetRepo string switch data.RepoType { case "cargo": diff --git a/contrib/integrations/artifactory/plugin-artifactory-release/main.go b/contrib/integrations/artifactory/plugin-artifactory-release/main.go index 9bc85a2ec4..9ec9403df1 100644 --- a/contrib/integrations/artifactory/plugin-artifactory-release/main.go +++ b/contrib/integrations/artifactory/plugin-artifactory-release/main.go @@ -173,6 +173,7 @@ func (e *artifactoryReleasePlugin) Run(ctx context.Context, opts *integrationplu 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) } + // artifactory does not manage virtual cargo repositories if rData.RepoType == "cargo" { repoParts := strings.Split(rData.RepoName, "-") promotedArtifacts = append(promotedArtifacts, fmt.Sprintf("%s-%s/%s", strings.Join(repoParts[:len(repoParts)-1], "-"), latestPromotion.ToMaturity, rData.Path))