1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-03-27 21:49:15 +02:00

feat(sonar): respect existing SONAR_SCANNER_OPTS definition (#5230)

* Don't overwrite SONAR_SCANNER_OPTS

* Add tests

* Reset SONAR_SCANNER_OPTS after testing it

* Reset SONAR_SCANNER_OPTS after testing it

---------

Co-authored-by: Ivan Nikiforov <nikiforr@gmail.com>
This commit is contained in:
Marc Bormeth 2025-02-10 14:32:33 +01:00 committed by GitHub
parent 5561d2a8ca
commit fad12860df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 3 deletions

View File

@ -415,13 +415,23 @@ func loadSonarScanner(url string, client piperhttp.Downloader) error {
return nil
}
func addSonarScannerOpts(opts string) {
tmpOpts := os.Getenv("SONAR_SCANNER_OPTS")
if len(tmpOpts) > 0 {
log.Entry().Debug("SONAR_SCANNER_OPTS already set. Appending to existing value: " + tmpOpts)
sonar.addEnvironment("SONAR_SCANNER_OPTS=" + tmpOpts + " " + opts)
} else {
sonar.addEnvironment("SONAR_SCANNER_OPTS=" + opts)
}
}
func loadCertificates(certificateList []string, client piperhttp.Downloader, runner command.ExecRunner) error {
truststorePath := filepath.Join(getWorkingDir(), ".certificates")
truststoreFile := filepath.Join(truststorePath, "cacerts")
if exists, _ := fileUtilsExists(truststoreFile); exists {
// use local existing trust store
sonar.addEnvironment("SONAR_SCANNER_OPTS=" + keytool.GetMavenOpts(truststoreFile))
addSonarScannerOpts(keytool.GetMavenOpts(truststoreFile))
log.Entry().WithField("trust store", truststoreFile).Info("Using local trust store")
} else if len(certificateList) > 0 {
// create download temp dir
@ -451,7 +461,7 @@ func loadCertificates(certificateList []string, client piperhttp.Downloader, run
// return errors.Wrap(err, "Adding certificate to keystore failed")
}
}
sonar.addEnvironment("SONAR_SCANNER_OPTS=" + keytool.GetMavenOpts(truststoreFile))
addSonarScannerOpts(keytool.GetMavenOpts(truststoreFile))
log.Entry().WithField("trust store", truststoreFile).Info("Using local trust store")
} else {
log.Entry().Debug("Download of TLS certificates skipped")

View File

@ -176,6 +176,8 @@ func TestRunSonar(t *testing.T) {
PullRequestProvider: "GitHub",
}
fileUtilsExists = mockFileUtilsExists(true)
os.Setenv("SONAR_SCANNER_OPTS", "-Xmx42m")
defer os.Setenv("SONAR_SCANNER_OPTS", "")
// test
err := runSonar(options, &mockDownloadClient, &mockRunner, apiClient, &mock.FilesMock{}, &sonarExecuteScanInflux{})
// assert
@ -184,7 +186,7 @@ func TestRunSonar(t *testing.T) {
assert.Contains(t, sonar.options, "-Dsonar.organization=SAP")
assert.Contains(t, sonar.environment, "SONAR_HOST_URL="+sonarServerURL)
assert.Contains(t, sonar.environment, "SONAR_TOKEN=secret-ABC")
assert.Contains(t, sonar.environment, "SONAR_SCANNER_OPTS=-Djavax.net.ssl.trustStore="+filepath.Join(getWorkingDir(), ".certificates", "cacerts")+" -Djavax.net.ssl.trustStorePassword=changeit")
assert.Contains(t, sonar.environment, "SONAR_SCANNER_OPTS=-Xmx42m -Djavax.net.ssl.trustStore="+filepath.Join(getWorkingDir(), ".certificates", "cacerts")+" -Djavax.net.ssl.trustStorePassword=changeit")
})
t.Run("with custom options", func(t *testing.T) {
// init
@ -456,6 +458,7 @@ func TestSonarLoadCertificates(t *testing.T) {
}
fileUtilsExists = mockFileUtilsExists(true)
defer func() { fileUtilsExists = piperutils.FileExists }()
defer os.Setenv("SONAR_SCANNER_OPTS", "")
// test
err := loadCertificates([]string{}, &mockClient, &mockRunner)
// assert
@ -471,6 +474,7 @@ func TestSonarLoadCertificates(t *testing.T) {
options: []string{},
}
fileUtilsExists = mockFileUtilsExists(false)
defer os.Setenv("SONAR_SCANNER_OPTS", "")
// test
err := loadCertificates([]string{"https://sap.com/custom-1.crt", "https://sap.com/custom-2.crt"}, &mockClient, &mockRunner)
// assert