1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/versioning/gomodfile.go
Jordan Levin 34967c502c
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

114 lines
2.9 KiB
Go

package versioning
import (
"io/ioutil"
"os"
"strings"
"golang.org/x/mod/modfile"
"github.com/pkg/errors"
)
// GoModDescriptor holds the unique identifier combination for Go modules
type GoModDescriptor struct {
GroupID string
ArtifactID string
Version string
Packaging string
}
// GoMod utility to interact with Go Modules specific versioning
type GoMod struct {
path string
readFile func(string) ([]byte, error)
writeFile func(string, []byte, os.FileMode) error
fileExists func(string) (bool, error)
buildDescriptorContent string
}
func (m *GoMod) init() error {
if m.readFile == nil {
m.readFile = ioutil.ReadFile
}
if m.writeFile == nil {
m.writeFile = ioutil.WriteFile
}
if len(m.buildDescriptorContent) == 0 {
content, err := m.readFile(m.path)
if err != nil {
return errors.Wrapf(err, "failed to read file '%v'", m.path)
}
m.buildDescriptorContent = string(content)
}
return nil
}
// GetVersion returns the go.mod descriptor version property
func (m *GoMod) GetVersion() (string, error) {
buildDescriptorFilePath := m.path
var err error
if strings.Contains(m.path, "go.mod") {
buildDescriptorFilePath, err = searchDescriptor([]string{"version.txt", "VERSION"}, m.fileExists)
if err != nil {
err = m.init()
if err != nil {
return "", errors.Wrapf(err, "failed to read file '%v'", m.path)
}
parsed, err := modfile.Parse(m.path, []byte(m.buildDescriptorContent), nil)
if err != nil {
return "", errors.Wrap(err, "failed to parse go.mod file")
}
if parsed.Module.Mod.Version != "" {
return parsed.Module.Mod.Version, nil
}
return "", errors.Wrap(err, "failed to retrieve version")
}
}
artifact := &Versionfile{
path: buildDescriptorFilePath,
versioningScheme: m.VersioningScheme(),
}
return artifact.GetVersion()
}
// SetVersion sets the go.mod descriptor version property
func (m *GoMod) SetVersion(v string) error {
return nil
}
// VersioningScheme returns the relevant versioning scheme
func (m *GoMod) VersioningScheme() string {
return "semver2"
}
// GetCoordinates returns the go.mod build descriptor coordinates
func (m *GoMod) GetCoordinates() (Coordinates, error) {
err := m.init()
if err != nil {
return nil, err
}
parsed, err := modfile.Parse(m.path, []byte(m.buildDescriptorContent), nil)
if err != nil {
return "", errors.Wrap(err, "failed to parse go.mod file")
}
descriptor := &GoModDescriptor{}
if parsed.Module == nil {
return "", errors.Wrap(err, "failed to parse go.mod file")
}
if parsed.Module.Mod.Path != "" {
artifactSplit := strings.Split(parsed.Module.Mod.Path, "/")
artifactID := artifactSplit[len(artifactSplit)-1]
descriptor.ArtifactID = artifactID
}
descriptor.Version = parsed.Module.Mod.Version
if descriptor.Version == "" {
descriptor.Version = "unspecified"
}
return descriptor, nil
}