From b0ecbf68ad680eabde4d0212782f965e0591865e Mon Sep 17 00:00:00 2001 From: Vyacheslav Starostin <32613074+vstarostin@users.noreply.github.com> Date: Wed, 10 Apr 2024 13:45:21 +0500 Subject: [PATCH] feat(artifactPrepareVersion): Introduce build tool CAP (#4890) * feat(artifactPrepareVersion): Introduce build tool CAP * feat(artifactPrepareVersion): Introduce build tool CAP * Add CAPVersioningPreference to versioning.Options * Include CAP to allowed build tool list * Update go.mod * Include CAP to allowed build tool list * Delete CAP from additionalTargetTools * Delete CAP from additionalTargetTools * Fix test * Update comment * Update comment * Add param description * Add param description --- cmd/artifactPrepareVersion.go | 15 +++++----- cmd/artifactPrepareVersion_generated.go | 13 +++++++- pkg/versioning/versioning.go | 30 +++++++++++-------- pkg/versioning/versioning_test.go | 30 +++++++++++++++++++ .../metadata/artifactPrepareVersion.yaml | 20 +++++++++++++ 5 files changed, 88 insertions(+), 20 deletions(-) diff --git a/cmd/artifactPrepareVersion.go b/cmd/artifactPrepareVersion.go index 182ca382f..f4edafa2f 100644 --- a/cmd/artifactPrepareVersion.go +++ b/cmd/artifactPrepareVersion.go @@ -114,13 +114,14 @@ func runArtifactPrepareVersion(config *artifactPrepareVersionOptions, telemetryD // Options for artifact artifactOpts := versioning.Options{ - GlobalSettingsFile: config.GlobalSettingsFile, - M2Path: config.M2Path, - ProjectSettingsFile: config.ProjectSettingsFile, - VersionField: config.CustomVersionField, - VersionSection: config.CustomVersionSection, - VersioningScheme: config.CustomVersioningScheme, - VersionSource: config.DockerVersionSource, + GlobalSettingsFile: config.GlobalSettingsFile, + M2Path: config.M2Path, + ProjectSettingsFile: config.ProjectSettingsFile, + VersionField: config.CustomVersionField, + VersionSection: config.CustomVersionSection, + VersioningScheme: config.CustomVersioningScheme, + VersionSource: config.DockerVersionSource, + CAPVersioningPreference: config.CAPVersioningPreference, } var err error diff --git a/cmd/artifactPrepareVersion_generated.go b/cmd/artifactPrepareVersion_generated.go index e2a7873e1..71b1482d9 100644 --- a/cmd/artifactPrepareVersion_generated.go +++ b/cmd/artifactPrepareVersion_generated.go @@ -20,7 +20,7 @@ import ( type artifactPrepareVersionOptions struct { AdditionalTargetTools []string `json:"additionalTargetTools,omitempty" validate:"possible-values=custom docker dub golang gradle helm maven mta npm pip sbt yarn"` AdditionalTargetDescriptors []string `json:"additionalTargetDescriptors,omitempty"` - BuildTool string `json:"buildTool,omitempty" validate:"possible-values=custom docker dub golang gradle helm maven mta npm pip sbt yarn"` + BuildTool string `json:"buildTool,omitempty" validate:"possible-values=custom docker dub golang gradle helm maven mta npm pip sbt yarn CAP"` CommitUserName string `json:"commitUserName,omitempty"` CustomVersionField string `json:"customVersionField,omitempty"` CustomVersionSection string `json:"customVersionSection,omitempty"` @@ -28,6 +28,7 @@ type artifactPrepareVersionOptions struct { DockerVersionSource string `json:"dockerVersionSource,omitempty"` FetchCoordinates bool `json:"fetchCoordinates,omitempty"` FilePath string `json:"filePath,omitempty"` + CAPVersioningPreference string `json:"CAPVersioningPreference,omitempty" validate:"possible-values=maven npm,required_if=BuildTool CAP"` GlobalSettingsFile string `json:"globalSettingsFile,omitempty"` IncludeCommitID bool `json:"includeCommitId,omitempty"` IsOptimizedAndScheduled bool `json:"isOptimizedAndScheduled,omitempty"` @@ -260,6 +261,7 @@ func addArtifactPrepareVersionFlags(cmd *cobra.Command, stepConfig *artifactPrep cmd.Flags().StringVar(&stepConfig.DockerVersionSource, "dockerVersionSource", os.Getenv("PIPER_dockerVersionSource"), "For `buildTool: docker`: Defines the source of the version. Can be `FROM`, any supported _buildTool_ or an environment variable name.") cmd.Flags().BoolVar(&stepConfig.FetchCoordinates, "fetchCoordinates", false, "If set to `true` the step will retreive artifact coordinates and store them in the common pipeline environment.") cmd.Flags().StringVar(&stepConfig.FilePath, "filePath", os.Getenv("PIPER_filePath"), "Defines a custom path to the descriptor file. Build tool specific defaults are used (e.g. `maven: pom.xml`, `npm: package.json`, `mta: mta.yaml`).") + cmd.Flags().StringVar(&stepConfig.CAPVersioningPreference, "CAPVersioningPreference", `maven`, "For CAP build tool only: Defines which file should be used for versioning.") cmd.Flags().StringVar(&stepConfig.GlobalSettingsFile, "globalSettingsFile", os.Getenv("PIPER_globalSettingsFile"), "Maven only - Path to the mvn settings file that should be used as global settings file.") cmd.Flags().BoolVar(&stepConfig.IncludeCommitID, "includeCommitId", true, "Defines if the automatically generated version (`versioningType: cloud`) should include the commit id hash.") cmd.Flags().BoolVar(&stepConfig.IsOptimizedAndScheduled, "isOptimizedAndScheduled", false, "Whether the pipeline runs in optimized mode and the current execution is a scheduled one") @@ -382,6 +384,15 @@ func artifactPrepareVersionMetadata() config.StepData { Aliases: []config.Alias{}, Default: os.Getenv("PIPER_filePath"), }, + { + Name: "CAPVersioningPreference", + ResourceRef: []config.ResourceReference{}, + Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, + Type: "string", + Mandatory: false, + Aliases: []config.Alias{}, + Default: `maven`, + }, { Name: "globalSettingsFile", ResourceRef: []config.ResourceReference{}, diff --git a/pkg/versioning/versioning.go b/pkg/versioning/versioning.go index 3d20363e3..471bd5584 100644 --- a/pkg/versioning/versioning.go +++ b/pkg/versioning/versioning.go @@ -7,9 +7,8 @@ import ( "os" "path/filepath" - "github.com/SAP/jenkins-library/pkg/piperutils" - "github.com/SAP/jenkins-library/pkg/maven" + "github.com/SAP/jenkins-library/pkg/piperutils" ) // Coordinates to address the artifact coordinates like groupId, artifactId, version and packaging @@ -30,16 +29,17 @@ type Artifact interface { // Options define build tool specific settings in order to properly retrieve e.g. the version / coordinates of an artifact type Options struct { - ProjectSettingsFile string - DockerImage string - GlobalSettingsFile string - M2Path string - Defines []string - VersionSource string - VersionSection string - VersionField string - VersioningScheme string - HelmUpdateAppVersion bool + ProjectSettingsFile string + DockerImage string + GlobalSettingsFile string + M2Path string + Defines []string + VersionSource string + VersionSection string + VersionField string + VersioningScheme string + HelmUpdateAppVersion bool + CAPVersioningPreference string } // Utils defines the versioning operations for various build tools @@ -75,6 +75,12 @@ func GetArtifact(buildTool, buildDescriptorFilePath string, opts *Options, utils if fileExists == nil { fileExists = piperutils.FileExists } + + // CAPVersioningPreference can only be 'maven' or 'npm'. Verification done on artifactPrepareVersion.yaml level + if buildTool == "CAP" { + buildTool = opts.CAPVersioningPreference + } + switch buildTool { case "custom": var err error diff --git a/pkg/versioning/versioning_test.go b/pkg/versioning/versioning_test.go index 1cb335183..c18867efd 100644 --- a/pkg/versioning/versioning_test.go +++ b/pkg/versioning/versioning_test.go @@ -150,6 +150,25 @@ func TestGetArtifact(t *testing.T) { assert.Equal(t, "maven", maven.VersioningScheme()) }) + t.Run("CAP - maven", func(t *testing.T) { + opts := Options{ + ProjectSettingsFile: "projectsettings.xml", + GlobalSettingsFile: "globalsettings.xml", + M2Path: "m2/path", + CAPVersioningPreference: "maven", + } + maven, err := GetArtifact("CAP", "", &opts, nil) + assert.NoError(t, err) + + theType, ok := maven.(*Maven) + assert.True(t, ok) + assert.Equal(t, "pom.xml", theType.options.PomPath) + assert.Equal(t, opts.ProjectSettingsFile, theType.options.ProjectSettingsFile) + assert.Equal(t, opts.GlobalSettingsFile, theType.options.GlobalSettingsFile) + assert.Equal(t, opts.M2Path, theType.options.M2Path) + assert.Equal(t, "maven", maven.VersioningScheme()) + }) + t.Run("mta", func(t *testing.T) { mta, err := GetArtifact("mta", "", &Options{VersionField: "theversion"}, nil) @@ -174,6 +193,17 @@ func TestGetArtifact(t *testing.T) { assert.Equal(t, "semver2", npm.VersioningScheme()) }) + t.Run("CAP - npm", func(t *testing.T) { + npm, err := GetArtifact("CAP", "", &Options{VersionField: "theversion", CAPVersioningPreference: "npm"}, nil) + assert.NoError(t, err) + + theType, ok := npm.(*JSONfile) + assert.True(t, ok) + assert.Equal(t, "package.json", theType.path) + assert.Equal(t, "version", theType.versionField) + assert.Equal(t, "semver2", npm.VersioningScheme()) + }) + t.Run("yarn", func(t *testing.T) { npm, err := GetArtifact("yarn", "", &Options{VersionField: "theversion"}, nil) diff --git a/resources/metadata/artifactPrepareVersion.yaml b/resources/metadata/artifactPrepareVersion.yaml index 44abf0577..bb51f66c8 100644 --- a/resources/metadata/artifactPrepareVersion.yaml +++ b/resources/metadata/artifactPrepareVersion.yaml @@ -151,6 +151,7 @@ spec: - pip - sbt - yarn + - CAP - name: commitUserName aliases: - name: gitUserName @@ -209,6 +210,25 @@ spec: - PARAMETERS - STAGES - STEPS + - name: CAPVersioningPreference + type: string + description: "For CAP build tool only: Defines which file should be used for versioning." + longDescription: | + If `maven` is chosen (default value), then the step expects a pom.xml file in the project's root folder. Alternatively, you can specify the path to the file using the `filePath` parameter. + If `npm` is chosen, then the step expects a package.json file in the project's root folder. Alternatively, you can specify the path to the file using the `filePath` parameter. + + In case you want to propagate version to addition file(s), it can be done via `additionalTargetTools` and `additionalTargetDescriptors` parameters. + scope: + - PARAMETERS + - STAGES + - STEPS + default: maven + possibleValues: + - maven + - npm + mandatoryIf: + - name: buildTool + value: CAP - name: globalSettingsFile aliases: - name: maven/globalSettingsFile