Skip to content

Commit

Permalink
fix(maven): Add proper error handling for invalid settings.xml when p…
Browse files Browse the repository at this point in the history
…ublishing (#4884)

Co-authored-by: jliempt <>
  • Loading branch information
jliempt authored Apr 4, 2024
1 parent 38fe2ea commit a129cc4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/maven/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,16 @@ func CreateNewProjectSettingsXML(altDeploymentRepositoryID string, altDeployment

func UpdateProjectSettingsXML(projectSettingsFile string, altDeploymentRepositoryID string, altDeploymentRepositoryUser string, altDeploymentRepositoryPassword string, utils SettingsDownloadUtils) (string, error) {
projectSettingsFileDestination := ".pipeline/mavenProjectSettings"
var err error
if exists, _ := utils.FileExists(projectSettingsFile); exists {
projectSettingsFileDestination = projectSettingsFile
addServerTagtoProjectSettingsXML(projectSettingsFile, altDeploymentRepositoryID, altDeploymentRepositoryUser, altDeploymentRepositoryPassword, utils)
err = addServerTagtoProjectSettingsXML(projectSettingsFile, altDeploymentRepositoryID, altDeploymentRepositoryUser, altDeploymentRepositoryPassword, utils)
} else {
addServerTagtoProjectSettingsXML(".pipeline/mavenProjectSettings", altDeploymentRepositoryID, altDeploymentRepositoryUser, altDeploymentRepositoryPassword, utils)
err = addServerTagtoProjectSettingsXML(".pipeline/mavenProjectSettings", altDeploymentRepositoryID, altDeploymentRepositoryUser, altDeploymentRepositoryPassword, utils)
}

if err != nil {
return "", fmt.Errorf("failed to unmarshal settings xml file '%v': %w", projectSettingsFile, err)
}
return projectSettingsFileDestination, nil

Expand Down
12 changes: 12 additions & 0 deletions pkg/maven/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ func TestSettings(t *testing.T) {
}
})

t.Run("update server tag in existing settings file - invalid settings.xml", func(t *testing.T) {

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

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

t.Run("update active profile tag in existing settings file", func(t *testing.T) {

utilsMock := newSettingsDownloadTestUtilsBundle()
Expand Down

0 comments on commit a129cc4

Please sign in to comment.