diff --git a/src/com/cloudogu/ces/cesbuildlib/SonarQube.groovy b/src/com/cloudogu/ces/cesbuildlib/SonarQube.groovy index 6e00dd64..78a24644 100644 --- a/src/com/cloudogu/ces/cesbuildlib/SonarQube.groovy +++ b/src/com/cloudogu/ces/cesbuildlib/SonarQube.groovy @@ -112,7 +112,7 @@ class SonarQube implements Serializable { // Avoid exception "The main branch must not have a target" on master branch mvn.additionalArgs += " -Dsonar.branch.target=master " } - } else { + } else if (script.env.BRANCH_NAME) { mvn.additionalArgs += " -Dsonar.branch=${script.env.BRANCH_NAME} " } } diff --git a/test/com/cloudogu/ces/cesbuildlib/SonarQubeTest.groovy b/test/com/cloudogu/ces/cesbuildlib/SonarQubeTest.groovy index 2a22f5f7..5a563230 100644 --- a/test/com/cloudogu/ces/cesbuildlib/SonarQubeTest.groovy +++ b/test/com/cloudogu/ces/cesbuildlib/SonarQubeTest.groovy @@ -89,6 +89,23 @@ class SonarQubeTest { assert exception.message == "Requires either 'sonarQubeEnv', 'token' or 'usernamePassword' parameter." } + @Test + void analyzeWithoutBranchName() throws Exception { + def sonarQube = new SonarQube(scriptMock, [usernamePassword: 'usrPwCred', sonarHostUrl: 'http://ces/sonar']) + + scriptMock.env = [ + USERNAME: 'usr', + PASSWORD: 'pw', + ] + + sonarQube.analyzeWith(mavenMock) + + assert mavenMock.args == + 'sonar:sonar -Dsonar.host.url=http://ces/sonar -Dsonar.login=usr -Dsonar.password=pw ' + assert !mavenMock.additionalArgs.contains('-Dsonar.branch') + assert scriptMock.actualUsernamePasswordArgs['credentialsId'] == 'usrPwCred' + } + void analyzeWith(SonarQube sonarQube) throws Exception { scriptMock.env = [ SONAR_MAVEN_GOAL : 'sonar:sonar',