@@ -100,13 +100,7 @@ class SonarQube implements Serializable {
100100 if (isIgnoringBranches) {
101101 return
102102 }
103-
104- // Run SQ analysis in specific project for feature, hotfix, etc.
105- // Note that -Dsonar.branch is deprecated from SQ 6.6: https://docs.sonarqube.org/display/SONAR/Analysis+Parameters
106- // However, the alternative (the branch plugin is paid version only)
107- // See https://docs.sonarqube.org/display/PLUG/Branch+Plugin
108- // An alternative could be this:
109- // https://github.com/mc1arke/sonarqube-community-branch-plugin
103+ def artifactId = mvn. artifactId. trim()
110104 if (isUsingBranchPlugin) {
111105 mvn. additionalArgs + = " -Dsonar.branch.name=${ script.env.BRANCH_NAME} "
112106
@@ -116,39 +110,29 @@ class SonarQube implements Serializable {
116110 // Avoid exception "The main branch must not have a target" on master branch
117111 mvn. additionalArgs + = " -Dsonar.branch.target=${ targetBranch} "
118112 }
119- } else if (script. env. BRANCH_NAME ) {
120- // From SonarQube 7.9 "-Dsonar.branch" leads to an exception, because it's deprecated.
121- // mvn.additionalArgs += " -Dsonar.branch=${script.env.BRANCH_NAME} "
122- // In order to not break behavior of ces-build-lib, we re-implement similar behavior in ces-build-lib.
123-
124- // Note that the legacy "-Dsonar.branch" resulted in the display name "<maven name> <branch name>"
125- // We can't do that because the blank sends us to jenkis-shell-quoting-hell.
126- // Even this didn't help: https://gist.github.com/Faheetah/e11bd0315c34ed32e681616e41279ef4
127- // So change it to "<maven artifact>:<branch name>", as artifact is not allowed to consist spaces
128-
129- // We also apply this logic to PRs, since GitHub Plugin is deprecated from 7.2+.
130- // From SonarQube 6.6 "-Dsonar.analysis.mode" leads to an exception, because it's deprecated.
131- // There seems to be no replacement in the community version.
132- // See https://docs.sonarqube.org/display/PLUG/GitHub+Plugin
133- // Some examples for Env Vars when building PRs.
113+ // Use -Dsonar.branch.name with following plugin:
114+ // https://github.com/mc1arke/sonarqube-community-branch-plugin
115+ // Some examples for Env Vars when building PRs.
134116 // BRANCH_NAME=PR-26
135- // CHANGE_BRANCH=feature/simpify_git_push
117+ // CHANGE_BRANCH=feature/simplify_git_push
136118 // CHANGE_TARGET=develop
137-
138- def artifactId = mvn. artifactId. trim()
139- // Malformed key for Project: 'groupId:artifactId:feature/something'.
140- // Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.
141- String projectKey = replaceCharactersNotAllowedInProjectKey(script. env. BRANCH_NAME )
142- String projectName = script. env. BRANCH_NAME
143- mvn. additionalArgs + = " -Dsonar.projectKey=${ mvn.groupId} :${ artifactId} :${ projectKey} " +
144- " -Dsonar.projectName=${ artifactId} :${ projectName} "
119+ } else if (script. env. CHANGE_TARGET ) {
120+ mvn. additionalArgs + = " -Dsonar.projectKey=${ replaceCharactersNotAllowedInProjectKey(artifactId)} " +
121+ " -Dsonar.projectName=${ artifactId} " +
122+ " -Dsonar.pullrequest.key=${ script.env.CHANGE_ID} " +
123+ " -Dsonar.pullrequest.branch=${ script.env.CHANGE_BRANCH} " +
124+ " -Dsonar.pullrequest.base=${ script.env.CHANGE_TARGET} "
125+ } else if (script. env. BRANCH_NAME ) {
126+ mvn. additionalArgs + = " -Dsonar.projectKey=${ replaceCharactersNotAllowedInProjectKey(artifactId)} " +
127+ " -Dsonar.projectName=${ artifactId} " +
128+ " -Dsonar.branch.name=${ script.env.BRANCH_NAME} "
145129 }
146130 }
147131
148- protected String replaceCharactersNotAllowedInProjectKey (String potentialProjectKey ) {
149- return potentialProjectKey. replaceAll(" [^a-zA-Z0-9-_.:]" , " _" );
132+ protected static String replaceCharactersNotAllowedInProjectKey (String potentialProjectKey ) {
133+ return potentialProjectKey. replaceAll(" [^a-zA-Z0-9-_.:]" , " _" )
150134 }
151-
135+
152136 protected String determineIntegrationBranch () {
153137 if (config[' integrationBranch' ]) {
154138 return config[' integrationBranch' ]
@@ -221,7 +205,7 @@ class SonarQube implements Serializable {
221205 }
222206
223207 analyzeWith(mvn, script. env. SONAR_MAVEN_GOAL , script. env. SONAR_HOST_URL , script. env. SONAR_AUTH_TOKEN ,
224- sonarExtraProps)
208+ sonarExtraProps)
225209 }
226210 }
227211 }
@@ -257,9 +241,9 @@ class SonarQube implements Serializable {
257241
258242 def executeWith (Maven mvn ) {
259243 script. withCredentials([script. usernamePassword(credentialsId : usernameAndPasswordCredential,
260- passwordVariable : ' PASSWORD' , usernameVariable : ' USERNAME' )]) {
244+ passwordVariable : ' PASSWORD' , usernameVariable : ' USERNAME' )]) {
261245 analyzeWith(mvn, ' sonar:sonar' , host, script. env. USERNAME ,
262- " -Dsonar.password=${ script.env.PASSWORD} " )
246+ " -Dsonar.password=${ script.env.PASSWORD} " )
263247 }
264248 }
265249 }
0 commit comments