Skip to content

Commit 6988f43

Browse files
authored
feat: add build artifacts metadata for mtaBuild (#5166)
1 parent da609e1 commit 6988f43

10 files changed

+395
-300
lines changed

cmd/mavenBuild.go

+4-25
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func mavenBuild(config mavenBuildOptions, telemetryData *telemetry.CustomData, c
4141
}
4242

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

9191
func runMavenBuild(config *mavenBuildOptions, _ *telemetry.CustomData, utils maven.Utils, commonPipelineEnvironment *mavenBuildCommonPipelineEnvironment) error {
92-
93-
var flags = []string{"-update-snapshots", "--batch-mode"}
92+
flags := []string{"-update-snapshots", "--batch-mode"}
9493

9594
if len(config.Profiles) > 0 {
9695
flags = append(flags, "--activate-profiles", strings.Join(config.Profiles, ","))
@@ -255,7 +254,7 @@ func createBuildArtifactsMetadata(config *mavenBuildOptions, commonPipelineEnvir
255254
} else {
256255
coordinate.BuildPath = filepath.Dir(match)
257256
coordinate.URL = config.AltDeploymentRepositoryURL
258-
coordinate.PURL = getPurlForThePom(match)
257+
coordinate.PURL = piperutils.GetPurl(filepath.Join(filepath.Dir(match), "/target/"+mvnSimpleBomFilename+".xml"))
259258
buildCoordinates = append(buildCoordinates, coordinate)
260259
}
261260
}
@@ -274,25 +273,6 @@ func createBuildArtifactsMetadata(config *mavenBuildOptions, commonPipelineEnvir
274273
return nil, false
275274
}
276275

277-
func getPurlForThePom(pomFilePath string) string {
278-
bomPath := filepath.Join(filepath.Dir(pomFilePath) + "/target/" + mvnSimpleBomFilename + ".xml")
279-
exists, _ := piperutils.FileExists(bomPath)
280-
if !exists {
281-
log.Entry().Debugf("bom file doesn't exist and hence no pURL info: %v", bomPath)
282-
return ""
283-
}
284-
bom, err := piperutils.GetBom(bomPath)
285-
if err != nil {
286-
log.Entry().Warnf("failed to get bom file %s: %v", bomPath, err)
287-
return ""
288-
}
289-
290-
log.Entry().Debugf("Found purl: %s for the bomPath: %s", bom.Metadata.Component.Purl, bomPath)
291-
purl := bom.Metadata.Component.Purl
292-
293-
return purl
294-
}
295-
296276
func createOrUpdateProjectSettingsXML(projectSettingsFile string, altDeploymentRepositoryID string, altDeploymentRepositoryUser string, altDeploymentRepositoryPassword string, utils maven.Utils) (string, error) {
297277
if len(projectSettingsFile) > 0 {
298278
projectSettingsFilePath, err := maven.UpdateProjectSettingsXML(projectSettingsFile, altDeploymentRepositoryID, altDeploymentRepositoryUser, altDeploymentRepositoryPassword, utils)
@@ -310,15 +290,14 @@ func createOrUpdateProjectSettingsXML(projectSettingsFile string, altDeploymentR
310290
}
311291

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

316296
if len(javaCaCertFilePath) > 0 {
317297
existingJavaCaCerts = javaCaCertFilePath
318298
}
319299

320300
exists, err := fileUtils.FileExists(existingJavaCaCerts)
321-
322301
if err != nil {
323302
return errors.Wrap(err, "Could not find the existing java cacerts")
324303
}

cmd/mavenBuild_test.go

-54
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@
44
package cmd
55

66
import (
7-
"os"
8-
"path/filepath"
97
"testing"
108

11-
"github.com/SAP/jenkins-library/pkg/piperutils"
129
"github.com/stretchr/testify/assert"
1310
)
1411

1512
var cpe mavenBuildCommonPipelineEnvironment
1613

1714
func TestMavenBuild(t *testing.T) {
18-
1915
t.Run("mavenBuild should install the artifact", func(t *testing.T) {
2016
mockedUtils := newMavenMockUtils()
2117

@@ -123,54 +119,4 @@ func TestMavenBuild(t *testing.T) {
123119
assert.Equal(t, mockedUtils.Calls[0].Exec, "mvn")
124120
assert.Empty(t, cpe.custom.mavenBuildArtifacts)
125121
})
126-
127-
}
128-
129-
func createTempFile(t *testing.T, dir string, filename string, content string) string {
130-
filePath := filepath.Join(dir, filename)
131-
err := os.WriteFile(filePath, []byte(content), 0666)
132-
if err != nil {
133-
t.Fatalf("Failed to create temp file: %s", err)
134-
}
135-
return filePath
136-
}
137-
138-
func TestGetPurlForThePomAndDeleteIndividualBom(t *testing.T) {
139-
140-
t.Run("valid BOM file, aggregated BOM", func(t *testing.T) {
141-
tempDir, err := piperutils.Files{}.TempDir("", "test")
142-
if err != nil {
143-
t.Fatalf("Failed to create temp directory: %s", err)
144-
}
145-
146-
bomContent := `<bom>
147-
<metadata>
148-
<component>
149-
<purl>pkg:maven/com.example/[email protected]</purl>
150-
</component>
151-
<properties>
152-
<property name="maven.goal" value="makeAggregateBom" />
153-
</properties>
154-
</metadata>
155-
</bom>`
156-
pomFilePath := createTempFile(t, tempDir, "pom.xml", "")
157-
bomDir := filepath.Join(tempDir, "target")
158-
if err := os.MkdirAll(bomDir, 0777); err != nil {
159-
t.Fatalf("Failed to create temp directory: %s", err)
160-
}
161-
bomFilePath := createTempFile(t, bomDir, mvnSimpleBomFilename+".xml", bomContent)
162-
163-
purl := getPurlForThePom(pomFilePath)
164-
assert.Equal(t, "pkg:maven/com.example/[email protected]", purl)
165-
_, err = os.Stat(bomFilePath)
166-
assert.False(t, os.IsNotExist(err)) // File should not be deleted
167-
})
168-
169-
t.Run("BOM file does not exist", func(t *testing.T) {
170-
tempDir := t.TempDir()
171-
pomFilePath := createTempFile(t, tempDir, "pom.xml", "") // Create a temp pom file
172-
173-
purl := getPurlForThePom(pomFilePath)
174-
assert.Equal(t, "", purl)
175-
})
176122
}

0 commit comments

Comments
 (0)