1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-10-30 23:57:50 +02:00

Update npmExecuteScripts step (#3211)

* Update npmExecuteScripts step

* Fixed failing build

* Fixed path issue

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
Co-authored-by: Christian Volk <christian.volk@sap.com>
This commit is contained in:
ManjunathMS35
2022-02-07 14:46:03 +01:00
committed by GitHub
parent f3566ab7e0
commit 8108bb8f6f
4 changed files with 32 additions and 42 deletions

View File

@@ -20,25 +20,36 @@ func npmExecuteScripts(config npmExecuteScriptsOptions, telemetryData *telemetry
func runNpmExecuteScripts(npmExecutor npm.Executor, config *npmExecuteScriptsOptions, commonPipelineEnvironment *npmExecuteScriptsCommonPipelineEnvironment) error {
if config.Install {
packageJSONFiles, err := npmExecutor.FindPackageJSONFilesWithExcludes(config.BuildDescriptorExcludeList)
if err != nil {
return err
}
if len(config.BuildDescriptorList) > 0 {
if err := npmExecutor.InstallAllDependencies(config.BuildDescriptorList); err != nil {
return err
}
} else {
packageJSONFiles, err := npmExecutor.FindPackageJSONFilesWithExcludes(config.BuildDescriptorExcludeList)
if err != nil {
return err
}
err = npmExecutor.InstallAllDependencies(packageJSONFiles)
if err != nil {
return err
if err := npmExecutor.InstallAllDependencies(packageJSONFiles); err != nil {
return err
}
}
}
if config.CreateBOM {
packageJSONFiles, err := npmExecutor.FindPackageJSONFilesWithExcludes(config.BuildDescriptorExcludeList)
if err != nil {
return err
}
if len(config.BuildDescriptorList) > 0 {
if err := npmExecutor.CreateBOM(config.BuildDescriptorList); err != nil {
return err
}
} else {
packageJSONFiles, err := npmExecutor.FindPackageJSONFilesWithExcludes(config.BuildDescriptorExcludeList)
if err != nil {
return err
}
if err := npmExecutor.CreateBOM(packageJSONFiles); err != nil {
return err
if err := npmExecutor.CreateBOM(packageJSONFiles); err != nil {
return err
}
}
}

View File

@@ -29,7 +29,7 @@ func TestNpmExecuteScripts(t *testing.T) {
cpe := npmExecuteScriptsCommonPipelineEnvironment{}
t.Run("Call with packagesList", func(t *testing.T) {
config := npmExecuteScriptsOptions{Install: true, RunScripts: []string{"ci-build", "ci-test"}, BuildDescriptorList: []string{"src/package.json"}}
config := npmExecuteScriptsOptions{Install: true, RunScripts: []string{"ci-build", "ci-test"}, BuildDescriptorList: []string{"package.json", "src/package.json"}}
utils := npm.NewNpmMockUtilsBundle()
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))

View File

@@ -358,36 +358,15 @@ func (exec *Execute) CreateBOM(packageJSONFiles []string) error {
return err
}
if len(packageJSONFiles) > 0 {
path := filepath.Dir(packageJSONFiles[0])
createBOMConfig := []string{
// https://github.com/CycloneDX/cyclonedx-node-module does not contain schema parameter hence bom creation fails
//"--schema", "1.2", // Target schema version
"--include-license-text", "false",
"--include-dev", "false", // Include devDependencies
"--output", "bom.xml",
}
params := []string{
"cyclonedx-bom",
path,
}
params = append(params, createBOMConfig...)
// Generate BOM from first package.json
err := execRunner.RunExecutable("npx", params...)
if err != nil {
return err
}
// Merge BOM(s) into the current BOM
for _, packageJSONFile := range packageJSONFiles[1:] {
for _, packageJSONFile := range packageJSONFiles {
path := filepath.Dir(packageJSONFile)
params = []string{
params := []string{
"cyclonedx-bom",
path,
"--append",
"bom.xml",
"--include-license-text", "false",
"--include-dev", "false", // Include devDependencies
"--output", filepath.Join(path, "bom.xml"),
}
params = append(params, createBOMConfig...)
err := execRunner.RunExecutable("npx", params...)
if err != nil {
return err

View File

@@ -360,8 +360,8 @@ func TestNpm(t *testing.T) {
assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"install", "@cyclonedx/bom", "--no-save"}}, utils.execRunner.Calls[0])
assert.Equal(t, mock.ExecCall{Exec: "npx", Params: []string{"cyclonedx-bom", ".",
"--include-license-text", "false", "--include-dev", "false", "--output", "bom.xml"}}, utils.execRunner.Calls[1])
assert.Equal(t, mock.ExecCall{Exec: "npx", Params: []string{"cyclonedx-bom", "src", "--append", "bom.xml",
"--include-license-text", "false", "--include-dev", "false", "--output", "bom.xml"}}, utils.execRunner.Calls[2])
assert.Equal(t, mock.ExecCall{Exec: "npx", Params: []string{"cyclonedx-bom", "src",
"--include-license-text", "false", "--include-dev", "false", "--output", filepath.Join("src", "bom.xml")}}, utils.execRunner.Calls[2])
}
}
})