Skip to content

Commit

Permalink
feat(artifactPrepareVersion): Introduce build tool CAP (SAP#4890)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
vstarostin authored Apr 10, 2024
1 parent 3ae51e2 commit b0ecbf6
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 20 deletions.
15 changes: 8 additions & 7 deletions cmd/artifactPrepareVersion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion cmd/artifactPrepareVersion_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 18 additions & 12 deletions pkg/versioning/versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions pkg/versioning/versioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down
20 changes: 20 additions & 0 deletions resources/metadata/artifactPrepareVersion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ spec:
- pip
- sbt
- yarn
- CAP
- name: commitUserName
aliases:
- name: gitUserName
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b0ecbf6

Please sign in to comment.