2020-04-03 16:34:40 +02:00
|
|
|
package versioning
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-15 13:12:43 +02:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/piperutils"
|
2020-04-03 16:34:40 +02:00
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/maven"
|
|
|
|
)
|
|
|
|
|
2020-05-25 19:48:59 +02:00
|
|
|
// Coordinates to address the artifact
|
|
|
|
type Coordinates interface{}
|
|
|
|
|
2020-04-15 13:12:43 +02:00
|
|
|
// Artifact defines the versioning operations for various build tools
|
2020-04-03 16:34:40 +02:00
|
|
|
type Artifact interface {
|
|
|
|
VersioningScheme() string
|
|
|
|
GetVersion() (string, error)
|
|
|
|
SetVersion(string) error
|
2020-05-25 19:48:59 +02:00
|
|
|
GetCoordinates() (Coordinates, error)
|
2020-04-03 16:34:40 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 13:12:43 +02:00
|
|
|
// Options define build tool specific settings in order to properly retrieve e.g. the version of an artifact
|
2020-04-03 16:34:40 +02:00
|
|
|
type Options struct {
|
|
|
|
ProjectSettingsFile string
|
|
|
|
GlobalSettingsFile string
|
|
|
|
M2Path string
|
2020-04-15 13:12:43 +02:00
|
|
|
VersionSource string
|
|
|
|
VersionSection string
|
|
|
|
VersionField string
|
2020-04-24 20:52:16 +02:00
|
|
|
VersioningScheme string
|
2020-04-03 16:34:40 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
// Utils defines the versioning operations for various build tools
|
|
|
|
type Utils interface {
|
|
|
|
maven.Utils
|
|
|
|
}
|
|
|
|
|
2020-04-03 16:34:40 +02:00
|
|
|
type mvnRunner struct{}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
func (m *mvnRunner) Execute(options *maven.ExecuteOptions, utils maven.Utils) (string, error) {
|
|
|
|
return maven.Execute(options, utils)
|
2020-04-03 16:34:40 +02:00
|
|
|
}
|
2020-11-10 18:14:55 +02:00
|
|
|
func (m *mvnRunner) Evaluate(options *maven.EvaluateOptions, expression string, utils maven.Utils) (string, error) {
|
|
|
|
return maven.Evaluate(options, expression, utils)
|
2020-04-03 16:34:40 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 13:12:43 +02:00
|
|
|
var fileExists func(string) (bool, error)
|
|
|
|
|
|
|
|
// GetArtifact returns the build tool specific implementation for retrieving version, etc. of an artifact
|
2020-11-10 18:14:55 +02:00
|
|
|
func GetArtifact(buildTool, buildDescriptorFilePath string, opts *Options, utils Utils) (Artifact, error) {
|
2020-04-03 16:34:40 +02:00
|
|
|
var artifact Artifact
|
2020-04-15 13:12:43 +02:00
|
|
|
if fileExists == nil {
|
|
|
|
fileExists = piperutils.FileExists
|
|
|
|
}
|
2020-04-03 16:34:40 +02:00
|
|
|
switch buildTool {
|
2020-04-15 13:12:43 +02:00
|
|
|
case "custom":
|
|
|
|
var err error
|
2020-04-24 20:52:16 +02:00
|
|
|
artifact, err = customArtifact(buildDescriptorFilePath, opts.VersionField, opts.VersionSection, opts.VersioningScheme)
|
2020-04-15 13:12:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return artifact, err
|
|
|
|
}
|
|
|
|
case "docker":
|
|
|
|
artifact = &Docker{
|
2020-11-10 18:14:55 +02:00
|
|
|
utils: utils,
|
2020-05-06 22:07:27 +02:00
|
|
|
options: opts,
|
|
|
|
path: buildDescriptorFilePath,
|
|
|
|
versionSource: opts.VersionSource,
|
|
|
|
versioningScheme: opts.VersioningScheme,
|
2020-04-15 13:12:43 +02:00
|
|
|
}
|
|
|
|
case "dub":
|
|
|
|
if len(buildDescriptorFilePath) == 0 {
|
|
|
|
buildDescriptorFilePath = "dub.json"
|
|
|
|
}
|
|
|
|
artifact = &JSONfile{
|
|
|
|
path: buildDescriptorFilePath,
|
|
|
|
versionField: "version",
|
|
|
|
}
|
Whitesource scan (MVP) (#1658)
* Whitesource MVP for Gradle, Golang, and NPM/Yarn
* Refactoring
* Refactor and cleanup, better error checking
* publish stepResults, use pkg/versioning, bubble up errors, add gomod versioning support
* Run gofmt and cleanup comments
* Resolve PR comments
* Update resources/metadata/whitesource.yaml
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
* Only determine project coordinates if they are missing
Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com>
* Gradle versioning artifact
* fix gradle artifact version regexp and refactor
* Fix token extraction from output buffer
* Fix some issues with pip and jsonfile versioning logic
* Remove useless spacing
* Remove unnecessary test file and fix naming style for JSONDescriptor
* Automatically download wss-unified-agent if file does not exist
* adds downloadVulnerabilityReport, checkSecurityViolations, minor refactoring
* adds config.ReportDirectoryName, improves readability
* Version-wide reporting for vulnerabilities and list of libraries.
* Refactor and improve build accuracy
* fix sed command
* Add includes file pattern config option
* Adds --exclude command line flag
* run go mod tidy and regenerate step framework
* Fix unit tests
* revert changes
* poll project status before downloading reports
* merge with master
* go mod tidy, go fmt, and fix whitesource unit test
* sync go.mod
* sync go.mod again
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com>
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2020-07-01 07:54:13 +02:00
|
|
|
case "gradle":
|
|
|
|
if len(buildDescriptorFilePath) == 0 {
|
2020-11-06 10:20:08 +02:00
|
|
|
buildDescriptorFilePath = "gradle.properties"
|
|
|
|
}
|
|
|
|
artifact = &Gradle{
|
|
|
|
path: buildDescriptorFilePath,
|
|
|
|
versionField: opts.VersionField,
|
Whitesource scan (MVP) (#1658)
* Whitesource MVP for Gradle, Golang, and NPM/Yarn
* Refactoring
* Refactor and cleanup, better error checking
* publish stepResults, use pkg/versioning, bubble up errors, add gomod versioning support
* Run gofmt and cleanup comments
* Resolve PR comments
* Update resources/metadata/whitesource.yaml
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
* Only determine project coordinates if they are missing
Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com>
* Gradle versioning artifact
* fix gradle artifact version regexp and refactor
* Fix token extraction from output buffer
* Fix some issues with pip and jsonfile versioning logic
* Remove useless spacing
* Remove unnecessary test file and fix naming style for JSONDescriptor
* Automatically download wss-unified-agent if file does not exist
* adds downloadVulnerabilityReport, checkSecurityViolations, minor refactoring
* adds config.ReportDirectoryName, improves readability
* Version-wide reporting for vulnerabilities and list of libraries.
* Refactor and improve build accuracy
* fix sed command
* Add includes file pattern config option
* Adds --exclude command line flag
* run go mod tidy and regenerate step framework
* Fix unit tests
* revert changes
* poll project status before downloading reports
* merge with master
* go mod tidy, go fmt, and fix whitesource unit test
* sync go.mod
* sync go.mod again
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com>
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2020-07-01 07:54:13 +02:00
|
|
|
}
|
2020-04-15 13:12:43 +02:00
|
|
|
case "golang":
|
|
|
|
if len(buildDescriptorFilePath) == 0 {
|
|
|
|
var err error
|
Whitesource scan (MVP) (#1658)
* Whitesource MVP for Gradle, Golang, and NPM/Yarn
* Refactoring
* Refactor and cleanup, better error checking
* publish stepResults, use pkg/versioning, bubble up errors, add gomod versioning support
* Run gofmt and cleanup comments
* Resolve PR comments
* Update resources/metadata/whitesource.yaml
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
* Only determine project coordinates if they are missing
Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com>
* Gradle versioning artifact
* fix gradle artifact version regexp and refactor
* Fix token extraction from output buffer
* Fix some issues with pip and jsonfile versioning logic
* Remove useless spacing
* Remove unnecessary test file and fix naming style for JSONDescriptor
* Automatically download wss-unified-agent if file does not exist
* adds downloadVulnerabilityReport, checkSecurityViolations, minor refactoring
* adds config.ReportDirectoryName, improves readability
* Version-wide reporting for vulnerabilities and list of libraries.
* Refactor and improve build accuracy
* fix sed command
* Add includes file pattern config option
* Adds --exclude command line flag
* run go mod tidy and regenerate step framework
* Fix unit tests
* revert changes
* poll project status before downloading reports
* merge with master
* go mod tidy, go fmt, and fix whitesource unit test
* sync go.mod
* sync go.mod again
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com>
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2020-07-01 07:54:13 +02:00
|
|
|
buildDescriptorFilePath, err = searchDescriptor([]string{"VERSION", "version.txt", "go.mod"}, fileExists)
|
2020-04-15 13:12:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return artifact, err
|
|
|
|
}
|
|
|
|
}
|
Whitesource scan (MVP) (#1658)
* Whitesource MVP for Gradle, Golang, and NPM/Yarn
* Refactoring
* Refactor and cleanup, better error checking
* publish stepResults, use pkg/versioning, bubble up errors, add gomod versioning support
* Run gofmt and cleanup comments
* Resolve PR comments
* Update resources/metadata/whitesource.yaml
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
* Only determine project coordinates if they are missing
Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com>
* Gradle versioning artifact
* fix gradle artifact version regexp and refactor
* Fix token extraction from output buffer
* Fix some issues with pip and jsonfile versioning logic
* Remove useless spacing
* Remove unnecessary test file and fix naming style for JSONDescriptor
* Automatically download wss-unified-agent if file does not exist
* adds downloadVulnerabilityReport, checkSecurityViolations, minor refactoring
* adds config.ReportDirectoryName, improves readability
* Version-wide reporting for vulnerabilities and list of libraries.
* Refactor and improve build accuracy
* fix sed command
* Add includes file pattern config option
* Adds --exclude command line flag
* run go mod tidy and regenerate step framework
* Fix unit tests
* revert changes
* poll project status before downloading reports
* merge with master
* go mod tidy, go fmt, and fix whitesource unit test
* sync go.mod
* sync go.mod again
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com>
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2020-07-01 07:54:13 +02:00
|
|
|
|
|
|
|
switch buildDescriptorFilePath {
|
|
|
|
case "go.mod":
|
versioning: fix panic when using go.mod (#1938)
```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x9830a6]
goroutine 1 [running]:
github.com/SAP/jenkins-library/pkg/versioning.searchDescriptor(0xc00067c060, 0x2, 0x2, 0x0, 0x0, 0xc0006549c0, 0xc0006413a0, 0x982ce0)
/home/runner/work/jenkins-library/jenkins-library/pkg/versioning/versioning.go:156 +0x76
github.com/SAP/jenkins-library/pkg/versioning.(*GoMod).GetVersion(0xc0006549c0, 0x6, 0x18089f1, 0x6, 0xc000664a80)
/home/runner/work/jenkins-library/jenkins-library/pkg/versioning/gomodfile.go:52 +0x18a
...
```
2020-08-20 12:39:41 +02:00
|
|
|
artifact = &GoMod{path: buildDescriptorFilePath, fileExists: fileExists}
|
Whitesource scan (MVP) (#1658)
* Whitesource MVP for Gradle, Golang, and NPM/Yarn
* Refactoring
* Refactor and cleanup, better error checking
* publish stepResults, use pkg/versioning, bubble up errors, add gomod versioning support
* Run gofmt and cleanup comments
* Resolve PR comments
* Update resources/metadata/whitesource.yaml
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
* Only determine project coordinates if they are missing
Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com>
* Gradle versioning artifact
* fix gradle artifact version regexp and refactor
* Fix token extraction from output buffer
* Fix some issues with pip and jsonfile versioning logic
* Remove useless spacing
* Remove unnecessary test file and fix naming style for JSONDescriptor
* Automatically download wss-unified-agent if file does not exist
* adds downloadVulnerabilityReport, checkSecurityViolations, minor refactoring
* adds config.ReportDirectoryName, improves readability
* Version-wide reporting for vulnerabilities and list of libraries.
* Refactor and improve build accuracy
* fix sed command
* Add includes file pattern config option
* Adds --exclude command line flag
* run go mod tidy and regenerate step framework
* Fix unit tests
* revert changes
* poll project status before downloading reports
* merge with master
* go mod tidy, go fmt, and fix whitesource unit test
* sync go.mod
* sync go.mod again
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com>
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2020-07-01 07:54:13 +02:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
artifact = &Versionfile{path: buildDescriptorFilePath}
|
2020-04-15 13:12:43 +02:00
|
|
|
}
|
2020-04-03 16:34:40 +02:00
|
|
|
case "maven":
|
2020-04-15 13:12:43 +02:00
|
|
|
if len(buildDescriptorFilePath) == 0 {
|
|
|
|
buildDescriptorFilePath = "pom.xml"
|
|
|
|
}
|
2020-04-03 16:34:40 +02:00
|
|
|
artifact = &Maven{
|
2020-11-10 18:14:55 +02:00
|
|
|
runner: &mvnRunner{},
|
|
|
|
utils: utils,
|
2020-06-11 14:02:54 +02:00
|
|
|
options: maven.EvaluateOptions{
|
|
|
|
PomPath: buildDescriptorFilePath,
|
|
|
|
ProjectSettingsFile: opts.ProjectSettingsFile,
|
|
|
|
GlobalSettingsFile: opts.GlobalSettingsFile,
|
|
|
|
M2Path: opts.M2Path,
|
|
|
|
},
|
2020-04-15 13:12:43 +02:00
|
|
|
}
|
|
|
|
case "mta":
|
|
|
|
if len(buildDescriptorFilePath) == 0 {
|
|
|
|
buildDescriptorFilePath = "mta.yaml"
|
|
|
|
}
|
|
|
|
artifact = &YAMLfile{
|
2020-05-27 17:20:34 +02:00
|
|
|
path: buildDescriptorFilePath,
|
|
|
|
versionField: "version",
|
|
|
|
artifactIDField: "ID",
|
2020-04-03 16:34:40 +02:00
|
|
|
}
|
|
|
|
case "npm":
|
2020-04-15 13:12:43 +02:00
|
|
|
if len(buildDescriptorFilePath) == 0 {
|
|
|
|
buildDescriptorFilePath = "package.json"
|
|
|
|
}
|
|
|
|
artifact = &JSONfile{
|
|
|
|
path: buildDescriptorFilePath,
|
|
|
|
versionField: "version",
|
|
|
|
}
|
|
|
|
case "pip":
|
|
|
|
if len(buildDescriptorFilePath) == 0 {
|
|
|
|
var err error
|
2020-05-25 19:48:59 +02:00
|
|
|
buildDescriptorFilePath, err = searchDescriptor([]string{"version.txt", "VERSION", "setup.py"}, fileExists)
|
2020-04-15 13:12:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return artifact, err
|
|
|
|
}
|
|
|
|
}
|
2020-05-25 19:48:59 +02:00
|
|
|
artifact = &Pip{
|
|
|
|
path: buildDescriptorFilePath,
|
|
|
|
fileExists: fileExists,
|
2020-04-15 13:12:43 +02:00
|
|
|
}
|
|
|
|
case "sbt":
|
|
|
|
if len(buildDescriptorFilePath) == 0 {
|
2020-09-29 12:44:31 +02:00
|
|
|
var err error
|
|
|
|
buildDescriptorFilePath, err = searchDescriptor([]string{"sbtDescriptor.json", "build.sbt"}, fileExists)
|
|
|
|
if err != nil {
|
|
|
|
return artifact, err
|
|
|
|
}
|
2020-04-15 13:12:43 +02:00
|
|
|
}
|
|
|
|
artifact = &JSONfile{
|
|
|
|
path: buildDescriptorFilePath,
|
|
|
|
versionField: "version",
|
2020-04-03 16:34:40 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return artifact, fmt.Errorf("build tool '%v' not supported", buildTool)
|
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|
2020-04-15 13:12:43 +02:00
|
|
|
|
|
|
|
func searchDescriptor(supported []string, existsFunc func(string) (bool, error)) (string, error) {
|
|
|
|
var descriptor string
|
|
|
|
for _, f := range supported {
|
|
|
|
exists, _ := existsFunc(f)
|
|
|
|
if exists {
|
|
|
|
descriptor = f
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(descriptor) == 0 {
|
|
|
|
return "", fmt.Errorf("no build descriptor available, supported: %v", supported)
|
|
|
|
}
|
|
|
|
return descriptor, nil
|
|
|
|
}
|
|
|
|
|
2020-04-24 20:52:16 +02:00
|
|
|
func customArtifact(buildDescriptorFilePath, field, section, scheme string) (Artifact, error) {
|
2020-04-15 13:12:43 +02:00
|
|
|
switch filepath.Ext(buildDescriptorFilePath) {
|
|
|
|
case ".cfg", ".ini":
|
|
|
|
return &INIfile{
|
2020-04-24 20:52:16 +02:00
|
|
|
path: buildDescriptorFilePath,
|
|
|
|
versionField: field,
|
|
|
|
versionSection: section,
|
|
|
|
versioningScheme: scheme,
|
2020-04-15 13:12:43 +02:00
|
|
|
}, nil
|
|
|
|
case ".json":
|
|
|
|
return &JSONfile{
|
|
|
|
path: buildDescriptorFilePath,
|
|
|
|
versionField: field,
|
|
|
|
}, nil
|
|
|
|
case ".yaml", ".yml":
|
|
|
|
return &YAMLfile{
|
|
|
|
path: buildDescriptorFilePath,
|
|
|
|
versionField: field,
|
|
|
|
}, nil
|
|
|
|
case ".txt", "":
|
|
|
|
return &Versionfile{
|
2020-04-24 20:52:16 +02:00
|
|
|
path: buildDescriptorFilePath,
|
|
|
|
versioningScheme: scheme,
|
2020-04-15 13:12:43 +02:00
|
|
|
}, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("file type not supported: '%v'", buildDescriptorFilePath)
|
|
|
|
}
|
|
|
|
}
|