Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add build artifacts metadata for mtaBuild #5166

Merged
merged 14 commits into from
Nov 4, 2024
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(filepath.Join(filepath.Dir(match), "/target/"+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
54 changes: 0 additions & 54 deletions cmd/mavenBuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@
package cmd

import (
"os"
"path/filepath"
"testing"

"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/stretchr/testify/assert"
)

var cpe mavenBuildCommonPipelineEnvironment

func TestMavenBuild(t *testing.T) {

t.Run("mavenBuild should install the artifact", func(t *testing.T) {
mockedUtils := newMavenMockUtils()

Expand Down Expand Up @@ -123,54 +119,4 @@ func TestMavenBuild(t *testing.T) {
assert.Equal(t, mockedUtils.Calls[0].Exec, "mvn")
assert.Empty(t, cpe.custom.mavenBuildArtifacts)
})

}

func createTempFile(t *testing.T, dir string, filename string, content string) string {
filePath := filepath.Join(dir, filename)
err := os.WriteFile(filePath, []byte(content), 0666)
if err != nil {
t.Fatalf("Failed to create temp file: %s", err)
}
return filePath
}

func TestGetPurlForThePomAndDeleteIndividualBom(t *testing.T) {

t.Run("valid BOM file, aggregated BOM", func(t *testing.T) {
tempDir, err := piperutils.Files{}.TempDir("", "test")
if err != nil {
t.Fatalf("Failed to create temp directory: %s", err)
}

bomContent := `<bom>
<metadata>
<component>
<purl>pkg:maven/com.example/[email protected]</purl>
</component>
<properties>
<property name="maven.goal" value="makeAggregateBom" />
</properties>
</metadata>
</bom>`
pomFilePath := createTempFile(t, tempDir, "pom.xml", "")
bomDir := filepath.Join(tempDir, "target")
if err := os.MkdirAll(bomDir, 0777); err != nil {
t.Fatalf("Failed to create temp directory: %s", err)
}
bomFilePath := createTempFile(t, bomDir, mvnSimpleBomFilename+".xml", bomContent)

purl := getPurlForThePom(pomFilePath)
assert.Equal(t, "pkg:maven/com.example/[email protected]", purl)
_, err = os.Stat(bomFilePath)
assert.False(t, os.IsNotExist(err)) // File should not be deleted
})

t.Run("BOM file does not exist", func(t *testing.T) {
tempDir := t.TempDir()
pomFilePath := createTempFile(t, tempDir, "pom.xml", "") // Create a temp pom file

purl := getPurlForThePom(pomFilePath)
assert.Equal(t, "", purl)
})
}
Loading
Loading