From 7d92447c6676b363087b08a9aab97e038cade693 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sun, 24 May 2020 20:29:29 +0200 Subject: [PATCH 1/2] make sonar quality gate timeout configurable --- .../cloudogu/ces/cesbuildlib/SonarQube.groovy | 3 ++- .../ces/cesbuildlib/ScriptMock.groovy | 4 ++++ .../ces/cesbuildlib/SonarQubeTest.groovy | 24 ++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/com/cloudogu/ces/cesbuildlib/SonarQube.groovy b/src/com/cloudogu/ces/cesbuildlib/SonarQube.groovy index e2f7ab27..ca3a7298 100644 --- a/src/com/cloudogu/ces/cesbuildlib/SonarQube.groovy +++ b/src/com/cloudogu/ces/cesbuildlib/SonarQube.groovy @@ -12,6 +12,7 @@ class SonarQube implements Serializable { // If enabled uses the branch plugin, available for developer edition and above boolean isUsingBranchPlugin = false boolean isIgnoringBranches = false + int timeoutInMinutes = 2 protected Map config @Deprecated @@ -64,7 +65,7 @@ class SonarQube implements Serializable { } protected boolean doWaitForQualityGateWebhookToBeCalled() { - script.timeout(time: 2, unit: 'MINUTES') { // Needed when there is no webhook for example + script.timeout(time: timeoutInMinutes, unit: 'MINUTES') { // Needed when there is no webhook for example def qGate = script.waitForQualityGate() script.echo "SonarQube Quality Gate status: ${qGate.status}" if (qGate.status != 'OK') { diff --git a/test/com/cloudogu/ces/cesbuildlib/ScriptMock.groovy b/test/com/cloudogu/ces/cesbuildlib/ScriptMock.groovy index 6204f72d..3e540ccd 100644 --- a/test/com/cloudogu/ces/cesbuildlib/ScriptMock.groovy +++ b/test/com/cloudogu/ces/cesbuildlib/ScriptMock.groovy @@ -12,6 +12,9 @@ class ScriptMock { def expectedShRetValueForScript = [:] String actualSonarQubeEnv + + Map actualTimeoutParams = [:] + List actualUsernamePasswordArgs = [] List actualShStringArgs = new LinkedList<>() List actualEcho = new LinkedList<>() @@ -55,6 +58,7 @@ class ScriptMock { } def timeout(Map params, closure) { + this.actualTimeoutParams = params return closure.call() } diff --git a/test/com/cloudogu/ces/cesbuildlib/SonarQubeTest.groovy b/test/com/cloudogu/ces/cesbuildlib/SonarQubeTest.groovy index c0a77028..dd57fae3 100644 --- a/test/com/cloudogu/ces/cesbuildlib/SonarQubeTest.groovy +++ b/test/com/cloudogu/ces/cesbuildlib/SonarQubeTest.groovy @@ -199,7 +199,7 @@ class SonarQubeTest { assert mavenMock.additionalArgs == '-X -Dsonar.branch.name=develop ' } - @Test + @Test void analyzeWithBranchPluginWithConfiguredIntegrationBranch() throws Exception { scriptMock.env = [ BRANCH_NAME: 'feature' @@ -273,6 +273,28 @@ class SonarQubeTest { assert !qualityGate } + @Test + void waitForQualityGateWithDefaultTimeout() { + scriptMock.expectedQGate = [status: 'OK'] + + new SonarQube(scriptMock, 'sqEnv').waitForQualityGateWebhookToBeCalled() + + assert scriptMock.actualTimeoutParams.time == 2 + assert scriptMock.actualTimeoutParams.unit == 'MINUTES' + } + + @Test + void waitForQualityGateWithConfiguredTimeout() { + scriptMock.expectedQGate = [status: 'OK'] + + def sonarQube = new SonarQube(scriptMock, 'sqEnv') + sonarQube.timeoutInMinutes = 10 + sonarQube.waitForQualityGateWebhookToBeCalled() + + assert scriptMock.actualTimeoutParams.time == 10 + assert scriptMock.actualTimeoutParams.unit == 'MINUTES' + } + private void assertSonarHostUrlError(configMapg) { def exception = shouldFail { new SonarQube(scriptMock, configMapg).analyzeWith(mavenMock) From a5c11c30517fa9eed9571bdb97855676687c1792 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sun, 24 May 2020 20:41:58 +0200 Subject: [PATCH 2/2] added documentation for SonarQube webhook timeout --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f916ab88..11d78d95 100644 --- a/README.md +++ b/README.md @@ -677,6 +677,7 @@ stage('Statical Code Analysis') { def sonarQube = new SonarQube(this, [sonarQubeEnv: 'sonarQubeServerSetupInJenkins']) sonarQube.analyzeWith(new MavenInDocker(this, "3.5.0-jdk-8")) + sonarQube.timeoutInMinutes = 4 if (!sonarQube.waitForQualityGateWebhookToBeCalled()) { unstable("Pipeline unstable due to SonarQube quality gate failure") @@ -689,6 +690,7 @@ Note that * Calling `waitForQualityGateWebhookToBeCalled()` requires a WebHook to be setup in your SonarQube server (globally or per project), that notifies Jenkins (url: `https://yourJenkinsInstance/sonarqube-webhook/`). See [SonarQube Scanner for Jenkins](https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-AnalyzinginaJenkinspipeline). +* Jenkins will wait for the webhook with a default timeout of 2 minutes, for big projects this might be to short and can be configured with the `timeoutInMinutes` property. * Calling `waitForQualityGateWebhookToBeCalled()` will only work when an analysis has been performed in the current job, i.e. `analyzeWith()` has been called and in conjuction with `sonarQubeEnv`. * When used in conjunction with [SonarQubeCommunity/sonar-build-breaker](https://github.com/SonarQubeCommunity/sonar-build-breaker),