Skip to content

Commit

Permalink
add .xml extension to default path and use constant
Browse files Browse the repository at this point in the history
  • Loading branch information
bitmaskit committed Jan 30, 2025
1 parent 5796716 commit eb2b9b6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
58 changes: 26 additions & 32 deletions pkg/maven/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/SAP/jenkins-library/pkg/log"
)

const defaultMavenProjectSettingsPath = ".pipeline/mavenProjectSettings.xml"

var getenv = os.Getenv

// SettingsDownloadUtils defines an interface for downloading and storing maven settings files.
Expand Down Expand Up @@ -40,7 +42,7 @@ func DownloadAndGetMavenParameters(globalSettingsFile string, projectSettingsFil
}

if len(projectSettingsFile) > 0 {
projectSettingsFileName, err := getSettingsFilePath(projectSettingsFile, ".pipeline/mavenProjectSettings.xml", utils, false)
projectSettingsFileName, err := getSettingsFilePath(projectSettingsFile, defaultMavenProjectSettingsPath, utils, false)
if err != nil {
return nil, err
}
Expand All @@ -66,7 +68,6 @@ func DownloadAndCopySettingsFiles(globalSettingsFile string, projectSettingsFile
return err
}
} else {

log.Entry().Debugf("Project settings file not provided via configuration.")
}

Expand All @@ -79,7 +80,6 @@ func DownloadAndCopySettingsFiles(globalSettingsFile string, projectSettingsFile
return err
}
} else {

log.Entry().Debugf("Global settings file not provided via configuration.")
}

Expand All @@ -98,9 +98,7 @@ func UpdateActiveProfileInSettingsXML(newActiveProfiles []string, utils Settings
}

var projectSettings Settings
err = xml.Unmarshal([]byte(settingsXMLContent), &projectSettings)

if err != nil {
if err = xml.Unmarshal(settingsXMLContent, &projectSettings); err != nil {
return fmt.Errorf("failed to unmarshal settings xml file '%v': %w", settingsFile, err)
}

Expand All @@ -123,9 +121,7 @@ func UpdateActiveProfileInSettingsXML(newActiveProfiles []string, utils Settings
settingsXmlString = Replacer.Replace(settingsXmlString)
xmlstring := []byte(xml.Header + settingsXmlString)

err = utils.FileWrite(settingsFile, xmlstring, 0777)

if err != nil {
if err = utils.FileWrite(settingsFile, xmlstring, 0777); err != nil {
return fmt.Errorf("failed to write maven Settings during <activeProfile> update xml: %w", err)
}
log.Entry().Infof("Successfully updated <acitveProfile> details in maven settings file : '%s'", settingsFile)
Expand Down Expand Up @@ -157,25 +153,26 @@ func CreateNewProjectSettingsXML(altDeploymentRepositoryID string, altDeployment

xmlstring = []byte(xml.Header + string(xmlstring))

err = utils.FileWrite(".pipeline/mavenProjectSettings.xml", xmlstring, 0777)
if err != nil {
if err = utils.FileWrite(defaultMavenProjectSettingsPath, xmlstring, 0777); err != nil {
return "", fmt.Errorf("failed to write maven Project Settings xml: %w", err)
}

log.Entry().Infof("Successfully created maven project settings with <server> details at .pipeline/mavenProjectSettings.xml")
log.Entry().Infof("Successfully created maven project settings with <server> details at %s", defaultMavenProjectSettingsPath)

return ".pipeline/mavenProjectSettings.xml", nil
return defaultMavenProjectSettingsPath, nil

}

func UpdateProjectSettingsXML(projectSettingsFile string, altDeploymentRepositoryID string, altDeploymentRepositoryUser string, altDeploymentRepositoryPassword string, utils SettingsDownloadUtils) (string, error) {
projectSettingsFileDestination := ".pipeline/mavenProjectSettings"
projectSettingsFileDestination := defaultMavenProjectSettingsPath
var err error
if exists, _ := utils.FileExists(projectSettingsFile); exists {
log.Entry().Debugf("Project settings file provided via configuration: %s", projectSettingsFile)
projectSettingsFileDestination = projectSettingsFile
err = addServerTagtoProjectSettingsXML(projectSettingsFile, altDeploymentRepositoryID, altDeploymentRepositoryUser, altDeploymentRepositoryPassword, utils)
} else {
err = addServerTagtoProjectSettingsXML(".pipeline/mavenProjectSettings", altDeploymentRepositoryID, altDeploymentRepositoryUser, altDeploymentRepositoryPassword, utils)
log.Entry().Debugf("Project settings file provided '%s' does not exist. Using default location: %s", projectSettingsFile, defaultMavenProjectSettingsPath)
err = addServerTagtoProjectSettingsXML(defaultMavenProjectSettingsPath, altDeploymentRepositoryID, altDeploymentRepositoryUser, altDeploymentRepositoryPassword, utils)
}

if err != nil {
Expand Down Expand Up @@ -246,31 +243,28 @@ func downloadAndCopySettingsFile(src string, dest string, utils SettingsDownload
log.Entry().Debugf("Copying file \"%s\" to \"%s\"", src, dest)

if strings.HasPrefix(src, "http:") || strings.HasPrefix(src, "https:") {
err := downloadSettingsFromURL(src, dest, utils, true)
if err != nil {
if err := downloadSettingsFromURL(src, dest, utils, true); err != nil {
return err
}
} else {

// for sake os symmetry it would be better to use a file protocol prefix here (file:)

parent := filepath.Dir(dest)
return nil
}

parentFolderExists, err := utils.FileExists(parent)
// for sake os symmetry it would be better to use a file protocol prefix here (file:)
parent := filepath.Dir(dest)
parentFolderExists, err := utils.FileExists(parent)
if err != nil {
return err
}

if err != nil {
if !parentFolderExists {
if err = utils.MkdirAll(parent, 0775); err != nil {
return err
}
}

if !parentFolderExists {
if err = utils.MkdirAll(parent, 0775); err != nil {
return err
}
}

if _, err := utils.Copy(src, dest); err != nil {
return err
}
if _, err := utils.Copy(src, dest); err != nil {
return err
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/maven/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ func TestSettings(t *testing.T) {

xmlstring := []byte(xml.Header + settingsXmlString)

utilsMock.FileWrite(".pipeline/mavenProjectSettings", xmlstring, 0777)
utilsMock.FileWrite(defaultMavenProjectSettingsPath, xmlstring, 0777)

projectSettingsFilePath, err := UpdateProjectSettingsXML(".pipeline/mavenProjectSettings", "dummyRepoId2", "dummyRepoUser2", "dummyRepoPassword2", utilsMock)
projectSettingsFilePath, err := UpdateProjectSettingsXML(defaultMavenProjectSettingsPath, "dummyRepoId2", "dummyRepoUser2", "dummyRepoPassword2", utilsMock)
if assert.NoError(t, err) {
projectSettingsContent, _ := utilsMock.FileRead(projectSettingsFilePath)
var projectSettings Settings
Expand All @@ -162,9 +162,9 @@ func TestSettings(t *testing.T) {

utilsMock := newSettingsDownloadTestUtilsBundle()
xmlstring := []byte("well this is obviously invalid")
utilsMock.FileWrite(".pipeline/mavenProjectSettings", xmlstring, 0777)
utilsMock.FileWrite(defaultMavenProjectSettingsPath, xmlstring, 0777)

_, err := UpdateProjectSettingsXML(".pipeline/mavenProjectSettings", "dummyRepoId2", "dummyRepoUser2", "dummyRepoPassword2", utilsMock)
_, err := UpdateProjectSettingsXML(defaultMavenProjectSettingsPath, "dummyRepoId2", "dummyRepoUser2", "dummyRepoPassword2", utilsMock)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "failed to unmarshal settings xml file")
}
Expand Down

0 comments on commit eb2b9b6

Please sign in to comment.