Skip to content

Commit

Permalink
refactor: extsact getPurl in piper utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Germanov committed Oct 29, 2024
1 parent e64ac61 commit 4cf86a0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 62 deletions.
29 changes: 4 additions & 25 deletions cmd/mavenBuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func mavenBuild(config mavenBuildOptions, telemetryData *telemetry.CustomData, c
}

func runMakeBOMGoal(config *mavenBuildOptions, utils maven.Utils) error {
var flags = []string{"-update-snapshots", "--batch-mode"}
flags := []string{"-update-snapshots", "--batch-mode"}
if len(config.Profiles) > 0 {
flags = append(flags, "--activate-profiles", strings.Join(config.Profiles, ","))
}
Expand Down Expand Up @@ -89,8 +89,7 @@ func runMakeBOMGoal(config *mavenBuildOptions, utils maven.Utils) error {
}

func runMavenBuild(config *mavenBuildOptions, _ *telemetry.CustomData, utils maven.Utils, commonPipelineEnvironment *mavenBuildCommonPipelineEnvironment) error {

var flags = []string{"-update-snapshots", "--batch-mode"}
flags := []string{"-update-snapshots", "--batch-mode"}

if len(config.Profiles) > 0 {
flags = append(flags, "--activate-profiles", strings.Join(config.Profiles, ","))
Expand Down Expand Up @@ -255,7 +254,7 @@ func createBuildArtifactsMetadata(config *mavenBuildOptions, commonPipelineEnvir
} else {
coordinate.BuildPath = filepath.Dir(match)
coordinate.URL = config.AltDeploymentRepositoryURL
coordinate.PURL = getPurlForThePom(match)
coordinate.PURL = piperutils.GetPurl(match, mvnSimpleBomFilename+".xml")
buildCoordinates = append(buildCoordinates, coordinate)
}
}
Expand All @@ -274,25 +273,6 @@ func createBuildArtifactsMetadata(config *mavenBuildOptions, commonPipelineEnvir
return nil, false
}

func getPurlForThePom(pomFilePath string) string {
bomPath := filepath.Join(filepath.Dir(pomFilePath) + "/target/" + mvnSimpleBomFilename + ".xml")
exists, _ := piperutils.FileExists(bomPath)
if !exists {
log.Entry().Debugf("bom file doesn't exist and hence no pURL info: %v", bomPath)
return ""
}
bom, err := piperutils.GetBom(bomPath)
if err != nil {
log.Entry().Warnf("failed to get bom file %s: %v", bomPath, err)
return ""
}

log.Entry().Debugf("Found purl: %s for the bomPath: %s", bom.Metadata.Component.Purl, bomPath)
purl := bom.Metadata.Component.Purl

return purl
}

func createOrUpdateProjectSettingsXML(projectSettingsFile string, altDeploymentRepositoryID string, altDeploymentRepositoryUser string, altDeploymentRepositoryPassword string, utils maven.Utils) (string, error) {
if len(projectSettingsFile) > 0 {
projectSettingsFilePath, err := maven.UpdateProjectSettingsXML(projectSettingsFile, altDeploymentRepositoryID, altDeploymentRepositoryUser, altDeploymentRepositoryPassword, utils)
Expand All @@ -310,15 +290,14 @@ func createOrUpdateProjectSettingsXML(projectSettingsFile string, altDeploymentR
}

func loadRemoteRepoCertificates(certificateList []string, client piperhttp.Downloader, flags *[]string, runner command.ExecRunner, fileUtils piperutils.FileUtils, javaCaCertFilePath string) error {
//TODO: make use of java/keytool package
// TODO: make use of java/keytool package
existingJavaCaCerts := filepath.Join(os.Getenv("JAVA_HOME"), "jre", "lib", "security", "cacerts")

if len(javaCaCertFilePath) > 0 {
existingJavaCaCerts = javaCaCertFilePath
}

exists, err := fileUtils.FileExists(existingJavaCaCerts)

if err != nil {
return errors.Wrap(err, "Could not find the existing java cacerts")
}
Expand Down
21 changes: 1 addition & 20 deletions cmd/mtaBuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func buildArtifactsMetadata(config mtaBuildOptions, commonPipelineEnvironment *m
Packaging: "mtar",
BuildPath: filepath.Dir(mtarPath),
URL: config.MtaDeploymentRepositoryURL,
PURL: getPurl(mtarPath),
PURL: piperutils.GetPurl(mtarPath, "sbom-gen/bom-mta.xml"),
},
},
}
Expand Down Expand Up @@ -582,22 +582,3 @@ func getAbsPath(path string) string {
}
return filepath.FromSlash(abspath)
}

func getPurl(mtaYaml string) string {
expectedBomFilePath := filepath.Join(filepath.Dir(mtaYaml) + filepath.FromSlash("sbom-gen/bom-mta.xml"))
exists, err := piperutils.FileExists(expectedBomFilePath)
if err != nil {
log.Entry().Warnf("unable to check if bom file exists: %v", err)
return ""
}
if !exists {
log.Entry().Debugf("bom file doesn't exist and hence no pURL info: %v", expectedBomFilePath)
return ""
}
bom, err := piperutils.GetBom(expectedBomFilePath)
if err != nil {
log.Entry().Warnf("unable to get bom metadata: %v", err)
return ""
}
return bom.Metadata.Component.Purl
}
18 changes: 2 additions & 16 deletions pkg/npm/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pkg/errors"

"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
CredentialUtils "github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/SAP/jenkins-library/pkg/versioning"
)
Expand Down Expand Up @@ -217,7 +218,7 @@ func (exec *Execute) publish(packageJSON, registry, username, password string, p
coordinate.BuildPath = filepath.Dir(packageJSON)
coordinate.URL = registry
coordinate.Packaging = "tgz"
coordinate.PURL = getPurl(packageJSON)
coordinate.PURL = piperutils.GetPurl(packageJSON, npmBomFilename)

*buildCoordinates = append(*buildCoordinates, coordinate)
}
Expand All @@ -226,21 +227,6 @@ func (exec *Execute) publish(packageJSON, registry, username, password string, p
return nil
}

func getPurl(packageJSON string) string {
expectedBomFilePath := filepath.Join(filepath.Dir(packageJSON) + "/" + npmBomFilename)
exists, _ := CredentialUtils.FileExists(expectedBomFilePath)
if !exists {
log.Entry().Debugf("bom file doesn't exist and hence no pURL info: %v", expectedBomFilePath)
return ""
}
bom, err := CredentialUtils.GetBom(expectedBomFilePath)
if err != nil {
log.Entry().Warnf("unable to get bom metdata : %v", err)
return ""
}
return bom.Metadata.Component.Purl
}

func (exec *Execute) readPackageScope(packageJSON string) (string, error) {
b, err := exec.Utils.FileRead(packageJSON)
if err != nil {
Expand Down
23 changes: 22 additions & 1 deletion pkg/piperutils/cyclonedxBom.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package piperutils

import (
"encoding/xml"
"github.com/SAP/jenkins-library/pkg/log"
"io"
"os"
"path/filepath"

"github.com/SAP/jenkins-library/pkg/log"
)

// To serialize the cyclonedx BOM file
Expand Down Expand Up @@ -46,3 +48,22 @@ func GetBom(absoluteBomPath string) (Bom, error) {
}
return bom, nil
}

func GetPurl(filePath, bomFilename string) string {
bomFilePath := filepath.Join(filepath.Dir(filePath), bomFilename)
exists, err := FileExists(bomFilePath)
if err != nil {
log.Entry().Warnf("unable to check if bom file exists: %v", err)
return ""
}
if !exists {
log.Entry().Debugf("bom file doesn't exist and hence no pURL info: %v", bomFilePath)
return ""
}
bom, err := GetBom(bomFilePath)
if err != nil {
log.Entry().Warnf("unable to get bom metadata: %v", err)
return ""
}
return bom.Metadata.Component.Purl
}

0 comments on commit 4cf86a0

Please sign in to comment.