2021-11-16 16:02:57 +02:00
|
|
|
package buildsettings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BuildSettings struct {
|
2022-02-21 10:16:48 +02:00
|
|
|
GolangBuild []BuildOptions `json:"golangBuild,omitempty"`
|
|
|
|
GradleBuild []BuildOptions `json:"gradleBuild,omitempty"`
|
|
|
|
HelmExecute []BuildOptions `json:"helmExecute,omitempty"`
|
2021-11-16 16:02:57 +02:00
|
|
|
KanikoExecute []BuildOptions `json:"kanikoExecute,omitempty"`
|
2022-02-21 10:16:48 +02:00
|
|
|
MavenBuild []BuildOptions `json:"mavenBuild,omitempty"`
|
2021-11-16 16:02:57 +02:00
|
|
|
MtaBuild []BuildOptions `json:"mtaBuild,omitempty"`
|
2022-02-21 10:16:48 +02:00
|
|
|
PythonBuild []BuildOptions `json:"pythonBuild,omitempty"`
|
|
|
|
NpmExecuteScripts []BuildOptions `json:"npmExecuteScripts,omitempty"`
|
2021-11-16 16:02:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type BuildOptions struct {
|
|
|
|
Profiles []string `json:"profiles,omitempty"`
|
|
|
|
Publish bool `json:"publish,omitempty"`
|
|
|
|
CreateBOM bool `json:"createBOM,omitempty"`
|
|
|
|
LogSuccessfulMavenTransfers bool `json:"logSuccessfulMavenTransfers,omitempty"`
|
|
|
|
GlobalSettingsFile string `json:"globalSettingsFile,omitempty"`
|
|
|
|
DefaultNpmRegistry string `json:"defaultNpmRegistry,omitempty"`
|
|
|
|
BuildSettingsInfo string `json:"buildSettingsInfo,omitempty"`
|
|
|
|
DockerImage string `json:"dockerImage,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateBuildSettingsInfo(config *BuildOptions, buildTool string) (string, error) {
|
|
|
|
currentBuildSettingsInfo := BuildOptions{
|
|
|
|
CreateBOM: config.CreateBOM,
|
|
|
|
GlobalSettingsFile: config.GlobalSettingsFile,
|
|
|
|
LogSuccessfulMavenTransfers: config.LogSuccessfulMavenTransfers,
|
|
|
|
Profiles: config.Profiles,
|
|
|
|
Publish: config.Publish,
|
|
|
|
DefaultNpmRegistry: config.DefaultNpmRegistry,
|
2021-12-01 08:46:18 +02:00
|
|
|
DockerImage: config.DockerImage,
|
2021-11-16 16:02:57 +02:00
|
|
|
}
|
|
|
|
var jsonMap map[string][]interface{}
|
|
|
|
var jsonResult []byte
|
|
|
|
|
|
|
|
if config.BuildSettingsInfo != "" {
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(config.BuildSettingsInfo), &jsonMap)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "failed to unmarshal existing build settings json '%v'", config.BuildSettingsInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
if build, exist := jsonMap[buildTool]; exist {
|
|
|
|
if reflect.TypeOf(build).Kind() == reflect.Slice {
|
|
|
|
jsonMap[buildTool] = append(build, currentBuildSettingsInfo)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var settings []interface{}
|
|
|
|
settings = append(settings, currentBuildSettingsInfo)
|
|
|
|
jsonMap[buildTool] = settings
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonResult, err = json.Marshal(&jsonMap)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "Creating build settings failed with json marshalling")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var settings []BuildOptions
|
|
|
|
settings = append(settings, currentBuildSettingsInfo)
|
|
|
|
var err error
|
|
|
|
switch buildTool {
|
2022-02-21 10:16:48 +02:00
|
|
|
case "golangBuild":
|
2021-11-16 16:02:57 +02:00
|
|
|
jsonResult, err = json.Marshal(BuildSettings{
|
2022-02-21 10:16:48 +02:00
|
|
|
GolangBuild: settings,
|
2021-11-16 16:02:57 +02:00
|
|
|
})
|
2022-02-21 10:16:48 +02:00
|
|
|
case "gradleBuild":
|
2021-11-16 16:02:57 +02:00
|
|
|
jsonResult, err = json.Marshal(BuildSettings{
|
2022-02-21 10:16:48 +02:00
|
|
|
GradleBuild: settings,
|
|
|
|
})
|
|
|
|
case "helmExecute":
|
|
|
|
jsonResult, err = json.Marshal(BuildSettings{
|
|
|
|
HelmExecute: settings,
|
2021-11-16 16:02:57 +02:00
|
|
|
})
|
|
|
|
case "kanikoExecute":
|
|
|
|
jsonResult, err = json.Marshal(BuildSettings{
|
|
|
|
KanikoExecute: settings,
|
|
|
|
})
|
2022-02-21 10:16:48 +02:00
|
|
|
case "mavenBuild":
|
|
|
|
jsonResult, err = json.Marshal(BuildSettings{
|
|
|
|
MavenBuild: settings,
|
|
|
|
})
|
2021-11-16 16:02:57 +02:00
|
|
|
case "mtaBuild":
|
|
|
|
jsonResult, err = json.Marshal(BuildSettings{
|
|
|
|
MtaBuild: settings,
|
|
|
|
})
|
2022-02-21 10:16:48 +02:00
|
|
|
case "pythonBuild":
|
|
|
|
jsonResult, err = json.Marshal(BuildSettings{
|
|
|
|
PythonBuild: settings,
|
|
|
|
})
|
|
|
|
case "npmExecuteScripts":
|
|
|
|
jsonResult, err = json.Marshal(BuildSettings{
|
|
|
|
NpmExecuteScripts: settings,
|
|
|
|
})
|
2021-11-16 16:02:57 +02:00
|
|
|
default:
|
2022-02-21 10:16:48 +02:00
|
|
|
log.Entry().Warningf("buildTool '%s' not supported for creation of build settings", buildTool)
|
|
|
|
return "", nil
|
2021-11-16 16:02:57 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "Creating build settings failed with json marshalling")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 08:58:41 +02:00
|
|
|
log.Entry().Infof("build settings information successfully created with '%v", string(jsonResult))
|
2021-11-16 16:02:57 +02:00
|
|
|
|
|
|
|
return string(jsonResult), nil
|
|
|
|
|
|
|
|
}
|